How I can set log4net to log my files into different folders each day?

前端 未结 5 1740
终归单人心
终归单人心 2020-11-30 06:53
  1. I want to save all logs during each day in folder named YYYYMMdd - log4net should handle creating new folder depending on system datetime - how I can setup this?
相关标签:
5条回答
  • 2020-11-30 07:09

    You could use the rollinglogfileappender which creates files named by date and starts a new file at midnight. Then just write a script that moves them to a correct map at 00.01.

    As for logging all files during one day with a maximum of 1 MB per file, here's an example:

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logging\\MWLog"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="-yyyyMMdd"/>
      <maxSizeRollBackups value="-1"/>
      <maximumFileSize value="1MB"/>
      <countDirection value="1"/>
      <encoding value="utf-8"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
      </layout>
    </appender>
    
    0 讨论(0)
  • 2020-11-30 07:12

    Try this (It should be OK!):

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\" />
      <appendToFile value="true" />
      <DatePattern value="yyyy\\\\MM\\\\dd'.inf.log'" />
      <rollingStyle value="Date" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="[Header]&#13;&#10;" />
        <footer value="[Footer]&#13;&#10;" />
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
      </layout>
    </appender>
    

    It will create a logfile named 'logs\2010\04\02.inf.log' (let date be 2010-04-02)

    0 讨论(0)
  • 2020-11-30 07:14

    To build from the answer above using the SortByFolderFileAppender.

    This is how we resolved the issue using rolling date for log filenames. I changed the staticLogFileName to true so the entire filename is passed into the OpenFile method.

    If the filename ends in ".log" then nothing needs to be appended. I'm guessing some kind of locking has occurred and I want log4net to try using the same filename again, hoping the previous lock has been released.

    Although, I'm not sure if this could end up causing an infinite call to OpenFile if the file is locked and won't let go of it. We did create a web service using the producer consumer pattern to log everything in one location from all applications in the system, which is currently ten and growing.

    We don't need to include log4net into any of the other applications but we needed to create a web client class that is accessible by all applications to use for logging to the web service.

    The result for the filename is "Logs\Client\2017\11\08.log" and obviously changes everyday.

    protected override void OpenFile( string fileName, bool append )
    {
        // append "\yyyy\mm\dd.log" to create the correct filename.
        if ( !fileName.EndsWith( ".log") )
            fileName = $@"{fileName}\{DateTime.Now:yyyy\\MM\\dd}.log";
    
        base.OpenFile( fileName, append );
    }
    

    Modification of the configuration from above.

    <appender name="xxxRollingFileAppender" type="namespace.xxxRollingFileAppender">
        <file value="Logs\Client"/>
        <staticLogFileName value="true"/>
        <appendToFile value="true"/>
        <maxSizeRollBackups value="40"/>
        <maximumFileSize value="1MB"/>
        <encoding value="utf-8"/>
        <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
        </layout>
    </appender>
    
    0 讨论(0)
  • 2020-11-30 07:15

    Thank you all. We created SortByFolderFileAppender, which inherit from RollingFileAppender

    Example of final result: somewhere\Logs\20100305\Client-104615.0

    namespace CustomLogging
    {
      public class SortByFolderFileAppender : log4net.Appender.RollingFileAppender
      {
        protected override void OpenFile(string fileName, bool append)
        {
          //Inject folder [yyyyMMdd] before the file name
          string baseDirectory = Path.GetDirectoryName(fileName);
          string fileNameOnly = Path.GetFileName(fileName);
          string newDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd"));
          string newFileName = Path.Combine(newDirectory, fileNameOnly);
    
          base.OpenFile(newFileName, append);
        }
      }
    }
    
    <appender name="SortByFolderFileAppender" type="CustomLogging.SortByFolderFileAppender">
      <file type="log4net.Util.PatternString" value="Logs\Client"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="-HHmmss"/>
      <maxSizeRollBackups value="40"/>
      <maximumFileSize value="1MB"/>
      <countDirection value="1"/>
      <encoding value="utf-8"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
      </layout>
    </appender>
    
    0 讨论(0)
  • 2020-11-30 07:32

    I believe, you may create your own appender, based on the FileAppender class. You may need to override the OpenFile method to create a file in the right location.

    0 讨论(0)
提交回复
热议问题