Logging errors

Topics: ASP.NET 2.0
Feb 7, 2012 at 12:26 PM

Hi guys, I use ELMAH to log all unhanded errors in blog engine, but is there a way to set it up so blog engine uses ELMAH to log handled exceptions aswel?

 

Since  blog engine is such a big project, it would be very time consuming to go in each file and check for exception handling and add the ELMAH line there.

 

Thanks.

Coordinator
Feb 7, 2012 at 7:05 PM

Not sure if ELMAH has API for it, but based on this overview you can specify database as provider which will create a table holding all ELMAH errors. If so, you could extend BlogEngine's error logger to use this same table for built-in errors, so you'll get them all from ELMAH UI.

Feb 7, 2012 at 7:23 PM

Yes it does have an API, its a one line command but you have to use it each time you catch an exception. I already created the database for elmah, how would I go about extending this error logger?

Feb 7, 2012 at 9:10 PM

GateKeeper,

Logging in BE.NET is enabled as an Extension.  Source for it is located in /app_code/extensions/logger.cs.  You may want to start by looking at the OnLog() method.

Good luck,
Dave

Feb 7, 2012 at 11:37 PM

I looked a bit at that file. From what I understand, it receives the error message and appends it to end of the file. However ELMAH requires the actual exception to work. As far as I can tell only the error message is passed on to this class.

 

Maybe if someone has any more knowledge of how the logger class works, I can modify I to work with ELMAH.

 

Some questions:

 

-What does the logger class log, handled or unhandled exceptions?

-How is it called? Is it possible to make it pass the actual exception?

Coordinator
Feb 8, 2012 at 6:15 PM
Edited Feb 8, 2012 at 9:29 PM
In the logger extension, after line:

var
logMsg = (string)sender;

Add this to create exception from error message and pass it to ELMAH:

var ex = new ApplicationException(logMsg);
Elmah
.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex))
;
return;

I think this is all you need.
Feb 8, 2012 at 8:24 PM
Edited Feb 8, 2012 at 8:29 PM
rtur wrote:
In the logger extension, after line:

var
logMsg = (string)sender;

Add this to create exception from error message and pass it to ELMAH:

var ex = new ApplicationException(logMsg);
Elmah
.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex))
;
break;

I think this is all you need.

Ok, I will test this.

 

What I need to know is, does this apply to handled or unhandled exceptions?

 

And do I need to enable something to get the logging to start?

Coordinator
Feb 8, 2012 at 9:28 PM

Handled. Logging must be enabled in admin/settings/advanced.

Feb 8, 2012 at 10:26 PM
rtur wrote:

Handled. Logging must be enabled in admin/settings/advanced.

 

You sure it applies to handled?

 

This is what it says in the description:

 

"<label for="ctl00_cphAdmin_cbEnableErrorLogging">Enable error logging</label> (Unhandled errors will be logged to any extension/component subscribed to log notifications.)"

Coordinator
Feb 9, 2012 at 2:50 AM

By unhandled I meant those showing yellow screen, they usually won't get into logs. This is where ELMAH is useful. Logger will log anything you tell. Can write extension or add code to custom theme that will log referrer IP if it is Friday - and it'll go into log. Most of what you'll see there though is a result of standard try/catch with exception description and source method logged.

Feb 9, 2012 at 10:05 AM
rtur wrote:

By unhandled I meant those showing yellow screen, they usually won't get into logs. This is where ELMAH is useful. Logger will log anything you tell. Can write extension or add code to custom theme that will log referrer IP if it is Friday - and it'll go into log. Most of what you'll see there though is a result of standard try/catch with exception description and source method logged.

Yes that is exactly what I want, a log of any errors caught in the blog engine workings.

 

However, I tried to test it by giving false information int he "STMP" field for the email, and then click "Test". This is a handled exception.

 

The error was still logged regardless of having logging turned on.

Feb 13, 2012 at 12:46 PM

Bump

Feb 15, 2012 at 9:10 PM

Bump, no way to test if it is working?