1
votes

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.

1
Diagnostics: Please add the following lines to your web.config, then run (debug) and see the output window, and solve or get back here with the extended diagnostic info. Good luck. (sorry for the messy code, but this is a comment) : <appSettings> <add key="log4net.Internal.Debug" value="true" /> </appSettings>g.pickardou
and... Do you have a configSection like this? (Your code from you config file does not shows that) : <section name="log4net" type="....g.pickardou
No, I don't. Can you please provide more info on what I'm missing in the config file?Mefhisto1
Please see the Q/A what you were referring to (stackoverflow.com/questions/15088226/…). There is a part you are missing: <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections>g.pickardou
Post your whole config file it also seems you are making some mistake when copy and pasting and/or editing it.g.pickardou

1 Answers

1
votes

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>