Having a log per day

后端 未结 3 1645
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 13:20

Currently my application is using log4net to log errors, the web.config for this is as followed:

 
            


        
相关标签:
3条回答
  • 2020-12-08 13:45

    Like using the RollingFileAppender

    <appender name="RollingLogFileAppenderOutput" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="c:\my.log" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd'.log'" />
        <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%date [%thread] %-5level - %message%newline" />
        </layout>
    </appender>
    

    edit

    This is the whole log4net configuration section to roll everyday, the same as @lazyberezovsky answered just adding the log4net and root for clarification

    edit2 <file value="c:/paypal/logs/gateway_" /> this will create a file named 'gateway_' and at the end of the day (2012-04-27) it will be renamed as gateway_20120427.log and the next day (2012-04-28) it will create again the file gateway_ and at the end it'll create gateway_20120428.log.

    <log4net> 
        <appender name="RollingLogFileAppenderOutput" type="log4net.Appender.RollingFileAppender">
            <file value="c:/paypal/logs/gateway_" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd'.log'" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level - %message%newline" />
            </layout>
        </appender>
    
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingLogFileAppenderOutput" />
        </root>
    </log4net>
    
    0 讨论(0)
  • 2020-12-08 13:48

    You should use RollingFileAppender with rollingStyle Date and datePattern yyyyMMdd (this will roll every day).

    Example of configuration:

    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="c:/paypal/logs/gateway.log" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
        </layout>
    </appender>
    

    UPDATE: I think a better way is to include the date into file name. And use datePattern value only to show when new log file should be created.

    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="c:/paypal/logs/gateway_%date{yyyyMMdd}.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    

    In this case all your files will have name like gateway_20120427.log.

    0 讨论(0)
  • 2020-12-08 13:56

    PreserveLogFileName

    This setting keeps the extension of the file the same after you roll the log.

    <log4net> 
        <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="c:/paypal/logs/gateway.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="_yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
            </layout>
            <preserveLogFileNameExtension value="true" />
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="FileAppender" />
        </root>
    </log4net>
    
    0 讨论(0)
提交回复
热议问题