2016-01-13 129 views
0

是否有可能每天都創建一個日誌路徑,以下面的文件夾格式寫入日誌。Log4Net C# - 按日期創建日誌文件夾

D:\Logs\yyyyMMdd\App1\App.txt 

如果Log4Net不存在,Log4Net會創建路徑。 ?

我嘗試了以下,但它似乎沒有工作。

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> 
     <file value="D:\Logs\" /> 
     <appendToFile value="true" /> 
     <datePattern value="yyyyMMdd'\App1\App.txt'" /> 
     <rollingStyle value="Date" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception" /> 
     </layout> 
    </appender> 

在此先感謝。

回答

2

您可以介紹自己的appender:

namespace CustomLogging 
    { 
    public class CustomAppender : log4net.Appender.RollingFileAppender 
     { 
     protected override void OpenFile(string fileName, bool append) 
     { 
      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="CustomAppender" type="CustomLogging.CustomAppender"> 
    <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

感謝。它有幫助。 – Muthukumar

0

嘗試這種解決方案,但只有XAML

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
     <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> 
     <file value="logs\" /> 
     <datePattern value="dd.MM.yyyy'.log'" /> 
     <staticLogFileName value="false" /> 
     <appendToFile value="true" /> 
     <rollingStyle value="Composite" /> 
     <maxSizeRollBackups value="10" /> 
     <maximumFileSize value="5MB" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
     </layout> 
    </appender>