標籤:lin readline ogr 滾動 root ref 前台 hint elm
log4net 有四種主要的組件,分別是Logger(記錄器), Repository(庫), Appender(附著器)以及 Layout(布局).第一步:Log4net的安裝
Install-Package log4net
第二步:Log4net的配置
log4net.config
注意:將log4net.config的屬性“複製到輸出目錄”設定為“始終複製”
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日誌路徑--> <param name= "File" value= "D:\App_Log\"/> <!--是否是向檔案中追加日誌--> <param name= "AppendToFile" value= "true"/> <!--log保留天數--> <param name= "MaxSizeRollBackups" value= "10"/> <!--記錄檔名是否是固定不變的--> <param name= "StaticLogFileName" value= "false"/> <!--記錄檔名格式為:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd".log""/> <!--日誌根據日期滾動--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" /> </layout> </appender> <!-- 控制台前台顯示日誌 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="Info" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <root> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net></configuration>
第三步:調用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using log4net;using log4net.Config;namespace Log4net{ class Program { static void Main(string[] args) { InitLog4Net(); var logger = LogManager.GetLogger(typeof(Program)); logger.Info("訊息"); logger.Warn("警告"); logger.Error("異常"); logger.Fatal("錯誤"); Console.ReadLine(); } private static void InitLog4Net() { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); } }}
Log4net_簡單使用