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 
"/>
<footer value="Logging End 
"/>
<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!
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.