2017-03-27 65 views
0

app.config文件log4net的文件不會創建

<log4net> 
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"> 
     <file type="log4net.Util.PatternString" value="%property[LogFileName]"/> 
     <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> 
     <appendToFile value="true"/> 
     <rollingStyle value="Size"/> 
     <maxSizeRollBackups value="10000"/> 
     <maximumFileSize value="10MB"/> 
     <staticLogFileName value="true"/> 
     <layout type="log4net.Layout.PatternLayout"> 
      <param name="CoversionPattern" value="%date [%thread] : %message%newline%exception"/> 
     </layout> 
    </appender> 
    <root> 
     <level value="ALL"></level> 
     <appender-ref ref="LogFileAppender"/> 
    </root> 
</log4net> 

我在AssemblyInfo.cs中添加也

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

什麼是app.config中的錯誤餘did't獲得日誌文件,也是我在編寫日誌文件時沒有發生任何錯誤。這是Windows應用程序。

我在Bin文件夾中搜索到的文件是否正確?

+0

這裏看看http://stackoverflow.com/questions/756125/how -to-軌道下log4net的-問題 – Lanorkin

回答

1

你有2次失誤在配置:

變化:

<param name="CoversionPattern" value="%date [%thread] : %message%newline%exception"/> 

TO:

<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/> 

而且

變化:

<file type="log4net.Util.PatternString" value="%property[LogFileName]"/> 

TO:

<file type="log4net.Util.PatternString" value="C:\MyLogs\%property{LogFileName}.txt"/> 

Log4net sdk refference

關鍵查找必須括號內指定並直接 以下模式說明符,例如%屬性{}用戶

工作總的appender配置:

<appender name="RollingFileAppender" type="log4net.appender.RollingFileAppender"> 
      <file type="log4net.Util.PatternString" value="C:\MyLogs\%property{LogFileName}.txt"/> 
      <appendToFile value="true"/> 
      <rollingStyle value="Size"/> 
      <maximumFileSize value="10MB"/> 
      <maxSizeRollBackups value="5"/> 
      <staticLogFileName value="true"/> 
      <layout type="log4net.Layout.PatternLayout"> 
       <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/> 
      </layout> 
     </appender> 

和C#代碼:

using System; 

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

namespace Log4NetConsoleApplication 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      log4net.GlobalContext.Properties["LogFileName"] = "log";  

      log4net.ILog log = LogHelper.GetLogger(); 

      Console.WriteLine("hello world"); 

      log.Error("This is my error message"); 

      Console.ReadLine(); 
     } 
    } 
} 
1

變化的app.config行:

<file type="log4net.Util.PatternString" value="%property[LogFileName]"/> 

這一個

<file type="log4net.Util.PatternString" value=".\%property{LogFileName}" /> 

然後確保你是在GlobalContext中設置LOGFILENAME,示例代碼如下所示:

static void Main(string[] args) 
    { 
     log4net.GlobalContext.Properties["LogFileName"] = "log.txt"; 

     log4net.ILog logger = log4net.LogManager.GetLogger("Tests"); 

     logger.Debug("Test message"); 
    } 

參考這更多信息:https://www.codeproject.com/Tips/400224/Multiple-log-files-from-one-application-using-log

根據構建配置 - debug/release,您應該在BIN/debug或BIN/release文件夾中看到log.txt。