How to write entries to Windows Event Viewer by log4net in WPF?

1.1k views Asked by At

I have read this article. and this answer1SO, answer2SO, answer3SO.

What I've done so far:

1. Declared in the last line of AssemblyInfo.cs :

    [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

2. Written in app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <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>
    <root>
      <level value="ALL"/>
      <appender-ref ref="EventLogAppender"/>
    </root>
  </log4net>
</configuration>

3. What I've written in my handler:

private void btn_Click(object sender, RoutedEventArgs e)
{
   XmlConfigurator.Configure();       
   ILog Log = 
   LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
   Log.Fatal("The exception occurred", new Exception("Hello World of event log")); 
}`

After all these three implemented actions, I have no entries in my Window Event Viewer. Please, see my Window Event Viewer: enter image description here

1

There are 1 answers

12
stuartd On BEST ANSWER

Your app needs to run once as Administrator to create the event source that the logs will be assigned to: see "Why doesn't the EventLogAppender work?" in the log4net documentation.