标签:append pen enter object lin appendto 数据库 layout logs
1、在webconfig里添加配置
1.1 在<configSections> 中添加节点
1 <configSections> 2 <configSections> 3 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> 4 </configSections> 5 </configSections>
1.2 添加log4net配置section
<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>
2、在Global.asax.cs里添加
log4net.Config.XmlConfigurator.Configure();
3、添加 helper类
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace TestLog4Net { public class log4netHelper { 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); } } }
4、代码里使用
ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//获取一个日志记录器 log4netHelper.Fatal(this.GetType(), "Fatal"); //致命错误 log4netHelper.Error(this.GetType(), "Error", new Exception("error")); //错误 log4netHelper.Warn(this.GetType(), "Warn"); //禁告 log4netHelper.Info(this.GetType(), "Info"); //信息 log4netHelper.Debug(this.GetType(), "Debug"); //调试
FATAL(致命错误):记录系统中出现的能使用系统完全失去功能,服务停止,系统崩溃等使系统无法继续运行下去的错误。例如,数据库无法连接,系统出现死循环。
ERROR(一般错误):记录系统中出现的导致系统不稳定,部分功能出现混乱或部分功能失效一类的错误。例如,数据字段为空,数据操作不可完成,操作出现异常等。
WARN(警告):记录系统中不影响系统继续运行,但不符合系统运行正常条件,有可能引起系统错误的信息。例如,记录内容为空,数据内容不正确等。
INFO(一般信息):记录系统运行中应该让用户知道的基本信息。
DEBUG (调试信息):记录系统用于调试的一切信息,内容或者是一些关键数据内容的输出。
1、
标签:append pen enter object lin appendto 数据库 layout logs
原文地址:https://www.cnblogs.com/i-mengli/p/9523440.html