Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ELMAH display data in exception data dictionary

Tags:

elmah

When using ELMAH (which is brilliant) is it possible to view extra information that you have added to an exception.

E.g.

Exception ex = new Exception("New exception to use ErrorSignal functionality");
ex.Data.Add("ExtraInfo", "Here is some extra information i would like to be displayed.");
ErrorSignal.FromCurrentContext().Raise(ex);    

When I view the exception from elmah.axd it doesn’t seem to show the “ExtraInfo” key and value information, just the exception string.

like image 531
Rob Avatar asked Aug 03 '09 16:08

Rob


5 Answers

My solution was to add the information to the Server Variables collection like so.

var context = HttpContext.Current;
context.Request.ServerVariables["ERROR_CALLING_WEBSERVICE_URI"] = uri;
Elmah.ErrorLog.GetDefault(context).Log(new Error(e, context))

Yes, I Think this is a hack.
For small amounts of additional data consider encapsulating the error as suggested by @Roma
However this method is especially useful where you have too much information to put into an 'encapsulated error message'

like image 51
Myster Avatar answered Nov 08 '22 21:11

Myster


Elmah uses ToString() method to get exception details. Just override your custom exception ToString() method and this way it will work:

sample exception in elmah

My exception class:

public class MyCustomException : Exception
{
    public string CustomProperty { get; set; }

    public MyCustomException(string message, string customProperty): base(message)
    {
        this.CustomProperty = customProperty;
    }

    public override string ToString()
    {
        var result = base.ToString();
        var propertyData = String.Format("CustomProperty: {0}", this.CustomProperty);

        return result.Insert(
            result.IndexOf(Environment.NewLine),
            Environment.NewLine + propertyData
            );
    }
}
like image 39
frizik Avatar answered Nov 08 '22 20:11

frizik


The simple way to go around, is to encapsulate the error. The outer error will contain you custom message.

string msg = "my custom error message";

ErrorSignal.FromCurrentContext().Raise(
               new Elmah.ApplicationException(msg,ex));
like image 26
Roma Avatar answered Nov 08 '22 20:11

Roma


No, it is not possible to view the extra information with the current 1.x releases.

like image 41
Atif Aziz Avatar answered Nov 08 '22 19:11

Atif Aziz


Ok, so I have been waiting for this to be included for too long now and decided to make a own fork of it and include the feature myself.

You can find it here: https://github.com/boena/elmah-with-custom-data

like image 31
boena Avatar answered Nov 08 '22 21:11

boena