这是 serilog-asp.net 的demo: https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/Sample/Program.cs
开始是这一句话
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the // logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been // set up successfully. Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateBootstrapLogger();
这个 UseSerilog() 里面有个参数 bool preserveStaticLogger =false, 如果不传(默认false), 最终将会被替换。
而如果替换,上面的代码,就要写成 CreateBootstrapLogger(), 而不是 CreateLogger()
这关系到 UseSerilog() 这里面这句话的转换结果。
var reloadable = Log.Logger as ReloadableLogger;
UseSerilog() 的后面 依赖注入了 ILoggerFactory,实现的是SerilogLoggerFactory。
SerilogLoggerFactory 中 new SerilogLoggerProvider(), 后者又new SerilogLogger(this, _logger, name),这样定义 SerilogLogger : Microsoft.Extensions.Logging.ILogger
在这个方法中,_logger 参数是null, 传了一路,最后赋给默认值 Serilog.Log.Logger
这会让 ILogger 最终使用到Serilog.Log.Logger。
标签:asp,Log,serilog,net,UseSerilog,Logger,logger From: https://www.cnblogs.com/qgbo/p/16633431.html