0

In a C# code I need to create several log4net loggers with different names to differentiate the logs written. One logger can be called MyNamespace.MyClass.1, another one MyNamespace.MyClass.2, etc. The logs are then written in several files: MyNamespace.MyClass.1.log, MyNamespace.MyClass.2.log, etc.

The problem is that a have to manually add paragraphs in the configuration, each paragraph corresponding to only one logger name.

<appender name="logger1" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/MyNamespace.MyClass.1.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maximumFileSize value="2MB" />
  <maxSizeRollBackups value="10" />
  <staticLogFileName value="true" />
  <threshold value="INFO" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger %ndc: %message (%file:%line)%newline" />
  </layout>
</appender>

<logger name="MyNamespace.MyClass.1">
  <level value="INFO" />
  <appender-ref ref="logger1" />
</logger>

Is there a solution to generalize for n logger without writing n times this code?

Davy
  • 429
  • 2
  • 17

1 Answers1

1

You can programmatically configure multiple loggers. See the link below:

Log4Net: Programmatically specify multiple loggers

Community
  • 1
  • 1
Josep B.
  • 587
  • 1
  • 6
  • 16
  • Thanks a lot for this answer. With this method we lose the flexibility provided by the configuration file but I'll be able to automatically fill the log files. – Davy Nov 23 '16 at 12:10