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.