應用場景:apache
控制軟件工程拆分爲多個子項目,在開發階段但願每一個子項目的日誌是單獨的。同時又但願日誌統一集中定義在Tools(Tools爲工具類項目,生成Tools.dll)下,而不是分散在各個子項目中。這就須要解決如下幾個問題:app
如何在DLL方案中配置Log4net;如何分類Logger;如何在外部使用Logger。工具
1.在DLL中使用App.config配置Log4net單元測試
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net name="DefaultLogger"> <root> <level value="ALL"/> </root> <logger name="Doraemon.Hardware"> <appender-ref ref="HardwareLogger"/> </logger> <logger name="Doraemon.UI"> <appender-ref ref="UILogger"/> </logger> <!--硬件日誌--> <appender name="HardwareLogger" type="log4net.Appender.RollingFileAppender"> <file value="log/hardware.log"/> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="20"/> <maximumFileSize value="256KB"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date #[%thread]#%-5level #%logger #[%property{NDC}] - #%message%newline"/> </layout> </appender> <!--UI & Workflow日誌--> <appender name="UILogger" type="log4net.Appender.RollingFileAppender"> <file value="log/ui.log"/> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="20"/> <maximumFileSize value="256KB"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date #[%thread] #%-5level #%logger #[%property{NDC}] - #%message%newline"/> </layout> </appender> </log4net>
</configuration>
public static class Logger { public static ILog DefaultLogger { get; } public static ILog HardwareLogger { get; } static Logger() { DefaultLogger = LogManager.GetLogger(@"Doraemon.Default"); HardwareLogger = LogManager.GetLogger("Doraemon.Hardware"); } }
4.單元測試測試
[TestMethod] public void LoggerTest() { var loggerCollect = new List<ILog>() { DefaultLogger, HardwareLogger, }; var select = loggerCollect.FindAll((p) => p.IsDebugEnabled == p.IsErrorEnabled == p.IsFatalEnabled == p.IsInfoEnabled == p.IsWarnEnabled); Assert.AreEqual(loggerCollect.Count, select.Count, "日誌對象初始化存在問題"); }
ConfigManager.Net - App.config and Web.config helper utility, http://www.codeproject.com/Articles/26544/ConfigManager-Net-App-config-and-Web-config-helper
Log4net, http://logging.apache.org/log4net/download_log4net.cgi
2016-05-03 更新:ui
若是不想將Log4net的配置文件集成在工程.config中,能夠將它以XML格式存在在外部文件中。這樣作的好處是隨時能夠自定義格式、自定義目標。配置到外部XML文件並無什麼不一樣,與配置在.config中及其類似,如下文件存儲爲log.config.xml.spa
<?xml version="1.0" encoding="utf-8" ?> <log4net> <logger name="Doraemon.Console"> <appender-ref ref="Console"/> </logger> <logger name="Doraemon.UI"> <appender-ref ref="RollingFile"/> </logger> <appender name="Console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" /> </layout> </appender> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="example.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level %thread %logger - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> </root> </log4net>
在代碼中訪問也沒有什麼不一樣:)線程
class Program { private static readonly ILog log = LogManager.GetLogger(@"Doraemon.Console");//@"Doraemon.UI"); begins by importing log4net related classes. static void Main(string[] args) { // Set up a simple configuration that logs on the console. XmlConfigurator.Configure(new System.IO.FileInfo(@"log.config.xml")); //使用正確的配置讀取類別 log.Info("Entering application."); var i = 0; while (i++ < 10) log.Info($"i = {i}"); log.Debug("Exiting application."); Console.ReadLine(); } }
2016-12-15 更新: 格式串日誌
%m(message):輸出的日誌消息,如ILog.Debug(…)輸出的一條消息 %n(new line):換行 %d(datetime):輸出當前語句運行的時刻 %r(run time):輸出程序從運行到執行到當前語句時消耗的毫秒數 %t(thread id):當前語句所在的線程ID %p(priority): 日誌的當前優先級別,即DEBUG、INFO、WARN…等 %c(class):當前日誌對象的名稱,例如: %L:輸出語句所在的行號 %F:輸出語句所在的文件名 %-數字:表示該項的最小長度,若是不夠,則用空格填充