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

时间:2016-01-29 18:42:23

标签: c# wpf logging log4net xmlconfigurator

我已阅读https://github.com/aurelia/skeleton-navigation和此this article.answer1SOanswer2SO

到目前为止我做了什么:

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事件查看器: answer3SO

1 个答案:

答案 0 :(得分:1)

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