在配置文件web.config或App.config中的Configuration节点下增加以下配置
-
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
- configSections>
- <log4net>
- <appender name="Console" type="log4net.Appender.ConsoleAppender">
- <layout type="log4net.Layout.PatternLayout">
-
- <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline"/>
- layout>
- appender>
-
- <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
-
- <file value="App_Log\\log"/>
-
- <appendToFile value="true"/>
-
- <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
-
- <maxSizeRollBackups value="-1"/>
-
- <rollingStyle value="Composite"/>
- <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>
-
- <staticLogFileName value="false"/>
-
- <maximumFileSize value="100MB"/>
-
- <param name="CountDirection" value="1"/>
- <layout type="log4net.Layout.PatternLayout">
-
- <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level %n出错类:%logger 属性:[UserName:%property{UserName}] %n日志描述:%message%newline %n"/>
- layout>
- appender>
- <root>
-
- <level value="ALL"/>
-
- <appender-ref ref="RollingFile"/>
- root>
- log4net>
注意,在命名空间上方要加代码:[assembly: log4net.Config.XmlConfigurator(Watch = true)],才能正确读取配置文件中的内容。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- [assembly: log4net.Config.XmlConfigurator(Watch = true)]
- namespace Demo.Helper
- {
- ///
- /// 日志帮助类
- ///
- public class LogHelper
- {
- #region 输出日志到Log4Net
-
- ///
- /// 输出日志到Log4Net
- ///
- ///
- /// 异常对象
- /// 日志等级,默认为:1 Debug 级
- public static void WriteLog(Type t, Exception ex, LogLevel level = LogLevel.DEBUG)
- {
- WriteLog(t, ex.Message, level);
- }
-
- #endregion
-
- #region 输出日志到Log4Net
-
- ///
- /// 输出日志到Log4Net
- ///
- ///
- /// 错误信息
- /// 日志等级,默认为:1 Debug 级
- public static void WriteLog(Type t, string msg, LogLevel level = LogLevel.DEBUG)
- {
- // 对应日志信息中的“%property{UserName}”
- //log4net.ThreadContext.Properties["UserName"] = "Test";
-
- log4net.ILog log = log4net.LogManager.GetLogger(t);
- switch (level)
- {
- // 日志等级:OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL
- case LogLevel.DEBUG:
- log.Debug(msg);
- break;
- case LogLevel.INFO:
- log.Info(msg);
- break;
- case LogLevel.WARN:
- log.Warn(msg);
- break;
- case LogLevel.ERROR:
- log.Error(msg);
- break;
- case LogLevel.FATAL:
- log.Fatal(msg);
- break;
- default:
- log.Debug(msg);
- break;
- }
- }
-
- #endregion
-
- }
-
- ///
- /// 日志等级:
- /// OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL
- ///
- public enum LogLevel
- {
- ///
- /// ALL
- ///
- ALL = 0,
-
- ///
- /// DEBUG
- ///
- DEBUG = 1,
-
- ///
- /// INFO
- ///
- INFO = 2,
-
- ///
- /// WARN
- ///
- WARN = 3,
-
- ///
- /// ERROR
- ///
- ERROR = 4,
-
- ///
- /// FATAL
- ///
- FATAL = 5,
-
- ///
- /// OFF
- ///
- OFF = 6
- }
- }
- using System;
-
- namespace Demo
- {
- static class Program
- {
- ///
- /// 应用程序的主入口点。
- ///
- static void Main()
- {
- LogHelper.WriteLog(typeof(Demo), "输出日志信息。", LogLevel.INFO);
- }
- }
- }
- 记录时间:2022-08-24 17:58:48,881 线程ID:[4] 日志级别:INFO
- 出错类:Demo 属性:[UserName:John]
- 日志描述:监听启动中...
-
- 记录时间:2022-08-24 17:58:48,912 线程ID:[4] 日志级别:DEBUG
- 出错类:Demo 属性:[UserName:John]
- 日志描述:创建连接。
-
- 记录时间:2022-08-24 17:59:10,167 线程ID:[4] 日志级别:ERROR
- 出错类:Demo 属性:[UserName:John]
- 日志描述:连接失败。