Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net with EventLogAppender doesn't log

Nothing happens with the following configuration.

App.config

<?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" />
    </startup>
  <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>
  </log4net>
</configuration>

Form1.cs (Example)

public partial class Form1 : Form
{
    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    public Form1()
    {
        InitializeComponent();

        log.Fatal("Test!");
    }
}
like image 920
timmkrause Avatar asked Feb 26 '13 11:02

timmkrause


People also ask

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.


1 Answers

You are missing the root configuration so you need something like

<?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"/>
  </startup>
  <log4net debug="true">
      <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <applicationName value="MyApp" />
        <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>

Also note that if your program is called app.exe , then you need an eventlog source called app.exe. If this does not exist, then Log4net will attempt to create it, but that requires Admin rights, so you may need to run your program as admin at least once in order to create this event source. To avoid this, the event source would normally be created as part of the installation procedure which would already be running as admin.

like image 155
sgmoore Avatar answered Sep 18 '22 11:09

sgmoore