gpt4 book ai didi

c# - asp.net core - 从 appsettings.json 设置 Serilog.Exceptions

转载 作者:行者123 更新时间:2023-12-05 03:05:27 26 4
gpt4 key购买 nike

我需要使用 Serilog.Exceptions 包来捕获异常。Serilog是从appsettings.json中读取的

{
"Serilog": {
"Using": [
"Serilog.Sinks.RollingFile",
"Serilog.Sinks.Seq"
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"restrictedToMinimumLevel": "Debug",
"pathFormat": "myPath\\log-{Date}.log"
}
},
{
"Name": "RollingFile",
"Args": {
"restrictedToMinimumLevel": "Error",
"pathFormat": "myPath\\error-{Date}.log"
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "myUrl",
"apiKey": "myApiKey"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithThreadId"
],
"Properties": {
"Application": "myApplicationName"
}
}
}

在我的startup.cs

var logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.ReadFrom.Configuration(Configuration)
.CreateLogger();

Log.Logger = logger;

但它不起作用。我是否需要在 appsettings.json 中为 Serilog.Exceptions 包添加一些其他属性?还是 appsettings.json 配置正确?我究竟做错了什么?谢谢

最佳答案

以下是我在 ASP.NET Core 2.1 中设置 Serilog 的方式。你可以把它当作 list ,看看你错过了什么。

  1. 从 Nuget 安装包:Serilog.AspNetCoreSerilog.ExceptionsSerilog.Settings.Configuration 和您要使用的其他接收器。
  2. 在您的主程序中设置Serilog:

    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((hostingContext, loggerConfiguration) =>
    loggerConfiguration
    .ReadFrom.Configuration(hostingContext.Configuration)
    );
    }
  3. WithExceptionDetails 放入 appsettings.jsonSerilog 部分:

    {
    "Serilog": {
    ...
    "Enrich": [
    "FromLogContext",
    "WithExceptionDetails"
    ],
    ...
    }
    }

关于c# - asp.net core - 从 appsettings.json 设置 Serilog.Exceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com