12

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!");
    }
}
Philipp M
  • 1,877
  • 7
  • 27
  • 38
timmkrause
  • 3,367
  • 4
  • 32
  • 59

3 Answers3

15

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.

Shaun Wilson
  • 8,727
  • 3
  • 50
  • 48
sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • In combination with peers line of code it finally worked. Thank you! :) There is one last flaw: The log entries only show up if I start the .exe file itself but not when I hit f5. Any idea on that last one? – timmkrause Feb 27 '13 at 08:55
  • Do you have any error messages in Visual Studio output window? The debug="true" section of the config above should give you information on what log4net is doing and should include error messages if it is fails. (You probably want to disable debug mode when you have your logging working) – sgmoore Feb 27 '13 at 09:25
  • "log4net:ERROR [EventLogAppender] ErrorCode: GenericFailure. Caught a SecurityException trying to access the EventLog. Most likely the event source Log4NetEventViewer.vshost.exe doesn't exist and must be created by a local administrator." - I started the .exe as admin to create the event source but not the VS or the vshost edition. Running the .vshost.exe as admin was also not enough, because it started a .exe with a "-clr2" suffix or something. Running VS as admin once solved it. THANK YOU! – timmkrause Feb 27 '13 at 10:43
  • 4
    To avoid the eventlog source changing like this you can set the ApplicationName property in your config file. – sgmoore Feb 27 '13 at 11:43
  • That tip makes your answer even more awesome! :-) – timmkrause Feb 27 '13 at 13:38
  • Could you please elaborate where exactly should be this "ApplicationName" property in the application's .cofig file? – serge Aug 05 '15 at 14:05
  • `` goes into the EventLogAppender section, not necessarily your app.config, but definitely your log4net config – Shaun Wilson Jan 30 '16 at 03:46
6

You should add:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

in the AssemblyInfo.cs of your project. This will initialize the logger. And put the log4net into a file named log4net.config.

Peter
  • 27,590
  • 8
  • 64
  • 84
  • I tried that already and then deleted it again because it had no effect. – timmkrause Feb 26 '13 at 12:13
  • Sorry, you were right. After I added the element provided by sgmoore it still didn't work but after I added your line in the AssemblyInfo.cs it worked. – timmkrause Feb 27 '13 at 08:52
0

Make sure that you gave write permissions of your user to:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\EventLog\MyApp

and if you want to add more loggers have to follow this nomenclature:

<root name="EventLog">
  <level value="ALL"/>
  <appender-ref ref="FirstLog"/>
</root>

<logger name="FileLogger" additivity="false">
  <level value="ALL" />
  <appender-ref ref="Second_Log" />
</logger>

Good Luck.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
David Castro
  • 1,773
  • 21
  • 21