1.引用Log4Net.dll
2.LogHelper类定义
1 private static readonly ILog _logger = LogManager.GetLogger("LogTrace"); 2 3 public static void Info(string message) 4 { 5 _logger.Info(message); 6 } 7 public static void Debug(string message) 8 { 9 _logger.Debug(message); 10 } 11
3.配置文件内容添加
1 <configSections> 2 <!--Add Log4Net Section--> 3 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 4 </configSections>
<log4net> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="D:App.log" /> <!--file可以指定具体的路径 eg : d:\test.log。不指定的话log被生成在项目的bin/Debug 或者 bin/Release目录下 (web的项目 默认生成在根目录下)--> <param name="AppendToFile" value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <!--指定log的格式--> <conversionPattern value="%nTime:%date %Info:%message%newline" /> </layout> </appender> <logger name="LogTrace"> <level value="ALL" /> <appender-ref ref="LogFileAppender" /> </logger> </log4net>
4.AssemblyInfo配置
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]