How to Use Elmah to Manually Log Errors

How to use ELMAH to manually log errors

Direct log writing method, working since ELMAH 1.0:

try 
{
some code
}
catch(Exception ex)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
}

ELMAH 1.2 introduces a more flexible API:

try 
{
some code
}
catch(Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}

There is a difference between the two solutions:

  • Raise method applies ELMAH filtering rules to the exception. Log method does not.
  • Raise is subscription based and is able to log one exception into the several loggers.

Elmah error logging, can I just log a message?

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

ELMAH Log handled exceptions

Of course it's only getting triggered for unhandled exceptions. Those filters only run for errors that are allowed to bubble up (meaning, unhandled). If you want to log a handled exception, then you need to put that ErrorSignal.FromCurrentContext().Raise() logic inside of your catch block.

catch (Exception e)
{
ModelState.AddModelError("", "Something unexpected happened, please try again.");
ErrorSignal.FromCurrentContext().Raise(e);
return View(model);
}

If you find yourself doing this a lot, then I suggest you switch off using Elmah. Elmah isn't a general logging framework, it's geared for unhandled errors. It would be better to use a logging system such as Serilog or Nlog and then have those log to a specialized system such as Seq.

Manually logged ELMAH errors aren't sending email

I spoke too soon.

This doesn't send email:

Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Elmah.Error(ex));

But this does:

Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

Manual Elmah Logging dosnt work properly in MVC

I have also seen this problem when calling ELMAH logger without any HttpContext in class library projects.

I used the old manual way to get around this:

Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));

Log an exception without throwing it when using ELMAH

Your code logs the thrown exception (ex) in ELMAH and returns successful to the caller. In other words, Raise does not throw an exception.

If your catch block re-throws ex or throw a new exception, both ex and the new exception are logged to ELMAH and a status code 500 is returned to the caller.

Manual Elmah logging

I understand why filter ignores error - because when using Elmah.ErrorSignal.FromCurrentContext().Raise() we are "emulate" process of getting error and this errors fall under the filter.
Instead of this method we should use Elmah logger method to add error to log:

Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log()

This method will add error to log and filter below will ignore all others:

<errorFilter>
<test>
<regex binding="Context.Request.Url" pattern="." />
</test>
</errorFilter>

After all this I have one question - name tag inside <errorFilter> can only be test?



Related Topics



Leave a reply



Submit