12

I'm a bit confused at how to add a message to an error logged programatically with ELMAH.

eg:

public ActionResult DoSomething(int id)
{
    try { ... }

    catch (Exception e)
    {
        // I want to include the 'id' param value here, and maybe some
        // other stuff, but how?
        ErrorSignal.FromCurrentContext().Raise(e);
    }
}

It seems all Elmah can do is log the raw exception, how can I also log my own debug info?

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
  • 1
    curious why Elmah doesn't have an overload for Raise for additional messages. contextual information is vital when trying to fix certain errors – Simon_Weaver Feb 08 '13 at 11:14

2 Answers2

18

You can throw a new Exception setting the original as the inner exception and ELMAH will log the messages for both:

catch(Exception e)
{
    Exception ex = new Exception("ID = 1", e);
    ErrorSignal.FromCurrentContext().Raise(ex);
}

will show

System.Exception: ID = 1 ---> System.NullReferenceException: Object reference not set to an instance of an object.
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • you should use Exception.Data, but by default elmah does not have this functionality. I added to a custom version I use. I really should post the code up somewhere. I know this is old but I think this is not 100% correct. Yes you can use this for small details, but if you want a list of data that's what the Exception.Data was made for. For example when an SQLException is thrown you get a lot of details. I also added this. It's not really hard to do yourself. Also when working with linq2sql you can also get a lot of details. – pqsk Feb 05 '14 at 17:18
3

I found that I can also do something like:

Elmah.ErrorSignal.FromCurrentContext().Raise(new NotImplementedException("class      FbCallback.Page_Load() Request.Url= " + Request.Url));

To log my own messages. Then in when I browse to

http://localhost:5050/elmah.axd

I see my messages as type NotImplementedException. Not very pretty but works.

Mark
  • 3,123
  • 4
  • 20
  • 31
sheir
  • 393
  • 4
  • 9