我开始使用log4net,我正在做一个小的控制台项目,在这个项目中我必须实现这个框架。
一开始,我创建了一个小的控制台项目,看看它是如何在没有任何其他代码的情况下工作的。我设法让它正常工作。
现在我尝试迁移我的应用程序中的所有代码,当我执行控制台应用程序的.exe时,我得到了这个错误:
"ERROR failed to find configuration section "log4net" in the application's .config
file.Check your .config file for the <log4net> and <configSections> elements. The
configuration section should look like : <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler.log4net"/>代码:
public class Program
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
ILog log = log4net.LogManager.GetLogger(typeof(Program));
...app.config还不存在,所以它的内容与我所做的测试项目相同:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="D:\WEB\SAI\log\nas\log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>在网上搜索了几次后,我补充道:
[assembly: log4net.Config.XmlConfigurator()]添加到我的AssemblyInfo.cs,但结果仍然是一样的…提前感谢您的帮助
发布于 2018-07-11 13:46:48
在AsseblyInfo.cs中添加程序集: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch = true)
https://stackoverflow.com/questions/26975907
复制相似问题