Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clientside error logging using Elmah

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?

like image 306
Daniel Brink Avatar asked May 04 '10 10:05

Daniel Brink


People also ask

How do you check Elmah error?

Build the application, run it in the browser, and navigate to http://www.yoursite.com/elmah.axd. You are prompted to log in before you see the content. After a successful authentication, you see a web page to remotely view the entire log of recorded exceptions.

What is Elmah logging?

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Does Elmah work with .NET core?

Logging to elmah.io from ASP.NET CoreYou can add logs, exceptions to elmah.io directly from an ASP.NET Core application using the Elmah. Io. ASPNetCore packages. These packages can be added to the project using the nuget package manager.


2 Answers

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 ){}
}
like image 109
rbj325 Avatar answered Sep 28 '22 09:09

rbj325


Take a look at hoptoadapp.com and how they do javascript reporting.

It's the exact same thing, but a different backend :-)

like image 38
changelog Avatar answered Sep 28 '22 10:09

changelog