英文:
Why serilog not logging to file?
问题
以下是翻译好的部分:
"WriteTo": [
{
"Name": "File"
},
{
"Name": "File",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
}
]
在你的应用程序的appsettings.json文件中有上述内容。
为什么Serilog没有将日志写入 "path": "Logs/Error/applog.log"?
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Logger.Information("Hello, world!");
你的设置文件如下:
{
"Logging": {
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"HrSoultion": "server=.;database=Maqta;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
}
}
英文:
"WriteTo": [
{
"Name": "File"
},
{
"Name": "File",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
}
I have this in my app setting.json file?
Why serilog not writting to "path": "Logs/Error/applog.log", ?
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Logger.Information("Hello, world!");
I have this setting file ?
{
"Logging": {
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"HrSoultion": "server=.;database=Maqta;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
}
}
答案1
得分: 2
Your configuration in appsettings.json
doesn't seem to be correct.
If you provide this configuration, it should work:
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
My result:
For reference, I used .NET 6 and these NuGet packages:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
Links:
英文:
Your configuration in appsettings.json
doesn't seem to be correct.
If you provide this configuration, it should work:
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/Error/applog.log",
"outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
My result:
For reference, I used .NET 6 and these NuGet packages:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
Links:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论