lac*_*dev 49 c# logging log4net
我想在我的application.config文件中存储log4net配置数据.根据我对文档的理解,我做了以下工作:
添加对log4net.dll的引用
在AssemblyInfo.cs中添加以下行:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Run Code Online (Sandbox Code Playgroud)按如下方式初始化记录器:
private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));
Run Code Online (Sandbox Code Playgroud)我的app.config中有以下代码:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该应用程序时,我在控制台上收到以下错误:
没有找到名为[Consoleappender]的追加器.
如何让log4net从配置文件中读取设置?
谢谢!
Cha*_*ana 38
在configSections元素中的app.config中添加一行
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0,
Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
然后添加log4Net部分,但委托给其他地方的实际log4Net配置文件......
<log4net configSource="Config\Log4Net.config" />
Run Code Online (Sandbox Code Playgroud)
在您的应用程序代码中,当您创建日志时,请写入
private static ILog GetLog(string logName)
{
ILog log = LogManager.GetLogger(logName);
return log;
}
Run Code Online (Sandbox Code Playgroud)
Kon*_*tin 35
从问题中显示的配置中只配置了一个appender,它被命名为"EventLogAppender".但是在root的配置中,作者引用了名为"ConsoleAppender"的appender,因此出现了错误消息.
我完全支持@Charles Bretana 的回答。但是,如果它不起作用,请确保只有一个<section>元素并且configSections是根元素的第一个子元素:
configsections必须是后配置中的第一个元素app.Config:
<?xml version="1.0"\xc2\xa0encoding="utf-8"?>\n<configuration>\n <configSections>\n <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />\n </configSections>\n <!-- add log 4 net config !-->\n <!-- add others e.g. <startup> !-->\n</configuration>\nRun Code Online (Sandbox Code Playgroud)\n