时间:2023-02-22 09:38:16 | 栏目:.NET代码 | 点击:次
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" /> </system.web> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <!--错误日志--> // <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!-- 日志文件存放位置,可以为绝对路径也可以为相对路径 --> <file value="log\\LogError\\" /> <!--是否支持分割文件--> <appendToFile value="true" /> <!--日志文件的记录形式--> <rollingStyle value="Date" /> <!-- 日志文件的命名规则 --> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <!-- 当将日期作为日志文件的名字时,必须将staticLogFileName的值设置为false --> <staticLogFileName value="false" /> <!--当日志文件达到MaxFileSize大小,就自动创建备份文件。--> <param name="MaxSizeRollBackups" value="100" /> <!--日志文件的布局格式--> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <!--输出格式--> <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n错误描述:%message%newline %n" /> </layout> </appender> <!--Info日志--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n日志描述:%message%newline %n" /> </layout> </appender> <!--监控日志--> <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogMonitor\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n跟踪描述:%message%newline %n" /> </layout> </appender> <!--Error日志--> <logger name="LogError"> <level value="ERROR" /> <appender-ref ref="RollingLogFileAppender" /> </logger> <!--Info日志--> <logger name="LogInfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <!--监控日志--> <logger name="LogMonitor"> <level value="Monitor" /> <appender-ref ref="MonitorAppender" /> </logger> </log4net> </configuration>
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
这里的ConfigFile 后面改成你的配置文件的名字。
public class LoggerHelper { private static readonly log4net.ILog LogInfo = log4net.LogManager.GetLogger("LogInfo"); private static readonly log4net.ILog LogError = log4net.LogManager.GetLogger("LogError"); private static readonly log4net.ILog LogMonitor = log4net.LogManager.GetLogger("LogMonitor"); /// <summary> /// 记录Error日志 /// </summary> /// <param name="errorMsg"></param> /// <param name="ex"></param> public static void Error(string errorMsg, Exception ex = null) { if (ex != null) { LogError.Error(errorMsg, ex); } else { LogError.Error(errorMsg); } } /// <summary> /// 记录Info日志 /// </summary> /// <param name="msg"></param> /// <param name="ex"></param> public static void Info(string msg, Exception ex = null) { if (ex != null) { LogInfo.Info(msg, ex); } else { LogInfo.Info(msg); } } /// <summary> /// 记录Monitor日志 /// </summary> /// <param name="msg"></param> public static void Monitor(string msg) { LogMonitor.Info(msg); } }