1、使用ILog对象记录日志
var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log.Warn("error", new Exception("异常!"));
前提:
1)引用log4net.dll
2)命名空间前加:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
3)App.config的configuration节点下的第一个节点必须为configSection,为:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
4)App.config的configuration节点下的增加:
<log4net>
<root>
<appender-ref ref="UdpAppender"/>
</root>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="7071" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>
</log4net>
其中root中的UdpAppender必须与appender的name一致。“127.0.0.1”不能指定为“localhost”,可以指定为本机的IP。
2、打开log2console程序
增加一个Receiver,指定为UDP,其它的默认即可。
当记录日志的时候,log2console就会显示出日志了。
如果没有显示日志,删除当前的Receiver,重新添加一个。
该Receiver的端口应该与配置中的一致,这里都是7071。
3、下载地址
log2console bin(从源码生成,修复在一个中文环境下的bug,来自网络)