2012-02-28 85 views
2

我有一個Windows窗體應用程序,我試圖實現log4net,以便我可以寫一些日誌。然而,我似乎無法讓它工作。在Windows窗體應用程序log4Net不寫日誌文件

我實現如下: -

log4Net.config: -

<configuration> 
<!-- Register a section handler for the log4net section --> 
    <configSections> 
     <section name="log4net" type="System.Configuration.IgnoreSectionHandler" requirePermission="false" /> 
    </configSections> 
    <appSettings> 
     <!-- To enable internal log4net logging specify the following appSettings key --> 
     <add key="log4net.Internal.Debug" value="true"/> 
    </appSettings> 
    <!-- This section contains the log4net configuration settings --> 
    <log4net> 
     <!-- Define some output appenders --> 
     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
      <file value="C:\Johann\Log\rolling-log.txt" /> 
      <appendToFile value="true" /> 
      <maxSizeRollBackups value="10" /> 
      <maximumFileSize value="100" /> 
      <rollingStyle value="Size" /> 
      <staticLogFileName value="true" /> 
      <layout type="log4net.Layout.PatternLayout"> 
       <header value="[Header]&#13;&#10;" /> 
       <footer value="[Footer]&#13;&#10;" /> 
       <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> 
      </layout> 
     </appender> 
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
      <file value="C:\Johann\Log\log-file.txt" /> 
      <appendToFile value="true" /> 
      <!-- An alternate output encoding can be specified --> 
      <!-- <encoding value="unicodeFFFE" /> --> 
      <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> 

     <!-- Setup the root category, add the appenders and set the default level --> 
     <root> 
      <level value="ALL" /> 
      <appender-ref ref="LogFileAppender" /> 
      <appender-ref ref="RollingLogFileAppender" /> 
     </root> 
    </log4net> 
</configuration> 

,並在我的.cs

using log4net; 

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

public partial class Form1 : Form 
{ 

    public static readonly ILog log = LogManager.GetLogger("NotifMailer"); 


    private void button1_Click(object sender, EventArgs e) 
    { 

     //CreateFolder(); 

     log4net.Config.BasicConfigurator.Configure(); 

     if (log.IsErrorEnabled) 
     { 
      try 
      { 
       log.Error("Page Load failed : "); 
      } 
      catch (Exception exc) 
      { 
       string exception = exc.Message; 
      } 
     } 

     if (log.IsDebugEnabled) 
     { 
      try 
      { 
       log.Debug("Application loaded successfully."); 
      } 
      catch (Exception exc) 
      { 
       string exception = exc.Message; 
      } 
     } 
    } 
} 

正如你可以從這個代碼中看到的,我做了小測試創建一個文件和文件夾通過代碼「// CreateFolder();」和工作,所以它不是一個權限問題。

我做錯了什麼?

感謝您的幫助和時間

UPDATE

Log4Net.config: -

<configuration> 
    <configSections> 
      <section name="log4net" type="System.Configuration.IgnoreSectionHandler" requirePermission="false" /> 
    </configSections> 
    <log4net> 
     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
      <file value="C:\Johann\Log\rolling-log.txt" /> 
      <appendToFile value="true" /> 
      <maxSizeRollBackups value="10" /> 
      <maximumFileSize value="100" /> 
      <rollingStyle value="Size" /> 
      <staticLogFileName value="true" /> 
      <layout type="log4net.Layout.PatternLayout"> 
       <header value="[Header]&#13;&#10;" /> 
       <footer value="[Footer]&#13;&#10;" /> 
       <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> 
      </layout> 
     </appender> 
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
      <file value="C:\Johann\Log\log-file.txt" /> 
      <appendToFile value="true" /> 
      <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> 

     <root> 
      <level value="ALL" /> 
      <appender-ref ref="LogFileAppender" /> 
      <appender-ref ref="RollingLogFileAppender" /> 
     </root> 
    </log4net> 
</configuration> 

App.Config中: -

<appSettings> 
    <add key="log4net-config-file" value="Log4Net.config"/> 
</appSettings> 

Assembly.cs: -

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)] 

Form1.cs中: -

private static readonly ILog log = LogManager.GetLogger(typeof(Form1)); 

private void button1_Click(object sender, EventArgs e) 
{ 

    //log4net.Config.BasicConfigurator.Configure(); 

    XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["log4net-config-file"])); 

    log.Error("Page Load failed : "); 
    log.Debug("Application loaded successfully."); 

} 

仍然沒有運氣雖然

+0

''...啓用log4net調試語句,看看是否給你任何線索。 – 2012-02-28 13:17:53

回答

2

我通常不線了log4net的使用集屬性

如果您正在使用外部配置文件app/web.confg你的根元素應該是:

<?xml version="1.0"?> 
<log4net> 
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
     <file value="C:\Johann\Log\rolling-log.txt" /> 
     <appendToFile value="true" /> 
     <maxSizeRollBackups value="10" /> 
     <maximumFileSize value="100" /> 
     <rollingStyle value="Size" /> 
     <staticLogFileName value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <header value="[Header]&#13;&#10;" /> 
     <footer value="[Footer]&#13;&#10;" /> 
     <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> 
     </layout> 
    </appender> 
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
     <file value="C:\Johann\Log\log-file.txt" /> 
     <appendToFile value="true" /> 
     <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> 

    <root> 
     <level value="ALL" /> 
     <appender-ref ref="LogFileAppender" /> 
     <appender-ref ref="RollingLogFileAppender" /> 
    </root> 
</log4net> 

根據文檔:

如果未指定ConfigFile或ConfigFileExtension屬性,則應用程序配置文件(例如, TestApp.exe.config)將被用作log4net配置文件。

所以你要麼需要做的:

[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config",Watch=true)] 

還是log4net.config重命名爲YourApp.exe.log4net和組件屬性設置爲:

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

或刪除全部屬性並使用XmlConfigurator:

​​

http://logging.apache.org/log4net/release/manual/configuration.html

+0

我已經在Assembly中試過:(([assembly:log4net.Config.XmlConfigurator(ConfigFile =「Log4Net.config」,Watch = true)] – Johann 2012-02-28 12:43:19

+0

嗨Bronumski你的根與我的根有什麼不同?對我來說,看起來類似於 – Johann 2012-02-28 12:50:28

+0

@Johann在外部文件中它應該是「log4net」而不是「配置」 – Bronumski 2012-02-28 12:52:37

0

log4net.Config.BasicConfigurator。配置();

這是一個最基本的配置只支持寫入控制檯也忽略你的配置文件。

你應該使用XmlConfigurator而非BasicConfigurator

+0

我已經刪除了BasicConfigurator,並且儘管XmlConfigurator仍然沒有運氣,但是//log4net.Config.BasicConfigurator.Configure(); log4net.Config.XmlConfigurator.Configure(); – Johann 2012-02-28 12:48:31

1

發現我的問題。

我應該做的

XmlConfigurator.Configure(); 

內Program.cs的,因爲它有這樣的記錄器的初始化,然後運行這段代碼

private static readonly ILog log = LogManager.GetLogger(typeof(Form1)); 
相關問題