Using Log4Net to use both event log and a rolling log file

Here’s the config section, note that the applicationName property in the EventLogAppender needs to be the same as the event source in the windows event log that you want to log to.  If the event source doesn’t exist, that appender won’t work.  In this particular project I create that during install using WiX (which is covered in another post)

  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log.txt" />
      <datePattern value="dd-MM-yyyy" />
      <appendToFile value="true" />
      <locationinfo value="false" />
      <rollingStyle value="Size" />
      <maximumFileSize value="1MB" />
      <maxSizeRollBackups value="10" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <applicationName value=”[EVENTSOURCEGOESHERE]" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="EventLogAppender" />

You can also create the event source programmatically (if that’s your thing) using System.Diagnostics.EventLog, like this:

string source = "EVENTSOURCEGOESHERE";
if (!EventLog.SourceExists(source))
    EventLog.CreateEventSource(source, "Application");