在.NET 6剃须刀页面中配置Serilog
我有一个基于Razor Pages(.NET 6)的Web Progressive应用程序,并且我无法通过文件中的文件配置Serilog。
我的program.cs
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Serilog;
using Serilog.Core;
using Serilog.Extensions.Logging;
using Microsoft.Extensions.Logging;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Serilog")).AddSerilog();
我的.csproj
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />
</ItemGroup>
我的appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3}] {Username} {Message:lj}{NewLine}{Exception}"
}
}
]
}
我正在注入剃须刀页面上的日志,例如:
@inject ILogger<Edit> _logger
日志未写入我配置的日志文件。
I have a web progressive app based on razor pages (.net 6) and I cant configure serilog by file in my project.
My program.cs
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Serilog;
using Serilog.Core;
using Serilog.Extensions.Logging;
using Microsoft.Extensions.Logging;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Serilog")).AddSerilog();
My .csproj
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />
</ItemGroup>
My appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3}] {Username} {Message:lj}{NewLine}{Exception}"
}
}
]
}
I'm injecting the log on my razor pages like:
@inject ILogger<Edit> _logger
The logs are not written to the log file I configured.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通过
configurationsection
而不是iconfiguration
- 没有过多加载方法,它接受类似的参数。我想您应该通过builder.configuration
:You pass
ConfigurationSection
instead ofIConfiguration
- there is no overloading method, which accepts parameter like that. I guess you should passbuilder.Configuration
instead:添加Nuget软件包 -
并在
program.cs
文件中使用以下命名空间 -应该修复
iloggingBuilder'不包含“ addConfiguration”
错误的定义。Add nuget package -
and use below namespace in
Program.cs
file -That should fix the
ILoggingBuilder' does not contain a definition for 'AddConfiguration'
error.