log4net的基本使用
1、net下配置使用
1)nuget管理器添加log4net包引用
2)配置web.config
<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <file value="log\\" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd'.txt'" /> <staticLogFileName value="false" /> <param name="MaxSizeRollBackups" value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <!--文件形式记录日志--> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net> </configuration>
注意节点位置放错了会报错,configSections在configuration下的第一个节点处放。log4net节点放到connectionStrings节点后面,没试过其他位置报错不。
3)为来方便使用,写一个loghelper静态类,方便调用
using log4net; [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace Infrastructure { public class LogHelper { public static readonly ILog _log = LogManager.GetLogger("log4net"); public static void Log(string message) { _log.Info(message); } public static void Debug(string message) { _log.Debug(message); } public static void Fatal(string message) { _log.Fatal(message); } public static void Warn(string message) { _log.Warn(message); } } }
4)直接调用,在需要记录日志的地方直接调用
LogHelper.Log("启动Web");
本文来自博客园,作者:CelonY,转载请注明原文链接:https://www.cnblogs.com/CelonY/p/12901282.html