简体   繁体   中英

log4net - event viewer appender not working

I checked:

log4net with EventLogAppender doesn't log

and tried to do almost the same as the answer in that question, but without success, and also I'm running Visual Studio as an admin.

.config file looks like this:

  <?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>

And I try to do something like this:

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

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

I'm using log4net assembly for .NET 2, if that has to do with anything.

I just get the error that the .config is not a valid xml file:

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

Try this: (the configSections goes first, unless you got the exception you described)

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM