Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log custom exception with additional Data property to Elmah?

I have a custom exception, where I have overriden the Data property 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 gets logged to elmah but the Data is not logged.

What changes do I have to make so that the Data is also logged to elmah ? Please advice.

like image 965
Yasser Shaikh Avatar asked Oct 30 '25 15:10

Yasser Shaikh


2 Answers

The Detail property of the Elmah.Error object - which is then processed by an ErrorLog class - is built from the ToString() method of the exception.

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

Add your data to an override of the ToString method in the MyCustomException to see it in Elmah.

like image 195
samy Avatar answered Nov 02 '25 16:11

samy


Your question is currently the issue with most stars on the ELMAH issue tracker:

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

like image 32
ThomasArdal Avatar answered Nov 02 '25 16:11

ThomasArdal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!