You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by "Hart, Leo" <Le...@FMR.COM> on 2005/09/01 21:13:00 UTC
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/ExceptionUnhandled.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_btnCauseException
_Click(Object sender, EventArgs e)
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.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