gpt4 book ai didi

winforms - Windows 窗体应用程序中的 log4Net 不写入日志文件

转载 作者:行者123 更新时间:2023-12-04 05:56:18 24 4
gpt4 key购买 nike

我有一个 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();”创建了一个文件和文件夹,并且有效,所以它不是权限问题。

我做错了什么?

感谢您的帮助和时间

更新

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>

装配.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

如果你使用 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>

根据文档:

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.



所以你要么需要做:

[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

关于winforms - Windows 窗体应用程序中的 log4Net 不写入日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9481960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com