log4net配置要點
阿新 • • 發佈:2017-05-15
-1 error ogg global .config config velt etc visit
1。log4net.config. 這個是最重要的配置,不用多說
<?xml version="1.0" encoding="utf-8" ?> <log4net> <!-- Define some output appenders --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\log.txt" /> <appendToFile value="true" /> <!--可以為:Once|Size|Date|Composite--> <!--Composite為Size和Date的組合--> <rollingStyle value="Composite" /> <!--日誌最大個數,都是最新的--> <!--rollingStyle節點為Date時,該節點不起作用--> <!--rollingStyle節點為Size時,只能有value個日誌--> <!--rollingStyle節點為Composite時,每天有value個日誌--> <maxSizeRollBackups value="-1" /> <!--當備份文件時,為文件名加的後綴--> <datePattern value=".yyyy-MM-dd" /> <!--日誌文件大小,可用的單位:KB|MB|GB--> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] "/> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <!--輸出級別--> <filter type="log4net.Filter.LevelRangeFilter"> <levelMax value="WARN" /> </filter> </appender> <appender name="RollingErrorLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\log\log_error.txt" /> <appendToFile value="true" /> <!--可以為:Once|Size|Date|Composite--> <!--Composite為Size和Date的組合--> <rollingStyle value="Composite" /> <!--日誌最大個數,都是最新的--> <!--rollingStyle節點為Date時,該節點不起作用--> <!--rollingStyle節點為Size時,只能有value個日誌--> <!--rollingStyle節點為Composite時,每天有value個日誌--> <maxSizeRollBackups value="-1" /> <!--當備份文件時,為文件名加的後綴--> <datePattern value=".yyyy-MM-dd" /> <!--日誌文件大小,可用的單位:KB|MB|GB--> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] " /> <footer value="[Footer] " /> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <!--輸出級別--> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> </filter> <!--必須結合起來用,第一個只過濾出WARN,第二個拒絕其它其它日誌輸出--> <!--<filter type="log4net.Filter.LevelMatchFilter"> <param name="LevelToMatch" value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" />--> </appender> <root> <level value="INFO" /> </root> <logger name="WebLogger"> <level value="WARN" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="RollingErrorLogFileAppender" /> </logger> <logger name="ServiceLogger"> <level value="INFO" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="RollingErrorLogFileAppender" /> <appender-ref ref="ColoredConsoleAppender" /> </logger> </log4net>
<configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net configSource="config\log4net.config" />
2. global.asax.cn 中要註冊下,才能訪問。log4net.Config.XmlConfigurator.Configure();
3。 assemblyinfo.cs 中,[assembly: log4net.Config.XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)] 可以不用配置
log4net配置要點