a specific type should write messages in a separate logfile

35 Views Asked by At

I use the following configuration for log4net:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\WebPortal.API.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <threshold value="ALL" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger{2} - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

I can now get a logger like this and use it:

static readonly ILog log = LogManager.GetLogger(typeof(OperationsPlanningService));

Now, I want to write loggings from one specific type in a separate log-file. When I create a logger with typeof(ExceptionHandlingMiddleware) like this:

static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandlingMiddleware));

the messages from this logger should be written in logs\WebPortal.ExceptionHandlingMiddleware.log

Is this possible with log4net?

1

There are 1 best solutions below

0
Klaus Gütter On BEST ANSWER

You can add specific configurations for specific loggers the same as for the root logger in the XML like:

<logger name="MyLoggerName">
    <level value="WARN" />
    <appender-ref ref="MyAppender" />
</logger>

MyLoggerName could be the full logger name or just the namespace or even part of the namespace (log4net loggers form a hierarchy).