Reputation: 2504
I'm using ELMAH to log my .net errors. It's working great, but I want to extend the error logging to include client side errors, i.e arbitrary JavaScript errors. I can capture the errors using window.onerror event and then call a .net handler (.ashx) to log the error in elmah, but this is just my little hack to solve the problem. Is there a better way to log client side error to elmah?
Upvotes: 14
Views: 6315
Reputation: 479
Take a look at this.
http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-5--javascript
The only modification I had to make to the above code concerned the bundling.
I replaced
...with @Scripts.Render("~/stacktrace.js")
And in my bundle config I added the line... bundles.Add(new ScriptBundle("~/stacktrace.js").Include("~/Scripts/stacktrace.js"));
This makes the code compatible with the newer MVC 4 bundling.
Upvotes: 0
Reputation: 984
Here's a more complete solution (I think I grabbed it from the ELMAH site... can't remember)
ErrorLogging.js:
window.onerror = function (msg, url, line) {
logError(msg, arguments.callee.trace());
}
function logError(ex, stack) {
if (ex == null) return;
if (logErrorUrl == null) {
alert('logErrorUrl must be defined.');
return;
}
var url = ex.fileName != null ? ex.fileName : document.location;
if (stack == null && ex.stack != null) stack = ex.stack;
// format output
var out = ex.message != null ? ex.name + ": " + ex.message : ex;
out += ": at document path '" + url + "'.";
if (stack != null) out += "\n at " + stack.join("\n at ");
// send error message
jQuery.ajax({
type: 'POST',
url: logErrorUrl,
data: { message: out }
});
}
Function.prototype.trace = function()
{
var trace = [];
var current = this;
while(current)
{
trace.push(current.signature());
current = current.caller;
}
return trace;
}
Function.prototype.signature = function()
{
var signature = {
name: this.getName(),
params: [],
toString: function()
{
var params = this.params.length > 0 ?
"'" + this.params.join("', '") + "'" : "";
return this.name + "(" + params + ")"
}
};
if (this.arguments)
{
for(var x=0; x<this.arguments.length; x++)
signature.params.push(this.arguments[x]);
}
return signature;
}
Function.prototype.getName = function()
{
if (this.name)
return this.name;
var definition = this.toString().split("\n")[0];
var exp = /^function ([^\s(]+).+/;
if (exp.test(definition))
return definition.split("\n")[0].replace(exp, "$1") || "anonymous";
return "anonymous";
}
Layout/Master Page:
<script src="@Url.Content( "~/Scripts/ErrorLogging.js" )" type="text/javascript"></script>
<script type="text/javascript">
//This needs to be here to be on everypage
var logErrorUrl = '@Url.Action( "LogJavaScriptError", "Home" )';
</script>
HomeController.cs:
[HttpPost]
public void LogJavaScriptError( string message ) {
ErrorSignal.FromCurrentContext().Raise( new JavaScriptErrorException( message ) );
}
JavaScriptErrorException.cs:
[Serializable]
public class JavaScriptErrorException: Exception{
public JavaScriptErrorException( string message ) : base ( message ){}
}
Upvotes: 11
Reputation: 2504
Catch all javascript errors by handling the window.onerror event and make ajax call to manually log error in elmah
window.onerror = function (msg, url, linenumber) {
//make ajax call with all error details and log error directly into the elmah database
//show freindly error msg here to user
//hide error from browser
return true;
}
No better solution was found at the time.
Upvotes: 1
Reputation: 4681
Take a look at hoptoadapp.com and how they do javascript reporting.
It's the exact same thing, but a different backend :-)
Upvotes: 4