我有一個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] " />
<footer value="[Footer] " />
<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] " />
<footer value="[Footer] " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %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] " />
<footer value="[Footer] " />
<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] " />
<footer value="[Footer] " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %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.");
}
仍然沒有運氣雖然
''...啓用log4net調試語句,看看是否給你任何線索。 –
2012-02-28 13:17:53