[英]Serilog not working from configuration in 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 我安装了所有软件包
Your config is for RollingFile
but your package list says Serilog.Sinks.File
. 您的配置适用于RollingFile
但您的包列表显示为Serilog.Sinks.File
。 These are different. 这些是不同的。 You need to add the Serilog.Sinks.RollingFile
package and it should start working. 你需要添加Serilog.Sinks.RollingFile
包,它应该开始工作。
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 如果你想使用File
器(正如评论中提到的@Kirk,现在是推荐的选项),那么你需要将设置更改为
"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"
}
}
NOTE 注意
The pathFormat
should be path
for the File
sink pathFormat
应该是File
器的path
See the Serilog File Sink Documentation 请参阅Serilog文件接收器文档
It seems the problem was writing directly to c: and not using File in config which seems not to work, but writing to a different directory works. 似乎问题是直接写入c:而不是在配置中使用File似乎不起作用,但写入不同的目录是有效的。
Here is the final solution for anyone coming across this 对于遇到此问题的人来说,这是最终解决方案
Program.cs Program.cs中
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog();
}
Startup.cs Startup.cs
public Startup(IConfiguration configuration)
{
var logConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(configuration);
Log.Logger = logConfiguration.CreateLogger();
Configuration = configuration;
}
appsettings.json appsettings.json
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\temp\\log.txt",
"rollingInterval": "Day"
}
}
]
} }
This is not an answer but an observation. 这不是一个答案,而是一个观察。 I had observed a definite problem here. 我在这里发现了一个明确的问题。 I have got three settingss files. 我有三个设置文件。
I have selected the profile from launchSettings.json such that the environment is VivekDev as follows. 我从launchSettings.json中选择了配置文件,使得环境是VivekDev,如下所示。
"ASPNETCORE_ENVIRONMENT": "VivekDev"
Now when i have the following setting in appsettings.VivekDev.json without having any Serilog setting in appsettings.json , then things are working fine. 现在,当我在appsettings.VivekDev.json中有以下设置而没有在appsettings.json中设置任何Serilog时 ,那么事情正常。
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day"
}
}
]
},
But when I have 但是当我有
"Serilog": {
"WriteTo": [ "Debug" ]
},
in appsettings.json , this is somehow overriding the setting in appsettings.VivekDev.json . 在appsettings.json中 ,这在某种程度上覆盖了appsettings.VivekDev.json中的设置。 As per my understanding this is incorrect and the setting inside of appsettings.VivekDev.json should override appsettings.json . 根据我的理解,这是不正确的, appsettings.VivekDev.json中的设置应该覆盖appsettings.json 。
And this is happening only in case of File sink. 只有在文件接收器的情况下才会发生这种情况。 If I use other sink such as console then things are working as expected. 如果我使用其他接收器,如控制台,那么事情正在按预期工作。 That is the serilog setting inside of appsettings.VivekDev.json is correctly overriding the serilog setting of appsettings.json . 也就是说appsettings.VivekDev.json的serilog设置内部正确覆盖appsettings.json的serilog设置。
Something is definitely buggy here. 这里肯定有些东西。
So of the now, I have removed the serilog settings in the appsettings.json and kept the serilog write to file setting in appsettings.VivekDev.json . 所以现在,我已删除的appsettings.json的serilog设置,并保持serilog写在appsettings.VivekDev.json到文件设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.