Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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'

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));

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

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
            );
    }
}

Well, having spent the day working on this, I thought I'd share my solution. Very similar to Myster's solution above, the difference being I modify the error object used by elmah instead of the request server variables, like so:

    public static void LogError(Exception e, IDictionary<string, string> customFields = null)
    {
        var logger = ErrorLog.GetDefault(HttpContext.Current);

        customFields = customFields ?? new Dictionary<string, string>();

        // Used to tell elmah not to log this twice (See global.asax)
        e.Data.Add("Logged", true);

        var error = new Error(e, HttpContext.Current);

        customFields.ForEach(pair => error.ServerVariables.Add(pair.Key, pair.Value));

        logger.Log(error);
    }

Then, depending on your application to call logger.LogError:

mvc add a custom error filter (http://maheshde.blogspot.ca/2012/09/error-handing-with-mvc-using-custom.html)

webforms override Application_Error in global.asax

Then, just one final step in the global.asax, dismiss any errors that have already been logged:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Exception.Data.Contains("Logged"))
    {
        if (e.Exception.Data["Logged"].Equals(true)) e.Dismiss();
    }
}

Since it is open source, you could just grab the project and customize Elmah for your own purposes:

http://code.google.com/p/elmah/

If you are using MSSQL

  • take a look at SqlErrorLog.cs

  • SQLServer.sql to generate your own database

I'm doing the same, but I need to add an extra randomly generated "error code" column and I'll be using exception data to track all the custom, session, form, cookie information that would normally be in the uber large xml field. I may change this to JSON, not sure yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top