标签:struct filename dmi cat value col 管理系统 ring 任务管理
接着上一章继续唠
这一章主要说一下
另外群里有人说serilog是最好用的,这个我也用过,感觉就那样吧,以前还自己写log
一.基础配置类
infrastructure下直接一个Log4Net配置文件
代码如下:
/// <summary> /// 日志服务 /// </summary> public class Log4Net { /// <summary> /// 定义日志容器 /// </summary> private static ILoggerRepository _repository = LogManager.CreateRepository(TbConstant.Log4RepositoryKey); /// <summary> /// 定义日志配置文件 /// </summary> private static FileInfo LogConfig = new FileInfo(ConfigLocator.Instance[TbConstant.Log4netKey]); /// <summary> /// 定义接口参数 /// </summary> /// <returns></returns> private static ILog SetLog() { var _MethodName = string.Empty; try { StackFrame _Call = new StackFrame(2); _MethodName = string.Format("{0}.{1}", _Call.GetMethod().ReflectedType.FullName, _Call.GetMethod().Name); var _St = new StackTrace().GetFrames(); foreach (var _item in _St) { if (_item.GetMethod().DeclaringType.ToString().EndsWith("Exception") && _St.Length > 2) { _MethodName = string.Format("{0}.{1}", _item.GetMethod().ReflectedType.FullName, _item.GetMethod().Name); continue; } } } catch { } XmlConfigurator.Configure(_repository, LogConfig); return LogManager.GetLogger(_repository.Name, _MethodName); } #region 异常日志 /// <summary> /// 异常日志 /// </summary> /// <param name="msg">错误信息</param> public static void Debug(object msg) { SetLog().Debug(msg); } /// <summary> /// 异常日志 /// </summary> /// <param name="msg">错误信息</param> /// <param name="ex">异常信息</param> public static void Debug(object msg, Exception ex) { SetLog().Debug(msg, ex); } #endregion #region 错误日志 /// <summary> /// 错误日志 /// </summary> /// <param name="msg">错误信息</param> public static void Error(object msg) { SetLog().Error(msg); } /// <summary> /// 错误日志 /// </summary> /// <param name="msg">错误信息</param> /// <param name="ex">异常信息</param> public static void Error(object msg, Exception ex) { SetLog().Error(msg, ex); } #endregion #region 数据日志 /// <summary> /// 数据日志 /// </summary> /// <param name="msg">错误信息</param> public static void Info(object msg) { SetLog().Info(msg); } /// <summary> /// 数据日志 /// </summary> /// <param name="msg">错误信息</param> /// <param name="ex">异常信息</param> public static void Info(object msg, Exception ex) { SetLog().Info(msg, ex); } #endregion #region 警告日志 /// <summary> /// 警告日志 /// </summary> /// <param name="msg">错误信息</param> public static void Warn(object msg) { SetLog().Warn(msg); } /// <summary> /// 警告日志 /// </summary> /// <param name="msg">错误信息</param> /// <param name="ex">异常信息</param> public static void Warn(object msg, Exception ex) { SetLog().Warn(msg, ex); } #endregion }
主要说一下这两句
/// <summary> /// 定义日志容器 /// </summary> private static ILoggerRepository _repository = LogManager.CreateRepository(TbConstant.Log4RepositoryKey); /// <summary> /// 定义日志配置文件 /// </summary> private static FileInfo LogConfig = new FileInfo(ConfigLocator.Instance[TbConstant.Log4netKey]);
CreateRepository里传入自定义key
第二句传入log4net配置文件路径
如图:
代码如下:配置还是老配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <!--错误日志--> <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="Log/Error/" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.‘error.log‘" /> <param name="staticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%n%n记录时间:%date %n日志级别: %-5level %n类及方法:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> <!--警告日志--> <appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="Log/Warn/" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.‘warn.log‘" /> <param name="staticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%n%n记录时间:%date %n日志级别: %-5level %n类及方法:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN" /> <param name="LevelMax" value="WARN" /> </filter> </appender> <!--数据日志--> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="Log/Info/" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.‘Info.log‘" /> <param name="staticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%n%n记录时间:%date %n日志级别: %-5level %n类及方法:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <!--异常日志--> <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="Log/Debug/" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.‘debug.log‘" /> <param name="staticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%n%n记录时间:%date %n日志级别: %-5level %n类及方法:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <root> <level value="DEBUG" /> <!--文件形式记录日志--> <appender-ref ref="ErrorRollingFileAppender" /> <appender-ref ref="DebugRollingFileAppender" /> <appender-ref ref="InfoRollingFileAppender" /> <appender-ref ref="WarnRollingFileAppender" /> </root> </log4net> </configuration>
二.测试结果
在你想记录日至的地方,直接用Log4Net点他的四种日志类型,Info,Error,Warn,Debug
他会在你项目根目录下生成Log文件夹,并包含四种类型日志,按日期划分
日志内容如下:
日志记录这块我就讲到这里
另外今天我对这个项目加了quartz计划任务管理模块,还加了报表导出
这个内容就下一章唠吧
下章内容
[外包]!采用asp.net core 快速构建小型创业公司后台管理系统(四.Log4Net的简单配置)
标签:struct filename dmi cat value col 管理系统 ring 任务管理
原文地址:https://www.cnblogs.com/gdsblog/p/10009985.html