log4Net EventlogAppender does not work for Asp.Net 2.0 WebSite? log4Net EventlogAppender does not work for Asp.Net 2.0 WebSite? asp.net asp.net

log4Net EventlogAppender does not work for Asp.Net 2.0 WebSite?


There's some very simple steps to get Log4Net working with the event log with ASP.NET.

Your options are:

Change the app pool

Run the application pool as LocalSystem

Or...set an application name, add a registry key

Configure your config file first so there is an application name.

This is important if you are having a permissions issue, because if you peer at the source you'll see the EventLogAppender defaults the application name (the source column in the event log) to:

System.Threading.Thread.GetDomain().FriendlyName

This requires your application to create a registry key each time the name changes, and is a bit messy. So the best solution is to set your application name. Here's an example EventLogAppender section that sends every error level, the ApplicationName line is the important part:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">    <param name="ApplicationName" value="MY-AWESOME-APP" />    <evaluator type="log4net.Core.LevelEvaluator">        <threshold value="INFO" />    </evaluator>    <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value="INFO"/>        <param name="LevelMax" value="FATAL"/>    </filter>    <layout type="log4net.Layout.PatternLayout">        <!-- The event log already logs the date so just log the message -->        <conversionPattern value="%message" />    </layout></appender>

Before making this change, do the following:

Create a registry key

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application\MY-AWESOME-APP

Create a string value inside this

Name it EventMessageFile, set its value to

C:\Windows\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll

That path appears to work in both 64 bit and 32 bit environments.

With this technique you don't need to set permissions in the registry, and once the key above is created it should just work.


I can't tell you exactly what might be wrong, but if you go here you will see "How do I enable log4net internal debugging?" way down at the bottom. Log4net will not throw exceptions if something is wrong, so you must enable internal debugging in order to get information from log4net.

http://logging.apache.org/log4net/release/faq.html

Edit: Also see the quesiton directly after that one that explains potential hangups with using the EventLogAppender.


I would suspect code access security is preventing you from accessing the event log. This article have information on the topic. Basically, if your ASP.Net application is running under medium trust the app will not have access to event logs.

Note: this is not a restriction built into log4net per se, it is a "feature" of the .Net Framework and ASP.Net.