1.log4net
1)新建一个Net空白项目,在引用出点击管理NuGet程序包,搜索log4net并安装
2)建立log4net.config配置文件
在configuration里面添加如下代码,级别为All,log的所有消息都可以调用
<!--配置小节——版本声名--> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-2.0.8.0"/> </configSections> <log4net> <!--记录器,程序入口--> <logger name="logFile"> <level value="All"/> <appender-ref ref="LogFileAppender" /> </logger> <!--附属配置--> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="./Log/log.txt" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n========== %n【日志级别】%-5level %n【记录时间】%date %n【线程编号】[%thread] %n【执行时间】[%r]毫秒 %n【出错文件】%F %n【出错行号】%L %n【出错的类】%logger 属性[%property{NDC}] %n【错误描述】%message %n【错误详情】%newline"/> </layout> </appender> </log4net>
并且右键属性,设置 复制到输出目录 为 如果较新则复制
3)在AssemblyInfo添加config入口
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
4)添加一个button,在click项目里面添加调用日志代码
int i = 1, j = 0; try { int result = i / j; } catch(Exception ex) { log.Error("出错了!", ex); }
2.封装LogHelper(和上面无关,这个是封装一个LogHelper,包括配置)
添加Nuget包log4net
新建一个标准库LogHelper
using log4net; using log4net.Config; using log4net.Repository; using System; using System.IO; [assembly: XmlConfigurator(Watch = true)] namespace LogHelper { public class Log4Helper { private static ILog logger = null; public static void LogInfo(string logstr) { if (logger == null) InitLog4net(); logger.Info(logstr); } public static void LogError(string logstr, Exception ex = null) { if (logger == null) InitLog4net(); logger.Error(logstr); if (ex != null) logger.Error(ex.ToString()); } private static object objlock = new object();//初始化log用的锁 private static void InitLog4net() { lock (objlock) { ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository"); //获取配置文件路径 var conpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"); XmlConfigurator.Configure(repository, new FileInfo(conpath)); logger = LogManager.GetLogger(repository.Name, "NETCorelog4net"); } } } }
在类库添加log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <root> <level value="INFO" /> <appender-ref ref="infoAppender" /> <appender-ref ref="errorAppender" /> <appender-ref ref="sqlAppender" /> </root> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <staticLogFileName value="false" /> <datePattern value="'error'yyyyMMdd'.log'" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="1MB" /> <threshold value="ERROR" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline--------%date--------%newline%message" /> </layout> </appender> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <staticLogFileName value="false" /> <datePattern value="'info'yyyyMMdd'.log'" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="1MB" /> <threshold value="INFO" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline--------%date--------%newline%message" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Info" /> </filter> </appender> <!-- Setup the root category, add the appenders and set the default level --> </log4net> </configuration>
打包,其他项目引用这个包,会自动生成log4net.config,设置属性 复制到输出目录->如果较新则复制
使用起来也很简单
Log4Helper.LogInfo("进来了"); Log4Helper.LogError("error"); Log4Helper.LogError("error", ex);