Log4net 控制台打印日志(二)

1.创建控制台程序

2.用NuGet添加log4net引用

3.添加应用程序配置文件:App.config

4.添加配置信息:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--添加配置节点-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections> <log4net>
<!--日志保存到文件里面 txt-->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red,HighIntensity" />
</mapping> <mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping> <mapping>
<level value="Info" />
<backColor value="Yellow" />
</mapping> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout> <filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Debug"/>
<param name="LevelMax" value="Fatal"/>
</filter> </appender>
<root>
<!--配置可输出日志级别-->
<level value="all"/>
<!--应用配置-->
<appender-ref ref="ColoredConsoleAppender"/>
</root>
</log4net> </configuration>

5.程序中代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net; namespace 控制台输出日志
{
class Program
{ static void Main(string[] args)
{
//初始化log4net配置
log4net.Config.XmlConfigurator.Configure();
ILog log = LogManager.GetLogger(typeof(Program));
log.Info("普通日志");
log.Error("错误日志");
log.Debug("异常信息");
log.Fatal("致命错误");
log.Warn("警告信息"); Console.ReadKey();
}
}
}

6.展示:

Log4net 控制台打印日志(二)

上一篇:【JavaScript 插件】图片展示插件 PhotoSwipe 初识


下一篇:POJ2391 Ombrophobic Bovines(网络流)(拆点)