log4net配置
.NET技术交流群 199281001 .欢迎加入。
1.配置类
1 public class Log4NetConfig 2 { 3 public static string SqlServer = "ADONetAppender_SqlServer"; 4 public static string File = "FileAppender"; 5 }
2.注册类
1 //Author:GaoBing 2 public class Log 3 { 4 5 //获取实例 6 private static log4net.ILog myLogger = log4net.LogManager.GetLogger(Log4NetConfig.SqlServer); 7 8 //错误级别:Info 9 public static void Info(string message) 10 { 11 InitialLog4Net(); 12 myLogger.Info(message); 13 } 14 //错误级别:Debug 15 public static void Debug(string message) 16 { 17 InitialLog4Net(); 18 myLogger.Debug(message); 19 } 20 //错误级别:Warn 21 public static void Warn(string message) 22 { 23 InitialLog4Net(); 24 myLogger.Warn(message); 25 } 26 //错误级别:Fatal 27 public static void Fatal(string message) 28 { 29 InitialLog4Net(); 30 myLogger.Fatal(message); 31 } 32 //错误级别:Error 33 public static void Error(string message) 34 { 35 InitialLog4Net(); 36 myLogger.Error(message); 37 } 38 39 private static void InitialLog4Net() 40 { 41 //注册log4net日志记录组件 42 log4net.Config.XmlConfigurator.Configure( 43 new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config")); 44 }
3.log4net.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 4 <!-- Author:GaoBingBing--> 5 <configSections> 6 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> 7 </configSections> 8 9 <log4net> 10 11 <!--写入到数据库--> 12 <appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender"> 13 <!--错误队列数据达到10个才持久化到数据库--> 14 <bufferSize value="10" /> 15 <connectionType value="System.Data.SqlClient.SqlConnection" /> 16 <connectionString value="Server=youserver;UID=sa;PWD=******;DataBase=DB_Logs;Max Pool Size=1000"/> 17 <commandText value="INSERT INTO Logs([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)"/> 18 <parameter> 19 <parameterName value="@log_date"/> 20 <dbType value="DateTime"/> 21 22 <layout type="log4net.Layout.PatternLayout"> 23 <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" /> 24 </layout> 25 <!-- <layout type="log4net.Layout.RawTimeStampLayout"/>--> 26 </parameter> 27 <parameter> 28 <parameterName value="@thread"/> 29 <dbType value="String"/> 30 <size value="255"/> 31 <layout type="log4net.Layout.PatternLayout"> 32 <conversionPattern value="%thread"/> 33 </layout> 34 </parameter> 35 <parameter> 36 <parameterName value="@log_level"/> 37 <dbType value="String"/> 38 <size value="50"/> 39 <layout type="log4net.Layout.PatternLayout"> 40 <conversionPattern value="%level"/> 41 </layout> 42 </parameter> 43 <parameter> 44 <parameterName value="@logger"/> 45 <dbType value="String"/> 46 <size value="255"/> 47 <layout type="log4net.Layout.PatternLayout"> 48 <conversionPattern value="%logger"/> 49 </layout> 50 </parameter> 51 <parameter> 52 <parameterName value="@message"/> 53 <dbType value="String"/> 54 <size value="4000"/> 55 <layout type="log4net.Layout.PatternLayout"> 56 <conversionPattern value="%message"/> 57 </layout> 58 </parameter> 59 </appender> 60 <root> 61 <level value="ALL"/> 62 <appender-ref ref="ADONetAppender_SqlServer"/> 63 64 </root> 65 <!-- Specify the level for some specific categories --> 66 <logger name="iNotes"> 67 <!-- <appender-ref ref="B" /> --> 68 <level value="ALL"/> 69 <appender-ref ref="ADONetAppender_SqlServer"/> 70 71 </logger> 72 73 </log4net> 74 </configuration>
4.调用
Log.Error("you message");