У меня есть приложение Windows Forms, и я пытаюсь реализовать 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.");
}
Все же не повезло, хотя