27

I just installed Elmah (https://code.google.com/p/elmah/) for my ASP.NET application. Is it possible to log a message without creating an Exception first?

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

So is it possible to do:

ErrorSignal.FromCurrentContext().log("Hello I am testing Elmah");
GuyB
  • 101
  • 7
user603007
  • 11,416
  • 39
  • 104
  • 168

3 Answers3

41

Yes, you can use ErrorSignal without throwing an exception.

ErrorSignal.FromCurrentContext().Raise(new NotSupportedException());

For the custom message, you can create a custom exception.

var customEx = new Exception("Hello I am testing Elmah", new NotSupportedException()); 
ErrorSignal.FromCurrentContext().Raise(customEx);
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
11

Try this

Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Your message"));
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
5

I know this is an old question, but if you don't want to create an exception you can also use

var error = new Error
{
   Source = eventType.ToString(),
   Type = $"Trace-{eventType}",
   Message = message,
   Time = DateTime.UtcNow
};

ErrorLog.GetDefault(HttpContext.Current).Log(error);

as shown in this answer.

madannes
  • 523
  • 1
  • 8
  • 14