gpt4 book ai didi

c# - 为什么我在使用 log4net 时得到一个空的事件日志?

转载 作者:行者123 更新时间:2023-11-30 15:10:15 25 4
gpt4 key购买 nike

我有课

namespace LogToolsTest
{
public class Foo
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Foo));

public Foo()
{

logger.Debug("Save this text");
logger.Info("Save this text");
logger.Warn("Save this text");
logger.Error("Save this text");
logger.Fatal("Save this text");


var b1 = logger.IsDebugEnabled;
var b2 = logger.IsInfoEnabled;
var b3 = logger.IsWarnEnabled;
var b4 = logger.IsErrorEnabled;
var b5 = logger.IsFatalEnabled;
}
}
}

我通过简单的测试:

 Foo foo = new Foo();

和 log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
</root>

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>


</log4net>

我添加到我的程序集信息中:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
"App.config", Watch = true)]

执行此代码后,事件日志中没有任何条目。这是为什么?

现在它几乎可以正常工作了:我删除了 log4net.config,并修改了我的 app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<root>
<priority value="ALL" />
<appender-ref ref="TraceAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender" />
<appender-ref ref="EventLogAppender" />
</root>

<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>

<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\\LOGS\\SampleLog.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
</layout>
</appender>
</log4net>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

</configuration>

但是有一点不对。我在输出中看到:

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
at System.Diagnostics.DiagnosticsConfiguration.Initialize()
at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
at System.Diagnostics.TraceInternal.InitializeSettings()
at System.Diagnostics.TraceInternal.WriteLine(String message)
at System.Diagnostics.Trace.WriteLine(String message)
at log4net.Util.LogLog.EmitErrorLine(String message)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.get_AppSettings()
at log4net.Util.SystemInfo.GetAppSetting(String key)

foreach 附加程序。

最佳答案

我假设您有一个控制台程序(看起来您正在测试 log4net)。所以我要做的第一件事是确保日志记录与更基本的附加程序一起工作。配置 ConsoleAppender 并确保您看到日志语句。这并不是绝对必要的,但正如我所说,这就是我会做的。

更重要的是打开internal debugging这应该会告诉您为什么没有任何内容写入事件日志。

很可能您遇到了权限问题。比照。 log4net faq .

关于c# - 为什么我在使用 log4net 时得到一个空的事件日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3467681/

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