Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 0 Stories | 874 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

I found that there were a couple of things required to get this working - over and above the samples shown here

  1. To initialize Log4Net and to tell it to load it's configuration from the web.config file add the following line to the Application_Start method within Global.aspx:
        log4net.Config.XmlConfigurator.Configure();
  2. To make sure all unhandled exceptions are logged add the following line to the Application_Error method within Global.aspx:
        Log.Fatal("An uncaught exception occurred", this.Server.GetLastError());
    For this to work you'll need to define the Log member eg:
        private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    will also need to add the following so you can access the Log4Net and reflection classes:
        using log4net;
        using System.Reflection;
  3. Add code in your app to write logs - this is done the same way as step 2 (define a Log member and use one of it's log writing methods eg Log.Info(), Log.Warn(), Log.Error() and Log.Fatal().   These writing methods take a string and/or and exception as input parameters.
  4. Register your app with the event log.  You can do this either with an installer or manually (ie code it using the System.Diagnostics.EventLog.CreateEventSource() method). 
  5. Add nodes within the app config file to configure Log4Net.  Note that params LogName and ApplicationName are optional (although I couldnt get it to work without at least specifying the ApplicationName - as that's what you register with the eventlog) The following is an example from a web.config (note that both the configSections and the log4net section are directly beneath the root configuration node - ie not within system.web):

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"
/>
</configSections
>
<log4net debug
="false">
    <appender name="EventLogAppender" type
="log4net.Appender.EventLogAppender">
        <param name="LogName" value="MyLog"
/>
       
<param name="ApplicationName" value="MyApp"
/>
       
<layout type
="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"
/>
        </layout
>
   
</appender
>
    <root
>
        <priority value="DEBUG"
/>
        <appender-ref ref="EventLogAppender"
/>
   
</root
>
</log4net>

Additional Resources:

  • Tom Gilki has written a good article explaining basic use of Log4Net found here.
  • If you are having security problems writing to the event log have a look at MS's solution here or my solution here.
  • Scott Colestock has a great article documenting how to use Log4Net with Biztalk here (a lot of useful info here even if you're not using Biztalk).

HTH

Tim

posted on Thursday, April 13, 2006 7:48 AM