1.log4netsql
1)新建一個Net空白項目,在引用出點擊管理NuGet程序包,搜索log4net並安裝app
2)創建log4net.config配置文件spa
在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入口code
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
4)添加一個button,在click項目裏面添加調用日誌代碼xml
int i = 1, j = 0; try { int result = i / j; } catch(Exception ex) { log.Error("出錯了!", ex); }
2.封裝LogHelper(和上面無關,這個是封裝一個LogHelper,包括配置)blog
添加Nuget包log4netutf-8
新建一個標準庫LogHelperget
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);