سؤال

I've just implemented Elmah for logging in my MVC3 app, and of course all is well, except that when I use signalling to log a custom exception, Elmah seems to 'see' the InnerException property of my custom exception, but not the custom exception itself.

When I use the code below to signal the exception, instead of seeing, "CtsDataException: Error" in my error log, as I would expect, I see, "DbEntityValidation: Validation failed for one or more entities.", the inner exception and its message. If I open the log item, I see that my custom exception has correctly been logged, so it looks like the 'exception descriptor' is wrong, not the actual log entry.

What am I doing wrong?

PS, my custom exception is as such:

public class CtsDataException: Exception
{
    public CtsDataException(string message, Exception innerException): base(message, innerException)
    {
        ValidationResults = new List<CtsDbValidationResult>();
        var vex = innerException as DbEntityValidationException;
        if (vex != null)
        {
            ValidationResults = vex.EntityValidationErrors.Select(e => new CtsDbValidationResult(e)).ToList();
        }
    }
    public IEnumerable<CtsDbValidationResult> ValidationResults { get; set; }
}

The signalling code looks like this:

protected void HandleDbEntityValidationException(DbEntityValidationException vex, string message)
{
    var ctsEx = new CtsDataException(message, vex);
    ErrorSignal.FromCurrentContext().Raise(ctsEx);
}

HandleDbEntityValidationException is on my base controller. It is invoked in derived controllers like this:

catch (DbEntityValidationException vx)
{
    var msg = string.Format("Error updating employee '{0}'", entity.RefNum);
    HandleDbEntityValidationException(vx, msg);
}
هل كانت مفيدة؟

المحلول

I've done some of my own testing and come to the conclusion that it's not ELMAH choosing to report only the InnerException. If you take a look at the details for the error and then click Original ASP.NET error page, you'll see that the original yellow screen that occurred will list the Exception Details as the InnerException and not the primary custom exception thrown. The stack trace further shows the original custom exception that was thrown instead. This is the information ELMAH is using in logging the error.

My testing consisted of creating a CustomException class that did nothing more than inherit from Exception. I then simply called:

throw new CustomException("error!", new NullReferenceException());

... and what got reported was the NullReferenceException with CustomException only appearing in the Stack Trace.

نصائح أخرى

My theory of choice in this scenario is that ELMAH is choosing to display the exception that was thrown rather than the exceptions used to wrap it. If ELMAH had a way to include a message with the log, I think this "wrap in a more descriptive but ultimately irrelevant exception that is never thrown" malarkey could end.

I realize that I'm a few years late to actually answer the original question on time, but for others trying to achieve what I think was the OP's intention, I came across a bit of smartness in the LibLog package for Hangfire (slightly adapted):

        var _errorType = Type.GetType("Elmah.Error, Elmah");
        dynamic error = Activator.CreateInstance(_errorType, originalException);

        error.Message = "Your custom message";
        error.Type = "Error"; // Or type of original exception or ...
        error.Time = DateTime.Now;

        Elmah.ErrorLog.GetDefault(null).Log(error);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top