Return-Path: Delivered-To: apmail-logging-log4net-user-archive@www.apache.org Received: (qmail 67988 invoked from network); 1 Sep 2005 20:47:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 1 Sep 2005 20:47:57 -0000 Received: (qmail 78912 invoked by uid 500); 1 Sep 2005 20:47:56 -0000 Delivered-To: apmail-logging-log4net-user-archive@logging.apache.org Received: (qmail 78888 invoked by uid 500); 1 Sep 2005 20:47:56 -0000 Mailing-List: contact log4net-user-help@logging.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Log4NET User" List-Id: Delivered-To: mailing list log4net-user@logging.apache.org Received: (qmail 78875 invoked by uid 99); 1 Sep 2005 20:47:56 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Sep 2005 13:47:55 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of nicko@neoworks.com designates 80.168.17.114 as permitted sender) Received: from [80.168.17.114] (HELO kronos.neoworks.co.uk) (80.168.17.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Sep 2005 13:48:10 -0700 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-MimeOLE: Produced By Microsoft Exchange V6.5.6944.0 Subject: RE: Logging Exceptions Through a Base Page Date: Thu, 1 Sep 2005 21:49:56 +0100 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Logging Exceptions Through a Base Page Thread-Index: AcWvKSuN8se3+7PuREKEy06Dh71a/gADRa1w From: "Nicko Cadell" To: "Log4NET User" X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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]=20 > Sent: 01 September 2005 20:13 > To: log4net-user@logging.apache.org > Subject: Logging Exceptions Through a Base Page >=20 > Hello, >=20 > I suspect this will be an easy one for you experts out there,=20 > but I haven't been able to figure it out at this point. >=20 > I have a web application utilizing Log4Net. I am trying to=20 > figure out the best way to capture and log unhandled=20 > exceptions in my app. I have two major requirements: >=20 > 1: I don't want to have to write the same repetitive code in=20 > each new page. > 2: I want the Logger of my log entry to contain the name of=20 > the Page that triggered the error. >=20 >=20 > So here's what I did: >=20 > I have a class called ReferenceWebApp.Web.BasePage that inherits > System.Web.UI.Page: >=20 > #Region " Options " >=20 > Option Explicit On=20 > Option Strict On >=20 > #End Region >=20 > #Region " Imports " >=20 > Imports log4net > Imports log4net.Config >=20 > Imports System.Web > Imports System.Web.SessionState > Imports System.IO >=20 > #End Region >=20 > Public Class BasePage > Inherits System.Web.UI.Page >=20 > #Region " Member Variables " >=20 > Protected m_log As ILog =3D > LogManager.GetLogger(GetType(BasePage)) >=20 > #End Region >=20 > Protected Overridable Sub Page_Error(ByVal=20 > sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error > Me.m_log.Error("An unexpected exception has > occurred: ", Server.GetLastError) > Server.ClearError() > =09 > Server.Transfer("/ReferenceWebApp/ExceptionPages/ExceptionUnha > ndled.aspx > ") > End Sub >=20 > End Class >=20 > This class acts as a base page class from which all other=20 > pages in the application will inherit. =20 >=20 > I also have a class called=20 > ReferenceWebApp.Web.SimpleLoggingPage1 that inherits from my=20 > base class: >=20 > #Region " Options " >=20 > Option Explicit On=20 > Option Strict On >=20 > #End Region >=20 > #Region " Imports " >=20 > Imports log4net >=20 > Imports ReferenceWebApp.Data.Dtos > Imports ReferenceWebApp.Data.Persistence.Daos >=20 > #End Region >=20 > Namespace LoggingUsage >=20 > Public Class SimpleLoggingPage1 > Inherits BasePage >=20 > #Region " Web Form Designer Generated Code " >=20 > 'This call is required by the Web Form Designer. > > Private Sub InitializeComponent() >=20 > End Sub >=20 > 'NOTE: The following placeholder=20 > declaration is required by the Web Form Designer. > 'Do not delete or move it. > Private designerPlaceholderDeclaration=20 > As System.Object >=20 > Private Sub Page_Init(ByVal sender As=20 > System.Object, ByVal e As System.EventArgs) Handles MyBase.Init > 'CODEGEN: This method call is=20 > required by the Web Form Designer > 'Do not modify it using the code editor. > InitializeComponent() > End Sub >=20 > #End Region >=20 > Protected WithEvents=20 > wc_btnCauseException As Button >=20 > #Region " Member Variables " >=20 > Protected Shadows m_log As ILog =3D > LogManager.GetLogger(GetType(SimpleLoggingPage1)) >=20 > #End Region >=20 > Private Sub Page_Load(ByVal sender As=20 > System.Object, ByVal e As System.EventArgs) Handles MyBase.Load > Me.m_log.Info("Info log.") >=20 > Me.m_log.Debug("Debug log 1.") > Me.m_log.Debug("Debug log 2.") > End Sub >=20 > Private Sub=20 > wc_btnCauseException_Click(ByVal sender As System.Object,=20 > ByVal e As System.EventArgs) Handles wc_btnCauseException.Click > Throw New Exception("Ack! An > exception!") > End Sub >=20 > End Class >=20 > End Namespace >=20 > This works almost as I want it to. Whenever an exception=20 > occurs, BasePage.Page_Error gets called and a log entry is made: >=20 > ERROR | 2005-09-01 14:52:44, 630 | 4244 |=20 > ReferenceWebApp.Web.BasePage | Page_Error | An unexpected=20 > exception has > occurred:=20 > 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() >=20 > The only problem is that the logger being used is=20 > ReferenceWebApp.Web.BasePage and not=20 > ReferenceWebApp.Web.SimpleLoggingPage1. I understand WHY=20 > this is occuring, but I was hoping you guys might have some=20 > idea how to get around this. I want to be able to filter on=20 > page or namespace and send certain error messages to one=20 > person and other messages to another. >=20 >=20 > Thanks, > Leo Hart >=20