1

I configured two loggers and want them to simultaneously log to Event Log and text file, but they both logging to text. Here is my configuration file:

<log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="NavRiskFileImportError.log" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <maxSizeRollBackups value="2" />
      <datePattern value="yyyyMMdd" />
      <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="{%level}{%date} – %message%newline"/>
        </layout>
    </appender>

    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <param name="ApplicationName" value="NavriskFromExcel" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
<!--loggers-->
    <logger name="FileLogger">
      <level value="ALL" />
      <appender-ref ref="RollingFile" />
    </logger>

    <logger name="EventLogger">
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </logger>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="EventLogAppender" />
      <appender-ref ref="RollingFile" />
    </root>
  </log4net>

In my application I have:

private static  ILog logger= LogManager.GetLogger("FileLogger");
private static  ILog eventLogger= LogManager.GetLogger("EventLogger");

But both loggers are writing data to the file and nothing in the event log. What I'm doing wrong?

Yuri
  • 2,820
  • 4
  • 28
  • 40

1 Answers1

4

You have to specify additivity="false" on the individual loggers, otherwise they inherit the appenders from the root logger:

Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used (source)

So that would mean:

<logger name="EventLogger" additivity="false">

That explains why both are writing to the file.

So the remaining question is why the EventLogAppender isn't working. You can check for configuration errors with this code:

var messages = LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>();

if (messages.Any())
{
     string message = messages.Aggregate("Log4net Configuration Errors: "
                                        + Environment.NewLine,
            (s, log) => s + log.Message + Environment.NewLine);
}

And if that doesn't show anything, enable log4net internal debugging and check the output.

Edit: the documentation states EventLogAppender will fail if you try to write using an event source that doesn't exist unless it is running with local administrator privileges but the event source needs to be created by an elevated process, or an installer - the event source has to be created once per machine.

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Thanks, tried that, now it is writing only to the file nothing in the Event Log and no errors – Yuri Feb 05 '15 at 19:16
  • My guess would be permissions. Is this a web application? – stuartd Feb 05 '15 at 19:40
  • No, this is console, running under domain admin account – Yuri Feb 05 '15 at 19:46
  • Try running it in as Administrator, in case it's trying to create an event source - I've not used log4net to log to the event log, but I've seen that in other contexts. – stuartd Feb 05 '15 at 19:52