logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hart, Leo" <Leo.H...@FMR.COM>
Subject RE: Logging Exceptions Through a Base Page
Date Fri, 02 Sep 2005 17:32:00 GMT
That did the trick!  Thanks a lot Nicko!

-----Original Message-----
From: Nicko Cadell [mailto:nicko@neoworks.com] 
Sent: Thursday, September 01, 2005 4:50 PM
To: Log4NET User
Subject: RE: Logging Exceptions Through a Base Page


Leo,

In your Page_Error don't use the m_log for the Base class, but instead
lookup the logger for the actual instance type of Me.

Not being a VB head I don't know exactly the syntax for doing that, but
if I had to guess it would be:


LogManager.GetLogger(Me.GetType()).Error("An unexpected exception ...")


Cheers,
Nicko

> -----Original Message-----
> From: Hart, Leo [mailto:Leo.Hart@FMR.COM]
> Sent: 01 September 2005 20:13
> To: log4net-user@logging.apache.org
> Subject: Logging Exceptions Through a Base Page
> 
> Hello,
> 
> I suspect this will be an easy one for you experts out there,
> but I haven't been able to figure it out at this point.
> 
> I have a web application utilizing Log4Net.  I am trying to
> figure out the best way to capture and log unhandled 
> exceptions in my app.  I have two major requirements:
> 
> 1:  I don't want to have to write the same repetitive code in
> each new page.
> 2:  I want the Logger of my log entry to contain the name of 
> the Page that triggered the error.
> 
> 
> So here's what I did:
> 
> I have a class called ReferenceWebApp.Web.BasePage that inherits
> System.Web.UI.Page:
> 
> 	#Region " Options "
> 
> 	Option Explicit On 
> 	Option Strict On
> 
> 	#End Region
> 
> 	#Region " Imports "
> 
> 	Imports log4net
> 	Imports log4net.Config
> 
> 	Imports System.Web
> 	Imports System.Web.SessionState
> 	Imports System.IO
> 
> 	#End Region
> 
> 	Public Class BasePage
> 		Inherits System.Web.UI.Page
> 
> 	#Region " Member Variables "
> 
> 		Protected m_log As ILog =
> LogManager.GetLogger(GetType(BasePage))
> 
> 	#End Region
> 
> 		Protected Overridable Sub Page_Error(ByVal
> sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error
> 			Me.m_log.Error("An unexpected exception has
> occurred: ", Server.GetLastError)
> 			Server.ClearError()
> 	
> Server.Transfer("/ReferenceWebApp/ExceptionPages/ExceptionUnha
> ndled.aspx
> ")
> 		End Sub
> 
> 	End Class
> 
> This class acts as a base page class from which all other
> pages in the application will inherit.  
> 
> I also have a class called
> ReferenceWebApp.Web.SimpleLoggingPage1 that inherits from my 
> base class:
> 
> 	#Region " Options "
> 
> 	Option Explicit On 
> 	Option Strict On
> 
> 	#End Region
> 
> 	#Region " Imports "
> 
> 	Imports log4net
> 
> 	Imports ReferenceWebApp.Data.Dtos
> 	Imports ReferenceWebApp.Data.Persistence.Daos
> 
> 	#End Region
> 
> 	Namespace LoggingUsage
> 
> 		Public Class SimpleLoggingPage1
> 			Inherits BasePage
> 
> 	#Region " Web Form Designer Generated Code "
> 
> 			'This call is required by the Web Form Designer.
> 			<System.Diagnostics.DebuggerStepThrough()>
> Private Sub InitializeComponent()
> 
> 			End Sub
> 
> 			'NOTE: The following placeholder
> declaration is required by the Web Form Designer.
> 			'Do not delete or move it.
> 			Private designerPlaceholderDeclaration 
> As System.Object
> 
> 			Private Sub Page_Init(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
> 				'CODEGEN: This method call is 
> required by the Web Form Designer
> 				'Do not modify it using the code editor.
> 				InitializeComponent()
> 			End Sub
> 
> 	#End Region
> 
> 			Protected WithEvents
> wc_btnCauseException As Button
> 
> 	#Region " Member Variables "
> 
> 			Protected Shadows m_log As ILog =
> LogManager.GetLogger(GetType(SimpleLoggingPage1))
> 
> 	#End Region
> 
> 			Private Sub Page_Load(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
> 				Me.m_log.Info("Info log.")
> 
> 				Me.m_log.Debug("Debug log 1.")
> 				Me.m_log.Debug("Debug log 2.")
> 			End Sub
> 
> 			Private Sub
> wc_btnCauseException_Click(ByVal sender As System.Object, 
> ByVal e As System.EventArgs) Handles wc_btnCauseException.Click
> 				Throw New Exception("Ack!  An
> exception!")
> 			End Sub
> 
> 		End Class
> 
> 	End Namespace
> 
> This works almost as I want it to.  Whenever an exception
> occurs, BasePage.Page_Error gets called and a log entry is made:
> 
> 	ERROR | 2005-09-01 14:52:44, 630 | 4244 |
> ReferenceWebApp.Web.BasePage | Page_Error | An unexpected 
> exception has
> occurred: 
> 	Exception: System.Exception
> 	Message: Ack!  An exception!
> 	Source: ReferenceWebApp.Web
> 	   at
> ReferenceWebApp.Web.LoggingUsage.SimpleLoggingPage1.wc_btnCaus
> eException
> _Click(Object sender, EventArgs e)
> 	   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
> 	   at
> System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventH
> andler.Rai
> sePostBackEvent(String eventArgument)
> 	   at
> System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
> sourceControl, String eventArgument)
> 	   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
> postData)
> 	   at System.Web.UI.Page.ProcessRequestMain()
> 
> The only problem is that the logger being used is
> ReferenceWebApp.Web.BasePage and not 
> ReferenceWebApp.Web.SimpleLoggingPage1.  I understand WHY 
> this is occuring, but I was hoping you guys might have some 
> idea how to get around this.  I want to be able to filter on 
> page or namespace and send certain error messages to one 
> person and other messages to another.
> 
> 
> Thanks,
> Leo Hart
> 

Mime
View raw message