重新整理 .net core 实践篇—————日志系统之战地记者[十五] (3)

那么最后来看一下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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { MessageLogger[] loggers = MessageLoggers; if (loggers == null) { return; } List<Exception> exceptions = null; for (int i = 0; i < loggers.Length; i++) { ref readonly MessageLogger loggerInfo = ref loggers[i]; if (!loggerInfo.IsEnabled(logLevel)) { continue; } LoggerLog(logLevel, eventId, loggerInfo.Logger, exception, formatter, ref exceptions, state); } if (exceptions != null && exceptions.Count > 0) { ThrowLoggingError(exceptions); } static void LoggerLog(LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func<TState, Exception, string> formatter, ref List<Exception> exceptions, in TState state) { try { logger.Log(logLevel, eventId, state, exception, formatter); } catch (Exception ex) { if (exceptions == null) { exceptions = new List<Exception>(); } exceptions.Add(ex); } } } }

里面循环判断是否当前级别能够输出:!loggerInfo.IsEnabled(logLevel)

然后调用对应的具体ILog实现的Log,这里贴一下ConsoleLogger 的实现:

[ThreadStatic] private static StringWriter t_stringWriter; 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)); } t_stringWriter ??= new StringWriter(); LogEntry<TState> logEntry = new LogEntry<TState>(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<LogMessageEntry> _messageQueue = new BlockingCollection<LogMessageEntry>(_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) { } } } 结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjdwd.html