如何在 WPF 中通过 log4net 将条目写入 Windows 事件查看器?

How to write entries to Windows Event Viewer by log4net in WPF?

我已阅读this article. and this answer1SO, answer2SO, answer3SO

到目前为止我做了什么:

1.在AssemblyInfo.cs的最后一行声明:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

2。写在app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="EventLogAppender"/>
    </root>
  </log4net>
</configuration>

3。我在处理程序中写的内容:

private void btn_Click(object sender, RoutedEventArgs e)
{
   XmlConfigurator.Configure();       
   ILog Log = 
   LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
   Log.Fatal("The exception occurred", new Exception("Hello World of event log")); 
}`

在执行了所有这三个操作之后,我的 Window 事件查看器中没有任何条目。请查看我的 Window 事件查看器:

您的应用需要以管理员身份 运行 一次以创建将日志分配到的事件源:请参阅 log4net 文档中的 "Why doesn't the EventLogAppender work?"