在.NET 6剃须刀页面中配置Serilog

发布于 2025-02-03 07:39:00 字数 1444 浏览 5 评论 0原文

我有一个基于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2025-02-10 07:39:00

您通过configurationsection而不是iconfiguration - 没有过多加载方法,它接受类似的参数。我想您应该通过builder.configuration

builder.Logging.AddConfiguration(builder.Configuration);

You pass ConfigurationSection instead of IConfiguration - there is no overloading method, which accepts parameter like that. I guess you should pass builder.Configuration instead:

builder.Logging.AddConfiguration(builder.Configuration);
风透绣罗衣 2025-02-10 07:39:00

添加Nuget软件包 -

Microsoft.Extensions.Logging.Configuration

并在program.cs文件中使用以下命名空间 -

using Microsoft.Extensions.Logging;

应该修复iloggingBuilder'不包含“ addConfiguration”错误的定义。

Add nuget package -

Microsoft.Extensions.Logging.Configuration

and use below namespace in Program.cs file -

using Microsoft.Extensions.Logging;

That should fix the ILoggingBuilder' does not contain a definition for 'AddConfiguration' error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文