ServiceCollectionServiceExtensions.AddSingleton(collection, (Func<IServiceProvider, M0>) (services => (ILoggerFactory) new SerilogLoggerFactory(logger, dispose, (LoggerProviderCollection)
将我们的ILoggerFactory 替换成了SerilogLoggerFactory,官方是LoggerFactory。
那么看下SerilogLoggerFactory:
public SerilogLoggerFactory( Serilog.ILogger logger = null, bool dispose = false, LoggerProviderCollection providerCollection = null) { this._provider = new SerilogLoggerProvider(logger, dispose); this._providerCollection = providerCollection; } /// <summary>Disposes the provider.</summary> public void Dispose() { this._provider.Dispose(); } /// <summary> /// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance. /// </summary> /// <param>The category name for messages produced by the logger.</param> /// <returns> /// The <see cref="T:Microsoft.Extensions.Logging.ILogger" />. /// </returns> public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) { return this._provider.CreateLogger(categoryName); } /// <summary> /// Adds an <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" /> to the logging system. /// </summary> /// <param>The <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" />.</param> public void AddProvider(ILoggerProvider provider) { if (provider == null) throw new ArgumentNullException(nameof (provider)); if (this._providerCollection != null) this._providerCollection.AddProvider(provider); else SelfLog.WriteLine("Ignoring added logger provider {0}", (object) provider, (object) null, (object) null); } }
那么继续查看CreateLogger,SerilogLoggerProvider的CreateLogger 及其相关:
private readonly Serilog.ILogger _logger; public SerilogLoggerProvider(Serilog.ILogger logger = null, bool dispose = false) { if (logger != null) this._logger = logger.ForContext((IEnumerable<ILogEventEnricher>) new SerilogLoggerProvider[1] { this }); if (!dispose) return; if (logger != null) this._dispose = (Action) (() => { if (!(logger is IDisposable disposable)) return; disposable.Dispose(); }); else this._dispose = new Action(Log.CloseAndFlush); } public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { return (Microsoft.Extensions.Logging.ILogger) new SerilogLogger(this, this._logger, name); }
而SerilogLogger 就是具体来管理全部继承ILogger具体的实现的类。 那么是否是和官方一样,其他实现类都是继承自ILogger的呢?答案不是,他们都继承自ILogEventSink,下面会继续提及,暂时不要关心这个。
同时要忘记官方每个实现具体打印的类继承ILogger,它的设计思想还是一样的,只是换了一个继承接口。
SerilogLogger 里面继承Microsoft.Extensions.Logging.ILogger,实现具体的打印的。
看下SerilogLogger 的具体的部分,主要看log方法,因为这个实现打印的,这下面不用看太多,只需要看到其实里面是调用_logger的方法就行。
internal class SerilogLogger : Microsoft.Extensions.Logging.ILogger { private static readonly MessageTemplateParser MessageTemplateParser = new MessageTemplateParser(); private static readonly LogEventProperty[] LowEventIdValues = Enumerable.Range(0, 48).Select<int, LogEventProperty>((Func<int, LogEventProperty>) (n => new LogEventProperty("Id", (LogEventPropertyValue) new ScalarValue((object) n)))).ToArray<LogEventProperty>(); private readonly SerilogLoggerProvider _provider; private readonly Serilog.ILogger _logger; public SerilogLogger(SerilogLoggerProvider provider, Serilog.ILogger logger = null, string name = null) { SerilogLoggerProvider serilogLoggerProvider = provider; if (serilogLoggerProvider == null) throw new ArgumentNullException(nameof (provider)); this._provider = serilogLoggerProvider; this._logger = logger; Serilog.ILogger logger1 = this._logger; if (logger1 == null) logger1 = Serilog.Log.Logger.ForContext((IEnumerable<ILogEventEnricher>) new SerilogLoggerProvider[1] { provider }); this._logger = logger1; if (name == null) return; this._logger = this._logger.ForContext("SourceContext", (object) name, false); } public bool IsEnabled(LogLevel logLevel) { return this._logger.IsEnabled(LevelConvert.ToSerilogLevel(logLevel)); } public IDisposable BeginScope<TState>(TState state) { return this._provider.BeginScope<TState>(state); } public void Log<TState>( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { LogEventLevel serilogLevel = LevelConvert.ToSerilogLevel(logLevel); if (!this._logger.IsEnabled(serilogLevel)) return; Serilog.ILogger logger = this._logger; string str1 = (string) null; List<LogEventProperty> logEventPropertyList = new List<LogEventProperty>(); if (state is IEnumerable<KeyValuePair<string, object>> keyValuePairs) { foreach (KeyValuePair<string, object> keyValuePair in keyValuePairs) { if (keyValuePair.Key == "{OriginalFormat}" && keyValuePair.Value is string str2) str1 = str2; else if (keyValuePair.Key.StartsWith("@")) { LogEventProperty property; if (logger.BindProperty(keyValuePair.Key.Substring(1), keyValuePair.Value, true, out property)) logEventPropertyList.Add(property); } else { LogEventProperty property; if (logger.BindProperty(keyValuePair.Key, keyValuePair.Value, false, out property)) logEventPropertyList.Add(property); } } Type type = state.GetType(); TypeInfo typeInfo = type.GetTypeInfo(); if (str1 == null && !typeInfo.IsGenericType) { str1 = "{" + type.Name + ":l}"; LogEventProperty property; if (logger.BindProperty(type.Name, SerilogLogger.AsLoggableValue<TState>(state, formatter), false, out property)) logEventPropertyList.Add(property); } } if (str1 == null) { string propertyName = (string) null; if ((object) state != null) { propertyName = "State"; str1 = "{State:l}"; } else if (formatter != null) { propertyName = "Message"; str1 = "{Message:l}"; } LogEventProperty property; if (propertyName != null && logger.BindProperty(propertyName, SerilogLogger.AsLoggableValue<TState>(state, formatter), false, out property)) logEventPropertyList.Add(property); } if (eventId.Id != 0 || eventId.Name != null) logEventPropertyList.Add(SerilogLogger.CreateEventIdProperty(eventId)); MessageTemplate messageTemplate = SerilogLogger.MessageTemplateParser.Parse(str1 ?? ""); LogEvent logEvent = new LogEvent(DateTimeOffset.Now, serilogLevel, exception, messageTemplate, (IEnumerable<LogEventProperty>) logEventPropertyList); logger.Write(logEvent); } }