[英]Serilog Sink to ASPNET Core Logging
What I would like is to be able to instrument my code with both Serilog and Microsoft.Extensions.Logging (ILogger). 我想要的是能够使用Serilog和Microsoft.Extensions.Logging(ILogger)来检测我的代码。 And have them both configured the same so they write to the same "sinks".
并让它们都配置相同,以便它们写入相同的“接收器”。 I have an ASPNET Core application with logging working and some dependent libraries, using Serilog, that I would like to capture the log output from (to the same place as the ASPNET Core app).
我有一个ASPNET核心应用程序与日志工作和一些依赖库,使用Serilog,我想捕获日志输出(与ASPNET核心应用程序相同的地方)。
I'm aware that there is a nuget package to use Serilog logging with ASPNET Core applications. 我知道有一个nuget包可以使用ASPIL Core应用程序进行Serilog日志记录。 However, this particular extension
但是,这个特殊的扩展
(WebAppBuilder).UseSerilog()
Appears to completely replace the existing ILoggerFactory with a serilog version and this stops my ASPNET logging from working. 似乎用serilog版本完全替换现有的ILoggerFactory,这会阻止我的ASPNET日志记录工作。 That would be ok if I could simply plugin the serilog config so that it works like the original ASPNET Core mechanism.
如果我可以简单地插入serilog配置,使其像原始ASPNET核心机制一样工作,那就没问题。 However, Serilog.Settings.Configuration doesn't seem to faithfully copy the config to Serilog.
但是,Serilog.Settings.Configuration似乎没有忠实地将配置复制到Serilog。
It also bothers me that they can't all share the share config, what if I use other libraries that use, say, NLog, how do i integrate that into the same logging infrastructure? 令我困扰的是,他们不能共享共享配置,如果我使用其他使用NLog的库,如何将其集成到同一个日志记录基础架构中呢?
Can Serilog be configured the same as Microsoft.Extensions.Logging? Serilog可以配置为与Microsoft.Extensions.Logging相同吗? And if so, how is this achieved?
如果是这样,这是如何实现的?
In your Program.cs
you will have this (after you've loaded your IConfigurationRoot
): 在
Program.cs
你将拥有它(在你加载IConfigurationRoot
):
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.Console()
.CreateLogger();
That sets up a default "Console" sink along with whatever else you may set up in your appsettings.json
configuration file. 这会设置一个默认的“控制台”接收器以及您在
appsettings.json
配置文件中设置的任何其他内容。
In your appsettings.json
you may have this, for example: 在你的
appsettings.json
你可能有这个,例如:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Error"
}
},
"WriteTo": [{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341"
}
}],
"Enrich": ["FromLogContext"],
"Properties": {
"Application": "MyApplication"
}
}
}
Now you can either write to the static Serilog.Log
: 现在你可以写入静态
Serilog.Log
:
Log.Information("JSON example {@Json}", json);
or to your injected Microsoft.Extensions.Logging.ILogger
: 或者注入
Microsoft.Extensions.Logging.ILogger
:
logger.LogInformation("JSON example {@Json}", json);
and both will write to the configured sinks. 并且两者都将写入配置的接收器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.