Search code examples
c#.netwebformselmah

Change server variable values that are logged in Elmah


I configured Elmah in my application and when I looked at the error logs I saw that in the User section Elmah logged an ID instead of the current logged in username a shown below.

I was wondering if there is a place where I can change how the values of these server variables are set so I can put say an email or proper username in the Auth_User variable..Looking at Elmah documentation I was only able to figure out how to filter out Error logging but not actually how to update server variable values before logging.

Elmah


Solution

  • I've written a blog post about exactly that: Enrich ELMAH errors using error filtering hook.

    To summarize, use ELMAH's error filtering to overwrite the message logged to ELMAH.

    In Global.asax.cs add the following code:

    void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
    {
        var httpContext = args.Context as HttpContext;
        if (httpContext != null)
        {
            var error = new Error(args.Exception, httpContext);
            error.User = GetUserFromDatabase();
            ErrorLog.GetDefault(httpContext).Log(error);
            args.Dismiss();
        }
    }
    

    How you implement the line where you set the User property on error will be up to your application. But the code will let you set something like the user's name, username, or email.