log4net - 事件查看器附加程序不工作

log4net - event viewer appender not working

我检查过:

log4net with EventLogAppender doesn't log

并尝试做与该问题的答案几乎相同的事情,但没有成功,而且我是 运行 Visual Studio 作为管理员。

.config 文件如下所示:

  <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true" />
  </appSettings>
  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.Patternlayout">
        <conversionPattern value="%date %-5level %logger - %message%newline "/>
      </layout>
      <!--<level value="ALL"/>-->
      <threshhold value="ALL"></threshhold>
      <!--<param name="Threshold" value="ERROR" />-->
      <!--<logName value="Services"/>-->
      <applicationName value="SikuliTestResultImporter"/>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="EventLogAppender"/>
    </root>
  </log4net>
</configuration>

我试着做这样的事情:

private static readonly ILog log = LogManager.GetLogger("EventLogAppender");

try
{
    throw new Exception("Exception mon!");
}
catch (Exception ex)
{
    log.Error(ex.Message);
}

我正在为 .NET 2 使用 log4net 程序集,如果这与任何事情有关的话。

我刚收到 .config 不是有效 xml 文件的错误:

log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.

试试这个:(首先是 configSections,除非你遇到了你描述的异常)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <startup>
       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true" />
  </appSettings>
  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.Patternlayout">
        <conversionPattern value="%date %-5level %logger - %message%newline "/>
      </layout>
      <!--<level value="ALL"/>-->
      <threshhold value="ALL"></threshhold>
      <!--<param name="Threshold" value="ERROR" />-->
      <!--<logName value="Services"/>-->
      <applicationName value="SikuliTestResultImporter"/>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="EventLogAppender"/>
    </root>
  </log4net>
</configuration>