[英]Use Serilog with Microsoft.Extensions.Logging.ILogger
I've created a .NET Core 3.1 project using a Host, the IoC container with IServiceCollection
and implemented logging allover the place using the ILogger<T>
interface from Microsoft.Extensions.Logging
.我已经使用主机创建了一个 .NET Core 3.1 项目,它是带有
IServiceCollection
IoC 容器,并使用来自Microsoft.Extensions.Logging
的ILogger<T>
接口实现了整个地方的日志记录。 I now need to implement more advanced logging and decided to use Serilog.我现在需要实现更高级的日志记录并决定使用 Serilog。
I assumed that it would be a breeze to switch from .NET built-in loggers to Serilog.我认为从 .NET 内置记录器切换到 Serilog 会轻而易举。 But to my surprise, Serilog is using it's own
ILogger
interface - bummer!但令我惊讶的是,Serilog 正在使用它自己的
ILogger
接口——真可惜! So now I needed to update ALL places to use Serilog ILogger
, or to implement Serilog with a .NET Core ILogger<T>
interface.所以现在我需要更新所有地方以使用 Serilog
ILogger
,或者使用 .NET Core ILogger<T>
接口实现 Serilog。
My question is - is it really not possible to use Serilog with the ILogger interface in Microsoft.Extensions.Logging
?我的问题是 - 真的不可能在
Microsoft.Extensions.Logging
中将 Serilog 与 ILogger 接口一起使用吗? Would be so much smarter!会更聪明!
In the Serilog.Extensions.Logging
assembly there is a extension method on IloggingBuilder
called AddSerilog
(it's in the Serilog namespace) that will allow you to use Serilog for logging.在
Serilog.Extensions.Logging
程序集中, IloggingBuilder
上有一个名为AddSerilog
的扩展方法(它位于 Serilog 命名空间中),它允许您使用 Serilog 进行日志记录。 For example:例如:
.NET Core 2.2 and earlier (using WebHost
): .NET Core 2.2 及更早版本(使用
WebHost
):
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddSerilog();
});
.NET Core 3.1 and later (using generic Host
for either web or console apps): .NET Core 3.1 及更高版本(对 web 或控制台应用程序使用通用
Host
):
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>()})
.UseSerilog();
Now the ILogger
and ILogger<>
implementation will call into Serilog.现在
ILogger
和ILogger<>
实现将调用 Serilog。
In addition to ways by using builder.host.UseSeriLog()
extensions method.除了使用
builder.host.UseSeriLog()
扩展方法的方法。 Here is more elegant way (we can use in external console or unit test ILogger without implementation of main project) .这是更优雅的方式(我们可以在外部控制台或单元测试 ILogger 中使用而无需执行主项目) 。
Serilog.Extensions.Logging
along with core package Serilog
.Serilog.Extensions.Logging
以及核心 package Serilog
。Log.Logger = new LoggerConfiguration).WriteTo.Console().CreateLogger()
.Log.Logger = new LoggerConfiguration).WriteTo.Console().CreateLogger()
一样。ILoggerFactory
extension method AddSeriLog()
like this ILoggerFactory factory = new LoggerFactory().AddSerilog(Log.Logger);
ILoggerFactory
扩展方法AddSeriLog()
ILoggerFactory factory = new LoggerFactory().AddSerilog(Log.Logger);
factory.CreateLogger<T>();
factory.CreateLogger<T>();
public static Microsoft.Extensions.Logging.ILogger LoggingInstance<T>()
{
Serilog.Log.Logger = Serilog.Log.Logger ?? new LoggerConfiguration().WriteTo.(xxxxYourSinksxxxx).CreateLogger();
ILoggerFactory factory = new LoggerFactory().AddSerilog(Log.Logger);
return factory.CreateLogger<T>();
}
Its usage be like它的用法就像
var logger = LoggingInstance<YourModel>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.