log4net multiple thread one file

39 Views Asked by At

Actually I use log4net in my c# application. I configure it like this.

<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="file" />
  </root>
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <file value="Debug_" />
    <appendToFile value="true"/>
    <staticLogFileName value="false"/>
    <datePattern value="'.'yyyy_MM_dd hh_mm_ss'.log'"/>
    <rollingStyle value="Date"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n"/>
    </layout>
  </appender>
</log4net>

My log file start when my application start with the datetime. In my application I've multiple thread and I call the log function to store information in the different thread. While my application is runnning multiple file are created. Is there a solution for all the thread to append in the same file ? is there a queue solution ?

Thanks

1

There are 1 best solutions below

2
stuartd On
<datePattern value="'.'yyyy_MM_dd hh_mm_ss'.log'"/>

By including seconds (hh_mm_ss) in the DatePattern you are telling log4net to create a new file every time a log entry is made more than a second after the last one.

Console.WriteLine(DateTime.Now.ToString("yyyy_MM_dd hh_mm_ss"));
// 2023_11_22 02_02_01