我有以下配置:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE}
[%thread] %level %logger - %message%newline"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Log.txt" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE}
[%thread] %level %logger - %message%newline" />
</layout>
<encoding value="utf-8" />
</appender>
<logger name="TraceLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="ConsoleAppender" />
</logger>
</log4net>
我在XmlConfigurator上运行了配置:
static class ProgramEntry
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
...
我的web.config有:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>
...
<log4net configSource="Config\log4net.config" />
...
<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<add name="Log4NetTraceListener" type="BLAH.Log4netTraceListener, BLAH" />
</listeners>
</trace>
</system.diagnostics>
我有一个Trace侦听器实现:
public class Log4netTraceListener : System.Diagnostics.TraceListener
{
private readonly ILog log;
public Log4netTraceListener()
{
log = LogManager.GetLogger("TraceLogger");
// disable logging to trace write if logging fails, or else we'll end up in a deadlock.
log4net.Util.LogLog.EmitInternalMessages = false;
}
private string level = "Info";
public override void Write(string message)
{
if (message.Contains("Error:"))
{
level = "Error";
}
else if (message.Contains("Warning:"))
{
level = "Warning";
}
else if (message.Contains("Information:"))
{
level = "Info";
}
else
{
if (log != null)
{
LogMessage(message);
}
}
}
public override void WriteLine(string message)
{
if (log != null)
{
LogMessage(message);
}
// reset the level
level = "Info";
}
private void LogMessage(string message)
{
if (level == "Info")
{
log.Info(message);
}
if (level == "Error")
{
log.Error(message);
}
if (level == "Warning")
{
log.Warn(message);
}
}
}
跟踪侦听器运行,似乎找到“TraceLogger”,但其appenders集合为空。
我可能做错了什么?真正的问题是
修改
发现错误:
log4net:ERROR无法解析配置文件。是个 指定为: System.Configuration.ConfigurationErrorsException:无法打开 configSource文件'Config \ log4net.config'。 (C:\ Users \ awdawd \ Documents \ Visual Studio 2015年\项目\ BLAH \ BLAH \ BLAH \ BIN \调试\ DataFactoryPreparationManager.exe.Config 第36行
我的app.config位于根目录中,我的Config文件夹位于此目录下
答案 0 :(得分:1)
确保通过&#39;复制到输出目录&#39;将log4net.config
复制到bin\Debug\Config
。 Visual Studio中的文件属性。
在解决方案资源管理器中右键单击该文件,然后选择“属性”。