我有一个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();”来创建一个文件和文件夹,这样就可以了,所以它不是权限问题。
我做错了什么?
感谢您的帮助和时间
更新
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.");
}
但仍然没有运气
答案 0 :(得分: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] " />
<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>
根据文件:
如果未指定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:
XmlConfigurator.ConfigureAndWatch("log4net.config");
http://logging.apache.org/log4net/release/manual/configuration.html
答案 1 :(得分:1)
我应该做的
XmlConfigurator.Configure();
在Program.cs中,因为它必须在执行初始化记录器之前运行该段代码
private static readonly ILog log = LogManager.GetLogger(typeof(Form1));
答案 2 :(得分:0)
log4net.Config.BasicConfigurator.Configure();
这是一个简单的配置,只支持写入控制台,也忽略了配置文件。
您应该使用XmlConfigurator而不是BasicConfigurator