Tim Huffam

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

  Home  |   Contact  |   Syndication    |   Login
  153 Posts | 0 Stories | 2747 Comments | 653 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

Feedback

# re: Log4Net - writing to windows event log with ASP.NET 8/3/2009 4:19 AM Nutan
Hi,

Can anyone please guide me how can Register my app with the event log as mentioned in Step# 4.

Thank you!

# re: Log4Net - writing to windows event log with ASP.NET 8/4/2009 3:47 AM Tim Huffam
RTFM!
Read all of step 4. If this doesn't make sense then cut and paste the following line into VS:
System.Diagnostics.EventLog.CreateEventSource
Then click on the word CreateEventSource and press F1 then read the help and try the sample code.


# re: Log4Net - writing to windows event log with ASP.NET 2/18/2010 12:46 AM Nash
How can i read the conversionPattern? Can you please post some code here?

Regards,
Nash

# re: Log4Net - writing to windows event log with ASP.NET 5/14/2010 1:13 AM Deepak
Better solutions here http://dotnetways.blogspot.com/

Post A Comment
Title:
Name:
Email:
Comment:
Verification: