1

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.

Community
  • 1
  • 1
Mefhisto1
  • 2,188
  • 7
  • 34
  • 73
  • 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_) : – g.pickardou Aug 11 '15 at 12:25
  • and... Do you have a configSection like this? (Your code from you config file does not shows that) :
    – g.pickardou Aug 11 '15 at 12:28
  • No, I don't. Can you please provide more info on what I'm missing in the config file? – Mefhisto1 Aug 11 '15 at 12:30
  • (not related with the issue but...) The parameter of GetLogger is _not_ about the logger name, instead it will identify the code part where the log is coming from (free string) typically you class name. – g.pickardou Aug 11 '15 at 12:31
  • 1
    Please see the Q/A what you were referring to (http://stackoverflow.com/questions/15088226/log4net-with-eventlogappender-doesnt-log). There is a part you are missing:
    – g.pickardou Aug 11 '15 at 12:33
  • 1
    Post your whole config file it also seems you are making some mistake when copy and pasting and/or editing it. – g.pickardou Aug 11 '15 at 12:34
  • configSections, if there, should be the first entry in your config file. Move it above the startup section. – Sandeep Aug 11 '15 at 12:51
  • If you enable catching CLR exceptions, the inner exception will clearly point out this issue. – Sandeep Aug 11 '15 at 12:52

1 Answers1

1

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>
g.pickardou
  • 32,346
  • 36
  • 123
  • 268