Using log4net to write to different loggers

You have configured Log4Net so that the root logger logs to both file and event log appenders. All loggers inherit this configuration, so both your loggers "DataIntakeService" and "EventLogAppender" log to these appenders.

If you don't see log messages in event viewer, it may be because your application doesn't have permission to create the Event source.

UPDATE

How can i configure it so the DataIntakeService logs to the file and the other one to the eventviewer ?

Here's a sample configuration:

<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <logger name="EventLogAppender" additivity="False">
    <level value="INFO" />
    <appender-ref ref="EventLogAppender" />
  </logger>
  <appender>
  ... 

With this sample:

  • The root logger (and hence all child loggers unless explicitly configured otherwise) will log to LogFileAppender. Your DataIntakeService logger isn't explicitly configured, so inherits this configuration.

  • The EventLogAppender logger is explicitly configured to log to EventLogAppender, and is configured not to inherit settings from parent loggers (additivity="false"). Therefore it doesn't log to LogFileAppender. If you set additivity="true" it will inherit settings and log to both LogFileAppender and EventLogAppender.

Incidentally, naming a logger EventLogAppender is perhaps a bit confusing: EventLogLogger might be a better name.


You can use log4net to write to different loggers by the configuration below.

<log4net>
<logger name="LogFileLogger">
  <level value="INFO" />
  <appender-ref ref="LogFileAppender" />
</logger>
<logger name="EventLogger">
  <level value="INFO" />
  <appender-ref ref="EventLogAppender" />
</logger>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
  ...
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  ...
</appender>

And you can call the loggers as:

private static readonly log4net.ILog LogFileLogger= log4net.LogManager.GetLogger("LogFileLogger");
private static readonly log4net.ILog EventLogger= log4net.LogManager.GetLogger("EventLogger");