Click here to Skip to main content
Click here to Skip to main content

How to Log Javascript Errors in C#

By , 29 Dec 2011
 

This code shows how to handle a JavaScript error by posting the details back to the server to be logged. This code can be wired up to use any logging system.

The errors.js file establishes a handler for window.onerror to call the handleError function. The handleError function creates a web request (using the createHttpRequest function in httpRequests.js) and posts the error details back to LogJavascriptError.aspx. The LogJavascriptError.aspx file then logs the details to the file.

errors.js
var applicationPath = '';
 
function handleError(msg, srcUrl, line){
 
    var receiveReq = createHttpRequest();
 
    var url = applicationPath + '/Admin/LogJavascriptError.aspx';
 
    var data  = "ErrorData=" + msg + "&SourcePage=" + srcUrl + "&Line=" + line;
 
    receiveReq.open("POST", url, true);
 
    receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    receiveReq.setRequestHeader("Content-length", data.length);
    receiveReq.setRequestHeader("Connection", "close");
 
    receiveReq.send(data);
}
 
window.onerror=handleError;

http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/WWW/js/errors.js[^]

httpRequests.js
function createHttpRequest() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest(); //Not IE
    } else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } else {
        alert("Your browser doesn't support the XmlHttpRequest object.  Please upgrade to Firefox.");
    }
}

http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/WWW/js/httpRequests.js[^]

LogJavascriptError.aspx
string errorData = Request.Form["ErrorData"];
string sourcePage = Request.Form["SourcePage"];
string line = Request.Form["Line"];
 
string full = "Javascript error: " + errorData + Environment.NewLine + 
              "Location: " + sourcePage + ", line " + line;
 
LogWriter.Error(full);
 
Response.Write("Done");

http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/WWW/Admin/LogJavascriptError.aspx[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SoftwareMonkeys
SoftwareMonkeys
Australia Australia
Member
Founder of www.softwaremonkeys.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionBe careful heremembertmbgfan29 Dec '11 - 5:54 
This is nice, but I would be careful here. Someone can easily start POSTing garbage data to your LogJavascriptError.aspx page and pollute your logs.
AnswerRe: Be careful herememberSoftwareMonkeys29 Dec '11 - 10:38 
Very good point. I might need to look into mitigating that because one day it could be a problem.
GeneralRe: Be careful herememberbaskinhu3 Jan '12 - 4:06 
sure, but this would still be very usefull during debug/development and during the test phase of an application right?
GeneralRe: Be careful herememberSoftwareMonkeys3 Jan '12 - 11:32 
Yes it can be very useful during dev/testing/etc. but I want to use it on live sites so I'm going to look into validating the source of the post.
 
If I can figure out how to do that reliably I'll post an update.
 
Has anyone else already solved this?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 29 Dec 2011
Article Copyright 2011 by SoftwareMonkeys
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid