在AppSettings.json中为Serilog接收器MsSqlServer配置列选项 [英] Configure Column Options for Serilog Sinks MsSqlServer in AppSettings.json

查看:257
本文介绍了在AppSettings.json中为Serilog接收器MsSqlServer配置列选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定是否可以为ASP.Net Core 2项目的appsettings.json文件中的serilog sink mssqlserver配置列选项.

I'm trying to determine if it's possible to configure the column options for serilog sink mssqlserver in the appsettings.json file for an ASP.Net Core 2 project.

我在Program.cs文件中创建并配置记录器.

I create and configure the logger in the Program.cs file.

        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();

        public static int Main(string[] args)
        {



            Log.Logger = new LoggerConfiguration()
                //.Enrich.WithProperty("AppName", "One Badass App") // Adds property to XML structure in properties column
                .ReadFrom.Configuration(Configuration)
                .CreateLogger();

                try
                {
                    Log.Information("Starting web host");
                    BuildWebHost(args).Run();
                    return 0;
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Host terminated unexpectedly");
                    return 1;
                }
                finally
                {
                    Log.CloseAndFlush();
                }
        }

我可以从appsettings.json文件构建配置文件,该文件包含一个Serilog节点,其中包含有关要使用哪个连接字符串和表的信息.

I can build the configuration file from the appsettings.json file, which contains a Serilog node with information for which connection string and table to use.

{
  "AppSettings": {
    "Application": {
      "Name": "Payment Processing API",
      "Version":  "1.0"
    }
  },
  "ConnectionStrings": {
    "localPaymentProcessingDb": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true",
          "tableName": "Logs"
        }
      }
    ]
 }

Github 有一个未解决的问题,但是我尚未找到任何其他相关信息.

There's an open issue on Github for this, but I haven't found any other information about it.

如果无法在appsettings.json中配置列选项,则应在ASP.Net Core 2项目中在何处以及如何配置它们?

If column options can't be configured in the appsettings.json, where and how should they be configured in an ASP.Net Core 2 project?

推荐答案

如果其他人偶然发现了同一问题,则问题中链接的GitHub问题现在包含答案:

If anyone else stumbles across this same issue the linked GitHub issue in the question now contains the answer:

现在可以使用最新的SQL Sink和 Serilog.Settings.Configuration程序包.

This is now possible with the latest SQL sink and Serilog.Settings.Configuration packages.

此外,我知道这个问题要求提供.NET Core 2答案,听起来像链接的GitHub页面为2给出了答案,我正在使用.NET Core 3.1,以下内容对我有用(希望它对2有用)也是

Also, I know this question asks for a .NET Core 2 answer, and it sounds like the linked GitHub page answers this for 2, I am using .NET Core 3.1 and the following worked for me (hopefully it works for 2 as well)

我安装了Serilog.Settings.Configuration Nuget软件包,并使用了以下Serilog appsettings.json配置:

I installed the Serilog.Settings.Configuration Nuget package and used the following Serilog appsettings.json configuration:

"Serilog":{
   "MinimumLevel":"Information",
   "WriteTo":[
      {
         "Name":"MSSqlServer",
         "Args":{
            "connectionString":"DbContext",
            "tableName":"EventLog",
            "autoCreateSqlTable":true,
            "columnOptionsSection":{
               "addStandardColumns":[
                  "LogEvent"
               ],
               "removeStandardColumns":[
                  "MessageTemplate",
                  "Properties"
               ]
            }
         }
      }
   ]
}

这篇关于在AppSettings.json中为Serilog接收器MsSqlServer配置列选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆