在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 LoggerProvIDer 的话就需要借助第三方日志框架实现了,而一些第三方框架的实现大多比较完善和成熟,不失为一个好办法。
自己写了一个 log4net 的扩展 WeihanLi.Common.Logging.Log4Net,提供了在 .net core 中使用 log4net 的扩展
安装 nuget 包通过 nuget 安装 WeihanLi.Common.Logging.Log4Net
使用基本使用
ILoggerFactory loggerFactory = new LoggerFactory();loggerFactory.AddLog4Net(); // loggerFactory.AddLog4Net(log4netConfigfilePath);
你可以在 asp.net core 应用里你的 Startup 文件中使用下面代码进行配置
// This method gets called by the runtime. Use this method to configure the http request pipeline.public voID Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerFactory) { loggerFactory.AddLog4Net(); // loggerFactory.AddLog4Net(log4netConfigfilePath); // ... }
默认使用当前目录下的 log4net.config 文件作为 log4net 的配置文件,如果不是需要自己设置 log4net 配置文件的路径。
log4net 配置参考 示例配置
源码解析项目源码
Log4NetLoggerProvider
实现记录日志的 ILogger internal class Log4NetLogger : ILogger { private Readonly ILog _logger; public Log4NetLogger(string name) => _logger = LogManager.GetLogger(ApplicationHelper.Applicationname,name); public Idisposable BeginScope<TState>(TState state) => NullScope.Instance; public bool IsEnabled(LogLevel logLevel) { switch (logLevel) { case LogLevel.Critical: return _logger.IsFatalEnabled; case LogLevel.DeBUG: case LogLevel.Trace: return _logger.IsDeBUGEnabled; case LogLevel.Error: return _logger.IsErrorEnabled; case LogLevel.information: return _logger.IsInfoEnabled; case LogLevel.Warning: return _logger.IsWarnEnabled; case LogLevel.None: return false; default: throw new ArgumentOutOfRangeException(nameof(logLevel)); } } public voID Log<TState>(LogLevel logLevel,EventID eventID,TState state,Exception exception,Func<TState,Exception,string> formatter) { if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } var message = formatter(state,exception); if (!(string.IsNullOrEmpty(message) && exception == null)) { switch (logLevel) { case LogLevel.Critical: _logger.Fatal(message,exception); break; case LogLevel.DeBUG: case LogLevel.Trace: _logger.DeBUG(message,exception); break; case LogLevel.Error: _logger.Error(message,exception); break; case LogLevel.information: _logger.Info(message,exception); break; case LogLevel.Warning: _logger.Warn(message,exception); break; default: _logger.Warn($"Encountered unkNown log level {logLevel},writing out as Info."); _logger.Info(message,exception); break; } } } }
实现提供 logger 的 ILoggerProvIDer [ProvIDerAlias("log4net")] internal class Log4NetLoggerProvIDer : ILoggerProvIDer { private Readonly ConcurrentDictionary<string,Log4NetLogger> _loggers = new ConcurrentDictionary<string,Log4NetLogger>(StringComparer.Ordinal); public Log4NetLoggerProvIDer(string conffilePath) { if (null == LogManager.GetAllRepositorIEs()?.FirstOrDefault(_ => _.name == ApplicationHelper.Applicationname)) { XmlConfigurator.ConfigureAnDWatch(LogManager.CreateRepository(ApplicationHelper.Applicationname),new fileInfo(conffilePath)); } } public voID dispose() => _loggers.Clear(); public ILogger CreateLogger(string categoryname) => _loggers.GetorAdd(categoryname,loggername => new Log4NetLogger(loggername)); }
添加 ILoggerFactory 扩展 public static class Log4NetLoggerFactoryExtensions { public static ILoggerFactory AddLog4Net(this ILoggerFactory factory) { factory.AddProvIDer(new Log4NetLoggerProvIDer(ApplicationHelper.MapPath("log4net.config"))); return factory; } public static ILoggerFactory AddLog4Net(this ILoggerFactory factory,string configfile) { factory.AddProvIDer(new Log4NetLoggerProvIDer(configfile)); return factory; } }
Memo如果有什么问题或建议,欢迎指出
总结以上是内存溢出为你收集整理的asp.net core 集成 log4net 日志框架全部内容,希望文章能够帮你解决asp.net core 集成 log4net 日志框架所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)