4步曲
1.引用log4net.dll(nuget)
2.任意位置的命名空间头部加入下面的代码,web.config可修改为自己定义的.xml
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]
3.web.config -> <configuration>/<configSections>配置
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
4.web.config -> <configuration>配置
1 <log4net>
2 <logger name="[one_name]">
3 <level value="Info"/>
4 <appender-ref ref="[two_name]" />
5 </logger>
6 <appender name="[two_name]" type="log4net.Appender.RollingFileAppender">
7 <param name="File" type="log4net.Util.PatternString" value="log4net[one_name]\%date{yyyy}\%date{yyyyMM}\%date{yyyyMMdd}.log" />
8 <param name="Encoding" value="UTF-8" />
9 <param name="AppendToFile" value="true" />
10 <param name="MaxSizeRollBackups" value="10" />
11 <datePattern value="yyyy-MM-dd"-Debug.log"" />
12 <param name="MaximumFileSize" value="2MB" />
13 <param name="RollingStyle" value="Size" />
14 <param name="StaticLogFileName" value="false" />
15 <layout type="log4net.Layout.PatternLayout">
16 <param name="ConversionPattern" value="%date [%thread] %logger %-5level - %message%newline" />
17 </layout>
18 </appender>
19 <root>
20 <level value="ALL" />
21 <appender-ref ref="RollingFileAppender" />
22 </root>
23 </log4net>
注意点
1.括号里面的two_name必须一致,很重要
2.<logger>标签可使log4net定义输出无数个独立的日志文件
3.%date{yyyyMMddHHmmssff}=年月日时分秒毫秒,%date{}不能少