How to register custom exception with optional Data property in Elmah?

I have a custom exception where I have overridden a property Data

using reflection like the following ...

public class MyCustomException : Exception
{
    private readonly SomeModel _log;
    public MyCustomException(SomeModel log)
        : base(string.Format("Could not insert to some table"))
    {
        _log = log;
    }

    public override System.Collections.IDictionary Data
    {
        get
        {
            var data = new Dictionary<string, object>();
            foreach (PropertyInfo pinfo in _log.GetType().GetProperties())
            {
                data.Add(pinfo.Name, pinfo.GetType().GetProperty(pinfo.Name));
            }
            return data;
        }
    }
}

      

When the above exception is thrown, it is logged in elmah, but Data

not logged.

What changes do I need to make so that Data

elmah writes as well? Please advice.

+3


source to share


3 answers


An Detail

object property Elmah.Error

that is then processed by the class ErrorLog

is built from an ToString()

exception method .

// Elmah.Error
public Error(Exception e, HttpContext context)
{
    // snip
    this._detail = e.ToString(); // here
    // snip

      



Add your data to the ToString method override in MyCustomException

order to see it in Elmah.

+1


source


At the moment you have a question with most stars on the ELMAH bug tracker:



https://code.google.com/p/elmah/issues/detail?id=162

+1


source


@samy might be more correct, but I also found another possible option that works for my situation. I am using elmah in a webapi2 project where users are anonymous and in one particular controller I want to record some request context from the viewmodel (in my case an email address, but I could write more data) and I want to be able to bind errors to by email, so I can determine after the error if the same user was able to submit the order successfully.

In my controller, I make multiple database calls in a transaction and then submit the order to paypal, all in a try / catch block. In catch, I create a new exception instance with the message containing the email and set the innerException property to the thrown exception and throw a new exception.

I know it is possible to lose some information about the stack trace, I tested this in my context and the stack trace seems to be supported, but exceptions happen inside the controller because there are not many layers for this particular controller and application. If anyone has a similar situation, this method may be the quickest and easiest.

catch (Exception ex)
{
    Exception newException = new Exception(viewModel.ContactEmail, ex);
    throw newException;
}

      

It assumes you have an exclusion filter like below (for webapi) and the filter is registered as global in global.asax.

public class LogExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        if (HttpContext.Current != null)
        {
            ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception);
        }
    }
}

      

+1


source







All Articles