我已经为Asp.Net 2.0配置了log4Net EventLogAppender.但是它不记录任何东西.我的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>
我已经创建了测试日志事件日志,AspNet用户具有事件日志注册表项的权限.我还在Global.asax Application_Start中配置了log4Net.
log4net.Config.XmlConfigurator.Configure();
更新:我打开了log4net内部调试,并在跟踪中发现以下错误.
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)
更新2:如果我在c#中使用基于控制台的应用程序创建事件源(Test-Web),然后使用Web应用程序,它将最终起作用.
解决方法
我不能准确地告诉你可能是什么错,但是如果你去这里,你会看到“如何启用log4net内部调试?”在底部的方式.如果出现错误,Log4net将不会抛出异常,所以您必须启用内部调试才能从log4net获取信息.
http://logging.apache.org/log4net/release/faq.html
编辑:还可以直接查看使用EventLogAppender解释潜在挂起的问题.