log4net无法输出日志
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4net无法输出日志相关的知识,希望对你有一定的参考价值。
配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--=====log4net必备的配置=====-->
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingLogFileAppender">
<!--=====设置日志为可追加=====-->
<AppenderToFile value="true"/>
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<LockingModel value="log4net.Appender.FileAppender.MinimalLock" />
<!--=====设置日志名称是否可更改,false为可修改=====-->
<StaticLogFileName value="false"/>
<!--=====日志输出位置=====-->
<File value ="FWIIILog'.bin'"/>
<!--=====日志筛选=====-->
<Filter type="log4net.Filter.LevelRangeFilter">
<levelMax value="FATAL"/>
<levelMin value="WARN"/>
</Filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="时间:%d%n线程ID:%t%n日志级别:%-5level%n出错文件名:%F%n出错类名:%c%n行号:%l%n出错描述:%m%n**********%n"/>
</layout>
</appender>
<root>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<logger>
<appender-ref ref="RollingLogFileAppender"/>
</logger>
</log4net>
</configuration>
调用代码:
ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
if (log.IsWarnEnabled)
log.Warn("要还原的备份路径无效");
C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Logger
public static bool GetTracerSwitchor()
string configPath = Application.StartupPath + "\\eSerialPro.exe";
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configPath);
bool IsTracer = Convert.ToBoolean(config.AppSettings.Settings["IsTracer"].Value);
return IsTracer;
/// <summary>
/// 调试信息
/// </summary>
/// <param name="msg"></param>
public static void Debug(string msg)
bool IsTracer = GetTracerSwitchor();
if (IsTracer)
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsDebugEnabled)
log.Debug(msg);
log = null;
这样调用
再在Assembly里加入信息
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
再在可执行程序的根目录下,写一个名为log4net.appconfig文件,内容如下
C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<!--
<level value="ALL" />
<appender-ref ref="rootFile" />
-->
</root>
<logger name="Test">
<level value="DEBUG" />
<appender-ref ref="rollingFile" />
</logger>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Application.log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------- " />
<param name="Footer" value=" ----------------------footer-------------- " />
</layout>
</appender>
</log4net>
</configuration> 参考技术B 在命名空间前面加上
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
试试
以上是关于log4net无法输出日志的主要内容,如果未能解决你的问题,请参考以下文章