问题
I have a Windows Forms App, and I am trying to implement log4net so that I can write some logs. However I cannot seem to get it to work.
My implementation is as follows :-
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>
and in my .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;
}
}
}
}
As you can see from this code, I did a small test to create a file and folder through code " //CreateFolder(); " and that works, so its not an issue with permissions.
What I am doing wrong?
Thanks for your help and time
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.");
}
Still no luck though
回答1:
I don't normally wire up log4net using the assembly attribute
If you are using a configuration file external to the app/web.confg your root element should be:
<?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>
According to the docs:
If neither of the ConfigFile or ConfigFileExtension properties are specified, the application configuration file (e.g. TestApp.exe.config) will be used as the log4net configuration file.
So you will either need to do:
[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config",Watch=true)]
Or rename the log4net.config to YourApp.exe.log4net and set the assembly attribute to:
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
Or remove the attribute all together and use the XmlConfigurator:
XmlConfigurator.ConfigureAndWatch("log4net.config");
http://logging.apache.org/log4net/release/manual/configuration.html
回答2:
found my problem.
I should have done
XmlConfigurator.Configure();
inside Program.cs, since it has to run that piece of code before doing the initialize of the logger
private static readonly ILog log = LogManager.GetLogger(typeof(Form1));
回答3:
log4net.Config.BasicConfigurator.Configure();
This is a bare-bones configuration which only supports writing to the Console and also ignores your config file.
You should be using an XmlConfigurator rather than BasicConfigurator
来源:https://stackoverflow.com/questions/9481960/log4net-in-windows-forms-app-does-not-write-log-file