New features Please use http://kentico.uservoice.com as the primary place to submit your suggestions and allow others to vote for your ideas!
Version 4.x > New features > Ehancement to Exception Logging View modes: 
User avatar
Member
Member
Steve-Appetere - 7/13/2009 8:12:00 AM
   
Ehancement to Exception Logging
Hello

The usefulness of the exception logging Kentico provides could be dramatically improved by a simple change to the Application_Error handler in Global.asax.

When the event is logged to the database or sent to email, Kentico currently uses:

ex.Message and
ex.StackTrace

where ex is the Exception object from GetLastError().

However, the useful information that actually describes the error is to be found in:

ex.InnerException.Message and
ex.InnerException.StackTrack

So as an example, with an artificial casting problem, Kentico would currently send an email with:


EventID: 3256

EventType: E

EventTime: 13/07/2009 13:29:17

Source: Application_Error

EventCode: EXCEPTION

UserID: 65

UserName: public

IPAddress: 127.0.0.1

EventDescription: EXCEPTION MESSAGE: Exception of type 'System.Web.HttpUnhandledException' was thrown.; STACK TRACE: at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.maru_cmspages_error404_aspx.ProcessRequest(HttpContext context) in c:\Users\Steve Moss\AppData\Local\Temp\Temporary ASP.NET Files\maru\60d554ab\aa14e8fb\App_Web_i-8j84wt.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Site: MARU Forum

EventUrl: http://localhost:50775/MARU/MARU/CMSPages/error404.aspx

EventMachineName: TOSHIBA



Which tells you almost nothing about what the problem is. With the simple change proposed, the same exception is now reported as:


EventID: 3261

EventType: E

EventTime: 13/07/2009 13:54:06

Source: Application_Error

EventCode: EXCEPTION

UserID: 65

UserName: public

IPAddress: 127.0.0.1

EventDescription: EXCEPTION MESSAGE: Unable to cast object of type 'ASP.maru_cmspages_error404_aspx' to type 'CMS.UIControls.TemplatePage'.; STACK TRACE: at CMS.UIControls.TemplateMasterPage.get_TemplatePage()
at CMS.UIControls.TemplateMasterPage.set_PageManager(IPageManager value)
at CMSTemplates_MARU_Root.CreateChildControls() in c:\Users\Steve Moss\Documents\Kentico\MARU\CMSTemplates\MARU\Root.master.cs:line 18
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Site: MARU Forum

EventUrl: http://localhost:50775/MARU/maru/cmspages/error404.aspx

EventMachineName: TOSHIBA



Now there is actually something useful in the exception message!

If you want to make the change yourself, open /App_Code/Global.asax.cs and around line 203 the event should be logged using the line:


eProvider.LogEvent("E", DateTime.Now, "Application_Error", "EXCEPTION", CMSContext.CurrentUser != null ? CMSContext.CurrentUser.UserID : 0, CMSContext.CurrentUser != null ? CMSContext.CurrentUser.UserName : "", CMSContext.CurrentDocument != null ? CMSContext.CurrentDocument.NodeID : 0, CMSContext.CurrentDocument != null ? CMSContext.CurrentDocument.DocumentName : "", HTTPHelper.GetUserHostAddress(), "EXCEPTION MESSAGE: " + ex.InnerException.Message + "; STACK TRACE: " + ex.InnerException.StackTrace, CMSContext.CurrentSite != null ? CMSContext.CurrentSite.SiteID : 0, HTTPHelper.GetAbsoluteUri());


There is a section following to send the error by email, which you could change as well. However, this looks like a legacy function from an earlier version which isn't used in Kentico 4.x, as the email is sent by another means in 4.x.

User avatar
Member
Member
Steve-Appetere - 7/13/2009 9:49:31 AM
   
RE:Ehancement to Exception Logging
I've just found a simpler & possibly more robust way of making the improvement above.

If you add "GetBaseException()" to the following line (around line 200 in Application_Error):


Exception ex = HttpContext.Current.Server.GetLastError().GetBaseException();

then you will automatically be using the base exception & you don't need to look for the InnerException.

This is probably more robust as there might be a situation when there is no InnerException (although when an event bubbles up from the page, there always will be), but with GetBaseException this is taken care of either way.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 7/27/2009 3:24:02 AM
   
RE:Ehancement to Exception Logging
Hi,

Thank you for your suggestions. We have already made some improvements to the event logging for upcoming version 4.1. They are made in similar way you suggested. I hope it will meet your needs as well.

Best Regards,
Juraj Ondrus