views:

522

answers:

2

I have configured log4Net EventLogAppender for Asp.Net 2.0. However it does not log anything. I have following in my Web.Config.

<log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <param name="LogName" value="Test Log" />
      <param name="ApplicationName" value="Test-Web" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <priority value="ERROR"/>
      <appender-ref ref="EventLogAppender"/>
    </root>
    <logger name="NHibernate">
      <level value="ERROR" />
      <appender-ref ref="EventLogAppender" />
    </logger>
  </log4net>

I already have Test-Log Event Log created and AspNet user has permission on the Event Log registry entry. I also have log4Net configured in Global.asax Application_Start.

log4net.Config.XmlConfigurator.Configure();

Update : I switched on the log4net internal debugging and found the following error in the trace.

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender [EventLogAppender] of type [log4net.Appender.EventLogAppender]. Reported error follows.
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
   at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly)
   at System.Diagnostics.EventLog.SourceExists(String source, String machineName)
   at System.Diagnostics.EventLog.SourceExists(String source)
   at log4net.Appender.EventLogAppender.ActivateOptions()
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)

Update 2 : It finally works if I create the Event Source (Test-Web) using a console based application in c# and then using the Web Application.

+3  A: 

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.

AaronLS
+1  A: 

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.

Peter Lillevold