Elmah: Exception.Data dictionary NOT logged.

What steps will reproduce the problem?

  1. Log any exception with some data in Exception.Data dictionary.
  2. go to UI (elmah.axd)
  3. search through Details and XML.

What is the expected output? What do you see instead?

  • get Exception.Data logged. It’s NOT logged.

Originally reported on Google Code with ID 162

Reported by Alexander.Chervony on 2010-03-17 18:38:21

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 1
  • Comments: 38 (31 by maintainers)

Most upvoted comments

@arc95 In regards to the example found on docs.elmah.io, that is possible with elmah.io only. elmah.io and ELMAH are not the same thing. elmah.io were originally a remote error logger for ELMAH, but today a full error management system for all major .NET web and logging frameworks. I totally understand why you are confused by the naming. We are currently in the process of changing our name to something not related to ELMAH (the open source project which is awesome by the way 😄).

Came across this possible solution: https://stackoverflow.com/a/22647084/177416. It uses the error.ServerVariables collection. What do you think?

It mixes server variables with arbitrary custom data and if you’re happy to run with that then it’s your call.

where can I find this project (its link is broken)? https://github.com/boena/elmah-with-custom-data

Best to ask @boena about that.

Thanks, @atifaziz! Came across this possible solution: https://stackoverflow.com/a/22647084/177416. It uses the error.ServerVariables collection. What do you think?

Also, where can I find this project (its link is broken)? https://github.com/boena/elmah-with-custom-data

@arc95 Your best bet is to customize the SqlErrorLog via derivation and make it aware of the data dictionary stored in an Exception. You would have to customize the SQL database objects to pass and log, as well as later retrieve, the additional information to and from an additional column. You would then override the Log, GetError & GetErrors (including async versions) methods of SqlErrorLog. The Error object passed to the Log method has a property called Exception using which you can retrieve the exception from which the error was created and get at its data dictionary for logging. Retrieval is trickier because none of the built-in views and other infrastructure will be aware of the additional information. In fact, the Error class is sealed so how do you add and return any extra information? For that, you could leverage Conditional​Weak​Table<,> to have one class supplement another. Take a look at LoggedException from the ELMAH Bootstrapper project for an example of how to do this.

maybe you could add a event OnLogging(Error error) on ErrorLogModule where the users can customize the error? This would make it possible for users to customize the error message, for example to log the Data dictionary and format it anyway they like. The event could look a like this; public Action<Error> OnLogging(Error error);

a usage for of the event for logging the Data dictionary could be implemented:

public void ErrorLog_OnLogging(Error error)
{
    var myMessage = new StringBuilder();
    myMessage.AppendLine(error.Message);
    foreach(var kvp in error.Exception.Data)
    {
        myMessage.AppendLine($"{kvp.Key} = {kvp.Data}";
    }
    error.Message = myMessage.ToString();
}