引言
log4net庫是Apache log4j框架在Micorsoft.NET平臺的實現,是一個幫組程序員將日志信息輸出到各種目標(控制臺、文件、數據庫等)的工具。(百度百科)
實際項目中使用log4net極大的方便程序猿記錄系統運行過程中的日志信息,特別是對bs系統說是一個比較實用的工具。本文簡單解釋它的使用過程,都是最基本的最簡單的運用,沒有其他多余的解釋只是簡單使用。
使用步驟:
步驟一:
在項目文件中創建一個類庫,本例使用類庫名:Log4NetUtility。引用log4net庫的dll,添加配置文件log4net.cfg.xml,注意將該文件的屬性設置為始終復制。配置文件都是一些固定的格式,可以做一些簡單的改動。基本內容如下:
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <!--日志記錄組建配置--> <log4net debug="true"> <!-- 調試日志配置 --> <appender name="DebugAppender" type="log4net.Appender.DebugAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date][%thread][%-5level][%c] - %message%newline" /> </layout> </appender> <!-- 文件日志配置 --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <appendToFile value="true" /> <rollingStyle value="Date" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="-1" /> <datePattern value="yyyy-MM-dd_HH'.log'" /> <lockingModel value="log4net.Appender.FileAppender.MinimalLock" /> <file value="Log//" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date][%thread][%-5level]%message%newline" /> </layout> </appender> <!-- Setup the root category, add the appenders and set the default PRiority --> <root> <level value="ALL" /> <appender-ref ref="DebugAppender" /> </root> <logger name="FileLogLogger" additivity="false"> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> </logger> </log4net></configuration>
步驟二:
編寫日志方法,如下:
public class Log4NetUtility { private static ILog fileLogger = LogManager.GetLogger("FileLogLogger"); private static ILog debugLogger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); static Log4NetUtility() { string binPath = System.AppDomain.CurrentDomain.BaseDirectory; string webFfileName = binPath + @"bin/Config/log4net.cfg.xml"; string appFileName = binPath + @"Config/log4net.cfg.xml"; if (File.Exists(webFfileName)) log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(webFfileName)); else if (File.Exists(appFileName)) log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(appFileName)); else Console.WriteLine("找不到Log4net配置文件"); } public static void DebugLog(String message) { debugLogger.Debug(message); } public static void Debug(object o, string message, Exception exception) { Type type = o.GetType(); fileLogger.Debug(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception); } public static void Debug(object o, String message) { Type type = o.GetType(); fileLogger.Debug(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message)); } public static void Debug(String typeName, String message, Exception exception) { fileLogger.Debug(string.Format("[{0}] - {1}", typeName, message), exception); } public static void Debug(String typeName, String message) { fileLogger.Debug(string.Format("[{0}] - {1}", typeName, message)); } public static void Info(object o, String message, Exception exception) { Type type = o.GetType(); fileLogger.Info(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception); } public static void Info(object o, String message) { Type type = o.GetType(); fileLogger.Info(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message)); } public static void Info(string typeName, String message, Exception exception) { fileLogger.Info(string.Format("[{0}] - {1}", typeName, message), exception); } public static void Info(string typeName, String message) { fileLogger.Info(string.Format("[{0}] - {1}", typeName, message)); } public static void Warn(object o, String message, Exception exception) { Type type = o.GetType(); fileLogger.Warn(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception); } public static void Warn(object o, String message) { Type type = o.GetType(); fileLogger.Warn(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message)); } public static void Warn(string typeName, String message, Exception exception) { fileLogger.Warn(string.Format("[{0}] - {1}", typeName, message), exception); } public static void Warn(string typeName, String message) { fileLogger.Warn(string.Format("[{0}] - {1}", typeName, message)); } public static void Error(object o, String message, Exception exception) { Type type = o.GetType(); fileLogger.Error(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception); } public static void Error(object o, String message) { Type type = o.GetType(); fileLogger.Error(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message)); } public static void Error(string typeName, String message, Exception exception) { fileLogger.Error(string.Format("[{0}] - {1}", typeName, message), exception); } public static void Error(string typeName, String message) { fileLogger.Error(string.Format("[{0}] - {1}", typeName, message)); } public static void Fatal(object o, String message, Exception exception) { Type type = o.GetType(); fileLogger.Fatal(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message), exception); } public static void Fatal(object o, String message) { Type type = o.GetType(); fileLogger.Fatal(string.Format("[{0}.{1}] - {2}", type.Namespace, type.Name, message)); } public static void Fatal(string typeName, String message, Exception exception) { fileLogger.Fatal(string.Format("[{0}] - {1}", typeName, message), exception); } public static void Fatal(string typeName, String message) { fileLogger.Fatal(string.Format("[{0}] - {1}", typeName, message)); } }
步驟三:
系統內進行調用:
Log4NetUtility.Log4NetUtility .Error(this, "log4net日志錯誤記錄");
至此,log4net的簡單應用就介紹完了,本文還是那句話,就是介紹它的一些簡單用法,告訴你如何使用它,具體其內部的原理還有更深層的東西還是交給以后吧。
新聞熱點
疑難解答