.net core2.0 中使用log4net
一、nuget安装log4net
二、添加log4net.config配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <? xml version="1.0" encoding="utf-8"?> < configuration > < configSections > <!--添加自定义节点:log4net type:解析类名,程序集名(log4net.dll)--> < section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </ configSections > < log4net > < root > <!--<level value="DEBUG"/>--> <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF ALL表示允许所有的日志请求。OFF是拒绝所有的请求。--> <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录--> < level value="ALL"/> <!--根据log级别记录到不同的日志文件--> < appender-ref ref="ErrorLog" /> </ root > <!--Error--> < appender name="ErrorLog" type="log4net.Appender.RollingFileAppender"> < file value="Log/"/> < appendToFile value="true" /> < rollingStyle value="Date" /> <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> < DatePattern value="yyyyMMdd"_Error.log"" /> <!--日志文件名是否为静态--> < StaticLogFileName value="false"/> <!--多线程时采用最小锁定--> < lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--布局(向用户显示最后经过格式化的输出信息)--> < layout type="log4net.Layout.PatternLayout"> < ConversionPattern value="%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message %newline" /> </ layout > < securityContext type="log4net.Util.WindowsSecurityContext"> < credentials value="Process" /> </ securityContext > < filter type="log4net.Filter.LevelRangeFilter"> < levelMin value="DEBUG" /> < levelMax value="ERROR" /> </ filter > </ appender > <!--Error--> </ log4net > </ configuration > |
三、编写loghelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | using GrabNovelApi; using log4net; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; namespace Grab.api { public static class LogHelper { private static readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>(); /// <summary> /// 获取记录器 /// </summary> /// <param name="source">soruce</param> /// <returns></returns> private static ILog GetLogger(Type source) { if (Loggers.ContainsKey(source)) { return Loggers[source]; } else { ILog logger = LogManager.GetLogger(Startup.repository.Name,source); Loggers.TryAdd(source, logger); return logger; } } /* Log a message object */ /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Debug( object source, string message) { Debug(source.GetType(), message); } /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="ps">ps</param> public static void Debug( object source, string message, params object [] ps) { Debug(source.GetType(), string .Format(message, ps)); } /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Debug(Type source, string message) { ILog logger = GetLogger(source); if (logger.IsDebugEnabled) { logger.Debug(message); } } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Info( object source, object message) { Info(source.GetType(), message); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Info(Type source, object message) { ILog logger = GetLogger(source); if (logger.IsInfoEnabled) { logger.Info(message); } } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Warn( object source, object message) { Warn(source.GetType(), message); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Warn(Type source, object message) { ILog logger = GetLogger(source); if (logger.IsWarnEnabled) { logger.Warn(message); } } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Error( object source, object message) { Error(source.GetType(), message); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Error(Type source, object message) { ILog logger = GetLogger(source); if (logger.IsErrorEnabled) { logger.Error(message); } } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Fatal( object source, object message) { Fatal(source.GetType(), message); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> public static void Fatal(Type source, object message) { ILog logger = GetLogger(source); if (logger.IsFatalEnabled) { logger.Fatal(message); } } /* Log a message object and exception */ /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Debug( object source, object message, Exception exception) { Debug(source.GetType(), message, exception); } /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Debug(Type source, object message, Exception exception) { GetLogger(source).Debug(message, exception); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Info( object source, object message, Exception exception) { Info(source.GetType(), message, exception); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Info(Type source, object message, Exception exception) { GetLogger(source).Info(message, exception); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Warn( object source, object message, Exception exception) { Warn(source.GetType(), message, exception); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Warn(Type source, object message, Exception exception) { GetLogger(source).Warn(message, exception); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Error( object source, object message, Exception exception) { Error(source.GetType(), message, exception); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Error(Type source, object message, Exception exception) { GetLogger(source).Error(message, exception); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Fatal( object source, object message, Exception exception) { Fatal(source.GetType(), message, exception); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Fatal(Type source, object message, Exception exception) { GetLogger(source).Fatal(message, exception); } } } |
四、使用
本文作者:WangJunZzz
本文链接:https://www.cnblogs.com/WangJunZzz/p/8708423.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步