12

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.

Rob
  • 1,688
  • 4
  • 30
  • 43

7 Answers7

20

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'

Myster
  • 17,704
  • 13
  • 64
  • 93
  • 3
    This method works before you raise an error, but you can also do it via the global.asax for all errors in `void ErrorLog_Filtering(object sender, Elmah.ExceptionFilterEventArgs e)` (before the Error object is created) and cast `e.Context as HttpContext` – Dave Transom Oct 16 '12 at 07:25
  • 2
    Very good answer but this works only inside the Web app and not in the Domain of the application as you need to access the HTTP Context. – Gabriel Feb 24 '13 at 13:02
7

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));
Roma
  • 71
  • 1
  • 1
2

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

Atif Aziz
  • 36,108
  • 16
  • 64
  • 74
2

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

Fatal
  • 3,338
  • 3
  • 19
  • 15
boena
  • 187
  • 1
  • 7
  • Do you have the code for it? The link that you have given above does not work. – Ajit Goel Feb 18 '13 at 04:43
  • Oh snap, sorry. I have renamed it since then I see now. Hopefully you found it by browsing my repos otherwise you can find it [link](https://github.com/boena/elmah-with-custom-data) and if you also wan't the Log Analyzer Desktop app displaying the custom data it's here [link](https://github.com/boena/elmah-log-analyzer-with-custom-data) – boena Apr 06 '13 at 16:56
  • i've fixed the broken link – Fatal Jan 05 '14 at 11:44
  • @boena, the link is broken again. – Alex Jun 12 '17 at 20:53
2

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
            );
    }
}
frizik
  • 1,766
  • 1
  • 13
  • 17
1

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();
    }
}
Mitchell Lee
  • 456
  • 3
  • 14
-1

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.

sonjz
  • 4,870
  • 3
  • 42
  • 60