Serilog无法通过asp.net Core 2.2 API中的配置工作 [英] Serilog not working from configuration in asp.net core 2.2 API
本文介绍了Serilog无法通过asp.net Core 2.2 API中的配置工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
.UseStartup<Startup>();
}
public class Startup
{
public IContainer Container { get; private set; }
public Startup(IConfiguration configuration)
{
Log.Warning("test");
Configuration = configuration;
}
}
appsettings.json
appsettings.json
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Default": "Information",
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:\\test.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Information"
}
}
]
},
"AllowedHosts": "*"
}
我已经安装了所有软件包
I have all the packages installed
- Serilog,
- Serilog.AspCore,
- Serilog.Settings.Configuration,
- Serilog.Sink.File
推荐答案
您的配置用于RollingFile
,但您的软件包列表显示Serilog.Sinks.File
.这些是不同的.您需要添加Serilog.Sinks.RollingFile
程序包,它应该开始工作.
Your config is for RollingFile
but your package list says Serilog.Sinks.File
. These are different. You need to add the Serilog.Sinks.RollingFile
package and it should start working.
如果要使用File
接收器(如评论中提到的@Kirk,现在建议使用该选项),则需要将设置更改为
If you want to use the File
sink (which, as @Kirk mentioned in the comments, is the recommended option now) then you need to change the settings to
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\test.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Information"
}
}
注意
对于File
接收器,pathFormat
应该为path
请参见 Serilog文件接收器文档
这篇关于Serilog无法通过asp.net Core 2.2 API中的配置工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文