1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!--添加自定义节点:log4net type:解析类名,程序集名(log4net.dll)--> 5 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 6 </configSections> 7 8 <log4net> 9 <!--定义输出到文件中--> 10 <appender name="Log4Net_INFO" type="log4net.Appender.RollingFileAppender"> 11 <!--定义文件存放位置--> 12 <file value="C:/log4net/"/> 13 <!--是否追加到文件,默认为true,通常无需设置--> 14 <appendToFile value="true"/> 15 <RollingStyle value="Date"/> 16 <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> 17 <DatePattern value="INFO_yyyyMMdd".log"" /> 18 <!--日志文件名是否为静态--> 19 <StaticLogFileName value="false"/> 20 <!--多线程时采用最小锁定--> 21 <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 22 <!--布局(向用户显示最后经过格式化的输出信息)--> 23 <layout type="log4net.Layout.PatternLayout"> 24 <!-- 25 %m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息 26 %n(new line):换行 27 %d(datetime):输出当前语句运行的时刻 28 %r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数 29 %t(thread id):当前语句所在的线程ID 30 %p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等 31 %c(class):当前日志对象的名称,例如: 32 %L:输出语句所在的行号 33 %F:输出语句所在的文件名 34 %-数字:表示该项的最小长度,如果不够,则用空格填充 35 --> 36 <Header value="[Header] "/> 37 <Footer value="[Footer] "/> 38 <!--正文--> 39 <ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> 40 </layout> 41 </appender> 42 43 <appender name="Log4Net_ERROR" type="log4net.Appender.RollingFileAppender"> 44 <file value="C:/log4net/"/> 45 <appendToFile value="true"/> 46 <RollingStyle value="Date"/> 47 <DatePattern value="ERROR_yyyyMMdd".log"" /> 48 <StaticLogFileName value="false"/> 49 <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 50 <layout type="log4net.Layout.PatternLayout"> 51 <Header value="[Header] "/> 52 <Footer value="[Footer] "/> 53 <!--正文--> 54 <ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> 55 </layout> 56 </appender> 57 58 <root> 59 <level value="ERROR"/> 60 <appender-ref ref="Log4Net_ERROR" /> 61 62 <level value="INFO"/> 63 <appender-ref ref="Log4Net_INFO" /> 64 </root> 65 66 </log4net> 67 68 </configuration>
配置文件
1 using log4net; 2 using System; 3 using System.Collections.Concurrent; 4 using System.Collections.Generic; 5 6 //指定log4net使用的config文件来读取配置信息 7 [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"LogConfig\Log4Net.config", Watch = true)] 8 namespace Project.Log4.Net.LogConfig 9 { 10 /// <summary> 11 /// 日志帮助类 12 /// </summary> 13 public class LogHelper 14 { 15 private static readonly ConcurrentDictionary<Type, ILog> _loggers = new ConcurrentDictionary<Type, ILog>(); 16 17 /// <summary> 18 /// 获取记录器 19 /// </summary> 20 /// <param name="source"></param> 21 /// <returns></returns> 22 private static ILog GetLogger(Type source) 23 { 24 if (_loggers.ContainsKey(source)) 25 { 26 return _loggers[source]; 27 } 28 else 29 { 30 ILog logger = LogManager.GetLogger(source); 31 _loggers.TryAdd(source, logger); 32 return logger; 33 } 34 } 35 36 /* Log a message object */ 37 38 /// <summary> 39 /// 调试信息 40 /// </summary> 41 /// <param name="source"></param> 42 /// <param name="message"></param> 43 public static void Debug(object source, string message) 44 { 45 Debug(source.GetType(), message); 46 } 47 48 /// <summary> 49 /// 调试信息 50 /// </summary> 51 /// <param name="source"></param> 52 /// <param name="message"></param> 53 /// <param name="ps"></param> 54 public static void Debug(object source, string message, params object[] ps) 55 { 56 Debug(source.GetType(), string.Format(message, ps)); 57 } 58 59 /// <summary> 60 /// 调试信息 61 /// </summary> 62 /// <param name="source"></param> 63 /// <param name="message"></param> 64 public static void Debug(Type source, string message) 65 { 66 ILog logger = GetLogger(source); 67 if (logger.IsDebugEnabled) 68 logger.Debug(message); 69 } 70 71 /// <summary> 72 /// 关键信息 73 /// </summary> 74 /// <param name="source"></param> 75 /// <param name="message"></param> 76 public static void Info(object source, object message) 77 { 78 Info(source.GetType(), message); 79 } 80 81 /// <summary> 82 /// 关键信息 83 /// </summary> 84 /// <param name="source"></param> 85 /// <param name="message"></param> 86 public static void Info(Type source, object message) 87 { 88 ILog logger = GetLogger(source); 89 if (logger.IsInfoEnabled) 90 logger.Info(message); 91 } 92 93 /// <summary> 94 /// 警告信息 95 /// </summary> 96 /// <param name="source"></param> 97 /// <param name="message"></param> 98 public static void Warn(object source, object message) 99 { 100 Warn(source.GetType(), message); 101 } 102 103 /// <summary> 104 /// 警告信息 105 /// </summary> 106 /// <param name="source"></param> 107 /// <param name="message"></param> 108 public static void Warn(Type source, object message) 109 { 110 ILog logger = GetLogger(source); 111 if (logger.IsWarnEnabled) 112 logger.Warn(message); 113 } 114 115 /// <summary> 116 /// 错误信息 117 /// </summary> 118 /// <param name="source"></param> 119 /// <param name="message"></param> 120 public static void Error(object source, object message) 121 { 122 Error(source.GetType(), message); 123 } 124 125 /// <summary> 126 /// 错误信息 127 /// </summary> 128 /// <param name="source"></param> 129 /// <param name="message"></param> 130 public static void Error(Type source, object message) 131 { 132 ILog logger = GetLogger(source); 133 if (logger.IsErrorEnabled) 134 logger.Error(message); 135 } 136 137 /// <summary> 138 /// 失败信息 139 /// </summary> 140 /// <param name="source"></param> 141 /// <param name="message"></param> 142 public static void Fatal(object source, object message) 143 { 144 Fatal(source.GetType(), message); 145 } 146 147 /// <summary> 148 /// 失败信息 149 /// </summary> 150 /// <param name="source"></param> 151 /// <param name="message"></param> 152 public static void Fatal(Type source, object message) 153 { 154 ILog logger = GetLogger(source); 155 if (logger.IsFatalEnabled) 156 logger.Fatal(message); 157 } 158 159 /* Log a message object and exception */ 160 161 /// <summary> 162 /// 调试信息 163 /// </summary> 164 /// <param name="source"></param> 165 /// <param name="message"></param> 166 /// <param name="exception"></param> 167 public static void Debug(object source, object message, Exception exception) 168 { 169 Debug(source.GetType(), message, exception); 170 } 171 172 /// <summary> 173 /// 调试信息 174 /// </summary> 175 /// <param name="source"></param> 176 /// <param name="message"></param> 177 /// <param name="exception"></param> 178 public static void Debug(Type source, object message, Exception exception) 179 { 180 GetLogger(source).Debug(message, exception); 181 } 182 183 /// <summary> 184 /// 关键信息 185 /// </summary> 186 /// <param name="source"></param> 187 /// <param name="message"></param> 188 /// <param name="exception"></param> 189 public static void Info(object source, object message, Exception exception) 190 { 191 Info(source.GetType(), message, exception); 192 } 193 194 /// <summary> 195 /// 关键信息 196 /// </summary> 197 /// <param name="source"></param> 198 /// <param name="message"></param> 199 /// <param name="exception"></param> 200 public static void Info(Type source, object message, Exception exception) 201 { 202 GetLogger(source).Info(message, exception); 203 } 204 205 /// <summary> 206 /// 警告信息 207 /// </summary> 208 /// <param name="source"></param> 209 /// <param name="message"></param> 210 /// <param name="exception"></param> 211 public static void Warn(object source, object message, Exception exception) 212 { 213 Warn(source.GetType(), message, exception); 214 } 215 216 /// <summary> 217 /// 警告信息 218 /// </summary> 219 /// <param name="source"></param> 220 /// <param name="message"></param> 221 /// <param name="exception"></param> 222 public static void Warn(Type source, object message, Exception exception) 223 { 224 GetLogger(source).Warn(message, exception); 225 } 226 227 /// <summary> 228 /// 错误信息 229 /// </summary> 230 /// <param name="source"></param> 231 /// <param name="message"></param> 232 /// <param name="exception"></param> 233 public static void Error(object source, object message, Exception exception) 234 { 235 Error(source.GetType(), message, exception); 236 } 237 238 /// <summary> 239 /// 错误信息 240 /// </summary> 241 /// <param name="source"></param> 242 /// <param name="message"></param> 243 /// <param name="exception"></param> 244 public static void Error(Type source, object message, Exception exception) 245 { 246 GetLogger(source).Error(message, exception); 247 } 248 249 /// <summary> 250 /// 失败信息 251 /// </summary> 252 /// <param name="source"></param> 253 /// <param name="message"></param> 254 /// <param name="exception"></param> 255 public static void Fatal(object source, object message, Exception exception) 256 { 257 Fatal(source.GetType(), message, exception); 258 } 259 260 /// <summary> 261 /// 失败信息 262 /// </summary> 263 /// <param name="source"></param> 264 /// <param name="message"></param> 265 /// <param name="exception"></param> 266 public static void Fatal(Type source, object message, Exception exception) 267 { 268 GetLogger(source).Fatal(message, exception); 269 } 270 } 271 272 }
LogHelper
时间: 2024-10-06 08:37:53