Search code examples
asp.net-mvc-4elmah.mvc

Elmah: How to hide username?


By default Elmah obviously logs the user name as part of its log entries. Our costumer sees this as sensible data and wants it removed. How can this be done?

(By the way: There also is the "Code" field in the logs. Its always empty. What does this field mean?)


Solution

  • Add the following to Global.asax:

    void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
    {
        var httpContext = args.Context as HttpContext;
        if (httpContext != null && httpContext.Request.User.Identity.IsAuthenticated)
        {
            var error = new Error(args.Exception, httpContext);
            error.User = "***hidden***";
            ErrorLog.GetDefault(httpContext).Log(error);
            args.Dismiss();
        }
    }
    

    Adapted from: http://docs.elmah.io/remove-sensitive-form-data/

    EDIT

    To answer you other question, the "Code" field should be the HTTP status code: 500, 404, etc.