7
votes

I am using log4net for logging, I have two appenders one file and other eventlog appender. I have register application in registry editor, problem is now both logger are writing in event viewer. I need fileAppender to write in file and eventLogAppender to be shown in event viewer.

<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <file value="file.log"/>
  <appendToFile value="false"/>
  <layout type="log4net.Layout.PatternLayout">
    <header value="Logging Start &#xD;&#xA;"/>
    <footer value="Logging End &#xD;&#xA;"/>
    <conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
  </layout>
</appender>

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">

  <param name="ApplicationName" value="eventlog" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

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

and then in code

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// private static readonly ILog log = LogManager.GetLogger("LogFileAppender");
private static readonly ILog logEvents = LogManager.GetLogger("EventLogAppender");

I have tried different things but no one is working, any solution?

Thanks!

1
Are you trying to make it so that you can write different things to each target? Normally you use multiple appenders in log4net to write the same thing to multiple destinations with one method call.dodexahedron
yes, I want more detailed logging using fileappender, and then some logging in Event viewer, I hope I make it clear..aDev
You don't have any logger elements in your config, so there's no way to instantiate more than one the way you have it set up. Answer coming shortly...dodexahedron

1 Answers

14
votes

In order to use multiple ILog instances with log4net and to log different information to each, you must configure at least two Logger elements in the log4net section of your config file.

In your case, you have two targets that you want - file and event log. I'll call them FileLogger and EventLogger.

Here is an example of the additional configuration you need in the log4net section:

<logger name="FileLogger">
  <level value="ALL" />
  <appender-ref ref="LogFileAppender" />
</logger>

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

Then, to use each one in code, instantiate them like so:

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

Note that the ref attribute of the appender elements matches the name attribute of the appenders you have already configured and that the name passed to GetLogger matches the name attribute of the logger elements.