Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_MACHINESYSTEMCurrentControlSetserviceseventlogApplicationMY-AWESOME-APP

Create a string value inside this

Name it EventMessageFile, set its value to

C:WindowsMicrosoft.NETFrameworkv2.0.50727EventLogMessages.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...