log4net配置文件
log4net配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <appSettings> </appSettings> <log4net> <!-- 控制台前台显示日志 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Green" /> </mapping> <mapping> <level value="INFO" /> <backColor 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> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file type="log4net.Util.PatternString" value="logs\App-%date{yyyy-MM-dd_HH-mm-ss}.log" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒 %n【调试文件】%F %n【调试行号】%L %n【调试的类】%logger 属性[%property{NDC}] %n【调试描述】%message %n【调试详情】%Method%newline %n" /> </layout> </appender> <!--定义输出到文件中--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\error\" /> <!--多线程时采用最小锁定--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <Encoding value="UTF-8" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <param name="StaticLogFileName" value="false"/> <param name="MaxSizeRollBackups" value="10" /> <maximumFileSize value="500kb" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒 %n【调试文件】%F %n【调试行号】%L %n【调试的类】%logger 属性[%property{NDC}] %n【调试描述】%message %n【调试详情】%Method%newline %n" /> </layout> <!--日志等级过滤规则,不在这个等级范围内的日志不记录--> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="DEBUG"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> </appender> <!--定义输出到文件中--> <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\debug\"/> <appendToFile value="true" /> <!--<rollingStyle value="Composite" />--> <rollingStyle value="Composite"/> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd.'txt'" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="500kb" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒1 %n【调试文件】%F %n【调试行号】%L %n【调试的类】%logger 属性[%property{NDC}] %n【调试描述】%message %n【调试详情】%Method%newline %n" /> </layout> <!--日志等级过滤规则,不在这个等级范围内的日志不记录--> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="DEBUG"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> </appender> <!--指定系统日志--> <appender name="InfoAppender" type="log4net.Appender.EventLogAppender,log4net"> <logName value="人员机构数据同步" /> <!--事件日志中的来源--> <applicationName value="人员机构数据同步A" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒 %n【文件】%F %n【行号】%L %n【类】%c 属性[%property{NDC}] %n【描述】%message %n【详情】%M%newline" /> </layout> <!--日志等级过滤规则,不在这个等级范围内的日志不记录--> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> </appender> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\error\" /> <!--多线程时采用最小锁定--> <!--记录日志写入文件时,不锁定文本文件,防止多线程时不能写Log,官方说线程非安全--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <Encoding value="UTF-8" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <!--是否只写到一个文件中--> <param name="StaticLogFileName" value="false"/> <!--最多产生的日志文件数,超过则只保留最新的n个。设定值value="-1"为不限文件数--> <param name="MaxSizeRollBackups" value="10" /> <maximumFileSize value="50kb" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒 %n【记录位置】%location %n【出错文件】%F %n【出错行号】%L %n【出错的类】%logger 属性[%property{NDC}] %n【错误描述】%message %n【错误详情】%method%newline %n" /> </layout> <!--日志等级过滤规则,不在这个等级范围内的日志不记录--> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="ERROR"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> </appender> <!--默认系统日志--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=" ========== %n【记录时间】%date %n【线程编号】[%thread] %n【日志级别】%-5level %n【执行时间】[%r]毫秒 %n【出错文件】%F %n【出错行号】%L %n【出错的类】%logger 属性[%property{NDC}] %n【错误描述】%message %n【错误详情】%method%newline" /> </layout> </appender> <!--记录到数据库--> <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender"> <!--存储到数据库的操作--> <bufferSize value="1"/> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <connectionString value="Data Source=CHENZH\SQL2016;Initial catalog=bizDB;Integrated Security=False;persist security info=True;User ID=sa;Password=123456"/> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @log_exception)"/> <parameter> <parameterName value="@log_date"/> <dbType value="DateTime"/> <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}"/> </parameter> <parameter> <parameterName value="@thread"/> <dbType value="String"/> <size value="255"/> <layout type="log4net.Layout.PatternLayout" value="%thread"/> </parameter> <parameter> <parameterName value="@log_level"/> <dbType value="String"/> <size value="50"/> <layout type="log4net.Layout.PatternLayout" value="%level"/> </parameter> <parameter> <parameterName value="@logger"/> <dbType value="String"/> <size value="255"/> <layout type="log4net.Layout.PatternLayout" value="%logger"/> </parameter> <parameter> <parameterName value="@message"/> <dbType value="String"/> <size value="4000"/> <layout type="log4net.Layout.PatternLayout" value="%message"/> </parameter> <parameter> <!--异常信息--> <parameterName value="@log_exception"/> <dbType value="String"/> <size value="255"/> <layout type="log4net.Layout.ExceptionLayout"/> </parameter> <!--日志等级过滤规则,不在这个等级范围内的日志不记录--> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <logger name="Debug" additivity="false"> <level value="DEBUG" /> <appender-ref ref="DebugAppender" /> </logger> <logger name="Info"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> <appender-ref ref="ColoredConsoleAppender" /> <appender-ref ref="ADONetAppender" /> </logger> <logger name="Error"> <level value="ERROR" /> <appender-ref ref="ErrorAppender" /> </logger> <logger name="Fatal"> <level value="FATAL" /> <!--appender-ref有多个说明可以多种方式记录日志,具体能不能写日志还要看每个appender-ref下的配置的过滤规则--> <appender-ref ref="InfoAppender" /> <appender-ref ref="ColoredConsoleAppender" /> </logger> <logger name="Warn"> <level value="WARN" /> <appender-ref ref="ErrorAppender" /> </logger> <!--OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL--> <root> <!--有了上面的配置下面的这些配置可以不用配了 <logger name="Warn"> <level value="WARN" /> <appender-ref ref="ErrorAppender" /> </logger> --> <!--<level value="ALL"/> <appender-ref ref="DebugAppender"/> <appender-ref ref="InfoAppender"/> <appender-ref ref="ErrorAppender"/> <appender-ref ref="AdoNetAppender"/> <appender-ref ref="AdoNetAppender_SqlServer1"/> <appender-ref ref="InfoAppender"/> <appender-ref ref="ErrorAppender"/>--> </root> </log4net> </configuration>