Actually, I have to take that back.  This does get me closer, but not
there 100%.

Now what I'm getting for a log entry is:

        ERROR | 2005-09-02 13:45:25, 446 | 4244 |
ASP.SimpleLoggingPage1_aspx | Page_Error | An unexpected exception has
occurred...

So it is giving me the correct class, but because of the way ASP.NET
renames its pages during JIT compilation, I get
"ASP.SimpleLoggingPage1_aspx" for a logger instead of
"ReferenceWebApp.Web.SimpleLoggingPage1".  I really want the latter
because I'm going to need to filter on the namespace level (not the page
level) in my email appenders.  This is why I was specifying:
        
        LogManager.GetLogger(GetType(SimpleLoggingPage1))

in my pages vs:

        LogManager.GetLogger(me.GetType())

which I normally do with the classes I create.

Any suggestions on how to get past this ASP.NET "feature"?

Thanks again,
Leo




-----Original Message-----
From: Hart, Leo 
Sent: Friday, September 02, 2005 1:32 PM
To: Log4NET User
Subject: RE: Logging Exceptions Through a Base Page


That did the trick!  Thanks a lot Nicko!

-----Original Message-----
From: Nicko Cadell [mailto:[EMAIL PROTECTED] 
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:[EMAIL PROTECTED]
> Sent: 01 September 2005 20:13
> To: [email protected]
> 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
> 

Reply via email to