注意事项
<!-- The file log4Net.config is copied into bin/Debug /release by compiler 添加如下内容到 assemblyinfo.cs [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)] 否则看不到Log文件 -->
Way 1: ....
Way 2:
Step 1. App.config file example(use extra file log4net.config) =>
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> <add key="log4net.Config" value="log4net.config" /> <add key="ClientSettingsProvider.ServiceUri" value="" /> </appSettings> <system.diagnostics> <assert assertuienabled="true" logfilename="D:\\myLog\\myFile.log" /> </system.diagnostics> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> <supportedRuntime version="v2.0.50727" /> </startup> <system.web> <membership defaultProvider="ClientAuthenticationMembershipProvider"> <providers> <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" /> </providers> </membership> <roleManager defaultProvider="ClientRoleProvider" enabled="true"> <providers> <add name=" /> </providers> </roleManager> </system.web> </configuration>
Step 2. log4net.config =>
<?xml version="1.0" encoding="utf-8" ?> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. The file log4Net.config is copied into bin/Debug /release by compiler 添加如下内容到 assemblyinfo.cs [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)] --> <log4net> <root> <level value="ALL" /> <appender-ref ref="rollingFile" /> <appender-ref ref="consoleApp" /> </root> <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="D:\myLog\appLog.txt" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy.MM.dd" /> <param name="StaticLogFileName" value="true" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> <logger name="Log4NetTest.LogTest"> <level value="DEBUG" /> <appender-ref ref="rollingFile" /> <appender-ref ref="consoleApp" /> </logger> </log4net>
Step 3. Tell App.config to load log4net.config
The file log4Net.config is copied into bin/Debug /release by compiler 添加如下内容到 assemblyinfo.cs [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)]