重新整理 .net core 实践篇—————日志系统之战地记者[十五]
2021-08-13 00:58
标签:img entry stop city locking single lazy expect data 前言 本节开始整理日志相关的东西。先整理一下日志的基本原理。 正文 首先介绍一下包: Microsoft.Extengsion.Logging.Abstrations 这个是接口包。 Microsoft.Extengsion.Logging 这个是实现包 Microsoft.Extengsion.Logging.Console 这个是扩展包 代码如下: static void Main(string[] args) { IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile("appsettings.json",optional:false,reloadOnChange:true); var config = configurationBuilder.Build(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(p=>config); serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService(); var loggerObj = loggerFactory.CreateLogger("Default"); loggerObj.LogInformation(2021, "Default,now that is 2021"); var loggerObj2 = loggerFactory.CreateLogger("loggerObj"); loggerObj2.LogDebug(2021, "loggerObj,now that is 2021"); Console.ReadKey(); } 配置文件: { "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "Console": { "LogLevel": { "Default": "Information", "Program": "Trace", "loggerObj": "Debug" } } } } 结果: 首先是配置级别的问题,查看loglevel 文件: public enum LogLevel { /// Logs that contain the most detailed messages. These messages may contain sensitive application data. /// These messages are disabled by default and should never be enabled in a production environment. Trace, /// Logs that are used for interactive investigation during development. These logs should primarily contain /// information useful for debugging and have no long-term value. Debug, /// Logs that track the general flow of the application. These logs should have long-term value. Information, /// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the /// application execution to stop. Warning, /// Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a /// failure in the current activity, not an application-wide failure. Error, /// Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires /// immediate attention. Critical, /// Not used for writing log messages. Specifies that a logging category should not write any messages. None, } 从上之下,依次提高log级别。 比如说设置了log 级别是Error,那么Debug、Information、Warning 都不会被答应出来。 那么就来分析一下代码吧。 AddLogging: public static IServiceCollection AddLogging(this IServiceCollection services, Action configure) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger), typeof(Logger))); services.TryAddEnumerable(ServiceDescriptor.Singleton( new DefaultLoggerLevelConfigureOptions(LogLevel.Information))); configure(new LoggingBuilder(services)); return services; } 这里面给注册了ILoggerFactory和ILogger。然后设置了一个打印log的级别配置,LogLevel.Information,这个就是如果我们没有配置文件默认就是Information这种级别了。 configure(new LoggingBuilder(services)) 给我们的委托提供了一个LoggingBuilder的实例化对象。这个对象就是用来专门做扩展的,是解耦的一种方式。 internal class LoggingBuilder : ILoggingBuilder { public LoggingBuilder(IServiceCollection services) { Services = services; } public IServiceCollection Services { get; } } 这个LoggingBuilder 类基本什么功能都没有,但是因为有了这样一个类,就可以作为扩展的标志了。 比如说上文的: builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); 看下AddConfiguration: public static ILoggingBuilder AddConfiguration(this ILoggingBuilder builder, IConfiguration configuration) { builder.AddConfiguration(); builder.Services.AddSingleton(new LoggerFilterConfigureOptions(configuration)); builder.Services.AddSingleton(new ConfigurationChangeTokenSource(configuration)); builder.Services.AddSingleton(new LoggingConfiguration(configuration)); return builder; } 这里面给我们注入了配置文件的配置:builder.Services.AddSingleton(new LoggerFilterConfigureOptions(configuration)) 同时给我们注册监听令牌:builder.Services.AddSingleton(new ConfigurationChangeTokenSource(configuration)); 这里给我们注册配置保存在LoggingConfiguration中:builder.Services.AddSingleton(new LoggingConfiguration(configuration)); 因为LoggingConfiguration 保存了,故而我们随时可以获取到LoggingConfiguration 的配置。 看下AddConsole: /// The to use. public static ILoggingBuilder AddConsole(this ILoggingBuilder builder) { builder.AddConfiguration(); builder.AddConsoleFormatter(); builder.AddConsoleFormatter(); builder.AddConsoleFormatter(); builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); LoggerProviderOptions.RegisterProviderOptions(builder.Services); return builder; } builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()) 里面给我们ILoggerProvider 增加了一个ConsoleLoggerProvider,故而我们多了一个打印的功能。 LoggerProviderOptions.RegisterProviderOptions(builder.Services) 给我们加上了ConsoleLoggerOptions 绑定为ConsoleLoggerProvider的配置。 RegisterProviderOptions 如下: public static void RegisterProviderOptions(IServiceCollection services) where TOptions : class { services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.TryAddEnumerable(ServiceDescriptor.Singleton()); } 接下来就是调用服务: var loggerObj = loggerFactory.CreateLogger("Default"); loggerObj.LogInformation(2021, "Default,now that is 2021"); 看下LoggerFactory的CreateLogger: public ILogger CreateLogger(string categoryName) { if (CheckDisposed()) { throw new ObjectDisposedException(nameof(LoggerFactory)); } lock (_sync) { if (!_loggers.TryGetValue(categoryName, out Logger logger)) { logger = new Logger { Loggers = CreateLoggers(categoryName), }; (logger.MessageLoggers, logger.ScopeLoggers) = ApplyFilters(logger.Loggers); _loggers[categoryName] = logger; } return logger; } } 里面做了缓存,如果categoryName有缓存的话直接使用缓存,如果没有那么调用CreateLoggers创建。 查看CreateLoggers: private LoggerInformation[] CreateLoggers(string categoryName) { var loggers = new LoggerInformation[_providerRegistrations.Count]; for (int i = 0; i _formatters[ConsoleFormatterNames.Systemd], _ => _formatters[ConsoleFormatterNames.Simple], }; if (_options.CurrentValue.FormatterName == null) { UpdateFormatterOptions(logFormatter, _options.CurrentValue); } #pragma warning disable CS0618 } return _loggers.GetOrAdd(name, loggerName => new ConsoleLogger(name, _messageQueue) { Options = _options.CurrentValue, ScopeProvider = _scopeProvider, Formatter = logFormatter, }); } } 看到这个IOptionsMonitor,就知道console 配置是支持热更新的,里面创建了ConsoleLogger,这个ConsoleLogger就是用来打log正在的调用类。 值得注意的是_messageQueue这个,看了打印log还是有一个队列的,按照先进先出原则。 那么最后来看一下loggerObj.LogInformation(2021, "Default,now that is 2021");: 第一层 public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args) { logger.Log(LogLevel.Information, eventId, message, args); } 第二层 public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object[] args) { logger.Log(logLevel, eventId, null, message, args); } 第三层 public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, string message, params object[] args) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter); } 那么这个logger.Log 是调用具体某个logger,像consoleLogger 吗? 不是,我们看LoggerFactory的CreateLogger时候封装了: logger = new Logger { Loggers = CreateLoggers(categoryName), }; 那么看下Logger的Log到底干了什么。 internal class Logger : ILogger { public LoggerInformation[] Loggers { get; set; } public MessageLogger[] MessageLoggers { get; set; } public ScopeLogger[] ScopeLoggers { get; set; } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { MessageLogger[] loggers = MessageLoggers; if (loggers == null) { return; } List exceptions = null; for (int i = 0; i 0) { ThrowLoggingError(exceptions); } static void LoggerLog(LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func formatter, ref List exceptions, in TState state) { try { logger.Log(logLevel, eventId, state, exception, formatter); } catch (Exception ex) { if (exceptions == null) { exceptions = new List(); } exceptions.Add(ex); } } } } 里面循环判断是否当前级别能够输出:!loggerInfo.IsEnabled(logLevel) 然后调用对应的具体ILog实现的Log,这里贴一下ConsoleLogger 的实现: [ThreadStatic] private static StringWriter t_stringWriter; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } t_stringWriter ??= new StringWriter(); LogEntry logEntry = new LogEntry(logLevel, _name, eventId, state, exception, formatter); Formatter.Write(in logEntry, ScopeProvider, t_stringWriter); var sb = t_stringWriter.GetStringBuilder(); if (sb.Length == 0) { return; } string computedAnsiString = sb.ToString(); sb.Clear(); if (sb.Capacity > 1024) { sb.Capacity = 1024; } _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options.LogToStandardErrorThreshold)); } 把这个队列的也贴一下,比较经典吧。 internal class ConsoleLoggerProcessor : IDisposable { private const int _maxQueuedMessages = 1024; private readonly BlockingCollection _messageQueue = new BlockingCollection(_maxQueuedMessages); private readonly Thread _outputThread; public IConsole Console; public IConsole ErrorConsole; public ConsoleLoggerProcessor() { // Start Console message queue processor _outputThread = new Thread(ProcessLogQueue) { IsBackground = true, Name = "Console logger queue processing thread" }; _outputThread.Start(); } public virtual void EnqueueMessage(LogMessageEntry message) { if (!_messageQueue.IsAddingCompleted) { try { _messageQueue.Add(message); return; } catch (InvalidOperationException) { } } // Adding is completed so just log the message try { WriteMessage(message); } catch (Exception) { } } // for testing internal virtual void WriteMessage(LogMessageEntry entry) { IConsole console = entry.LogAsError ? ErrorConsole : Console; console.Write(entry.Message); } private void ProcessLogQueue() { try { foreach (LogMessageEntry message in _messageQueue.GetConsumingEnumerable()) { WriteMessage(message); } } catch { try { _messageQueue.CompleteAdding(); } catch { } } } public void Dispose() { _messageQueue.CompleteAdding(); try { _outputThread.Join(1500); // with timeout in-case Console is locked by user input } catch (ThreadStateException) { } } } 结 因为是实践篇,只是具体执行过程带过一下,细节篇的时候,会详细介绍一下机制,比如说ConsoleLoggerProcessor的这种队列机制,又比如说Logger模型设计等。 以上只是个人整理,如有错误,望请指出。 下一节,服务与logger系统之间。重新整理 .net core 实践篇—————日志系统之战地记者[十五]标签:img entry stop city locking single lazy expect data 原文地址:https://www.cnblogs.com/aoximin/p/14854519.html
文章标题:重新整理 .net core 实践篇—————日志系统之战地记者[十五]
文章链接:http://soscw.com/index.php/essay/107379.html