log4net
1.使用Nuget下载log4net
2.、新建Config文件夹,将log4net配置文件放入此文件夹中(当修改web.config的时候会重启网站,所以把log4net单独出来比较好,也好管理),log4net配置:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<APPender name="fatalappender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\fatal.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ERRORAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\err.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="warnAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="WARN" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\warn.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\info.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\debug.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\perf.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="fatalAppender" />
<appender-ref ref="errorAppender" />
<appender-ref ref="warnAppender" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
</root>
</log4net>
-----------------------------------------------------------------------------------------------------------------------------
3.右键点击第2步骤的log4net文件属性,改成如图所示
4.
在Properties下的assemblyinfo.cs文件中加入
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]
log4帮助类方便使用,代码:
public static class Log4Helper
{
public static void Fatal(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Fatal(message);
else
log.Fatal(message, exception);
}
public static void Error(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Error(message);
else
log.Error(message, exception);
}
public static void Warn(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Warn(message);
else
log.Warn(message, exception);
}
public static void Info(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Info(message);
else
log.Info(message, exception);
}
public static void Debug(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Debug(message);
else
log.Debug(message, exception);
}
}
------------------------------------------------------------------------------------------------------
5、调用:
Log4Helper.Fatal(this.GetType(), "Fatal"); Log4Helper.Error(this.GetType(), "Error", new Exception("error")); Log4Helper.Warn(this.GetType(), "Warn"); Log4Helper.Info(this.GetType(), "Info"); Log4Helper.Debug(this.GetType(), "Debug");
Log4Net跨项目使用:
如果我们将log4net帮助类放在另一个公共的类库项目中,又需要在另一个引用的项目中使用log4helper类,
这时候仅需要在该项目里的Properties下的AssemblyInfo.cs文件中加入这句:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]
相关阅读
背景 使用 ping 命令可以进行网络监测,在进行一系列监测时,按照由近及远原则,首先执行的是____。 A. ping 默认网关 B. ping 本地
支付宝最近新上线了每日必抢的一个小程序,可以直接在支付宝里面进行拼团购买物品,这是支付宝的营销手段,支付宝和商家双赢,增加了用户
Java的时间类有Date类和Calendar类,其中Date类官方建议不再使用(理由是不利于国际化),取而代之的就是今天要讲的Calendar类 Ca
curses库是一组函数,程序员可以用它们来设置光标的位置和终端屏幕上显示的字符样式。curses库最初是由UCB的开发小组开发的。大部
使用加密狗进行程序的加密保护是非常成熟的加密方案。目前市面上比较常见的加密锁:深思的精锐5,威步,safenet。国内比较老牌的加密锁