Geeks With Blogs

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

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:
  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):

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

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).



Posted on Thursday, April 13, 2006 7:48 AM BizTalk , C# .NET , ASP.NET , Log4Net | Back to top

Comments on this post: Log4Net - writing to windows event log with ASP.NET

# re: Log4Net - writing to windows event log with ASP.NET
Requesting Gravatar...

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

Thank you!
Left by Nutan on Aug 03, 2009 4:19 AM

# re: Log4Net - writing to windows event log with ASP.NET
Requesting Gravatar...
Read all of step 4. If this doesn't make sense then cut and paste the following line into VS:
Then click on the word CreateEventSource and press F1 then read the help and try the sample code.
Left by Tim Huffam on Aug 04, 2009 3:47 AM

# re: Log4Net - writing to windows event log with ASP.NET
Requesting Gravatar...
How can i read the conversionPattern? Can you please post some code here?

Left by Nash on Feb 18, 2010 12:46 AM

# re: Log4Net - writing to windows event log with ASP.NET
Requesting Gravatar...
Better solutions here
Left by Deepak on May 14, 2010 1:13 AM

Your comment:
 (will show your gravatar)

Copyright © Tim Huffam | Powered by: