1

With log4net, is there a way to embed the name of the logger in the logfile name in the Appender?

In the following, I want to include the name of the logger in the filename in place of (My.Logger).

<appender name="MyFileAppender" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="c:\logs" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="'(My.Logger)-'yyyy-MM-dd'.log'" />
  <staticLogFileName value="false" />
  <layout type="MinLayout">
  </layout>
</appender>

<logger name="My.Logger">
  <level value="DEBUG" />
  <appender-ref ref="MyFileAppender"/>
</logger>

I realize the appender should not care which logger it is, but it the information is available in the layout.

My goal is that I want to be able to turn on/off logging for specific namespaces to corresponding files without having to define a unique Appender for each one.

ThisGuy
  • 2,335
  • 1
  • 28
  • 34
  • 1
    You can [set the file name at runtime](http://stackoverflow.com/questions/17142258/change-name-of-logfile-a-few-times-during-runtime), how you would do that on a per-logger basis depends on your logging strategy. I'd think using a wrapper which receives a log request, sets the log file name, and the writes the log data might work, as long as two loggers don't try and write at the same time.. – stuartd Mar 20 '14 at 19:27
  • Thanks stuartd. I was hoping for a pure configuration solution, but that sounds like it should work. – ThisGuy Mar 21 '14 at 19:16

0 Answers0