将 Serilog 连接到 MSSQL 数据库的细节和基础是什么?

What are the specifics and basics of connecting Serilog to a MSSQL database?

将 Serilog 连接到 MSSQL 数据库的细节和基础是什么?这是我到目前为止所拥有的。你能告诉我哪里错了吗?

在 app.config 我有:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <connectionStrings>
    <add name="LogConnectionString" connectionString="Data Source=data.dev.db.com,14330;Integrated Security=SSPI;Database=DNA_Logs;"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

在我的 C# 代码中:

public Guid transactionGuid = Guid.NewGuid();

public string transactionGUIDString = string.Empty;

public StringWriter TraceLog = new StringWriter();

public void HelloLog()
{
    this.transactionGUIDString = transactionGuid.ToString("B").ToUpper();

    var logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .Enrich.WithProperty("TransactionId", this.transactionGuid)
        .WriteTo.MSSqlServer("LogConnectionString", "Serilog_Logs")
        .WriteTo.TextWriter(TraceLog)
        .CreateLogger();

    // Output logs
    logger.Information("Hello, Serilog!");
}

数据库table配置如下:

所以我有几个问题。首先,由于数据库 table 在技术上被命名为 "dbo.Serilog_Logs" C# 代码不应该使用这个 table 名称而不仅仅是 "Serilog_Logs" 吗?

我找到了一些 Serilog 的示例代码,但它只写入控制台。我想修改此示例代码以写入 MSSQL Server。我想知道 table 的列应该如何配置。

如何创建 table 结构以及如何配置 Sink 的文档都在存储库的 README 中描述

https://github.com/serilog/serilog-sinks-mssqlserver/


Serilog.Sinks.MSSqlServer

A Serilog sink that writes events to Microsoft SQL Server. This sink will write the log event data to a table and can > optionally also store the properties inside an XML or JSON column so they can be queried. Important properties can also be > written to their own separate columns.

Package - Serilog.Sinks.MSSqlServer | Minimum Platforms - .NET Framework 4.5.2, .NET Core 2.0, .NET Standard 2.0

Topics

dbo.Serilog_Logs 默认模式名称是 dbo 如果您没有指定它,您可以使用下面的 SQL 语句创建自己的模式并使用 [= 在配置方法参数中提及它=15=]

CREATE SCHEMA LOG;

还要确保数据库存在并且你可以访问它抛出你的连接字符串,它的数据库不存在用下面的SQL命令创建新的

CREATE DATABASE Serilog_DB;

要配置 table 列,您可以使用 columnOptions 并使用 ColumnOptions 定义列,如下例

var connectionString = ConfigurationManager.ConnectionStrings["LogConnectionString"].ToString();
var tableName = "Serilog_Logs";
var customColumnOptions = new ColumnOptions();
customColumnOptions.Store.Remove(StandardColumn.Properties); // Remove Column
customColumnOptions.Store.Add(StandardColumn.LogEvent); // Add new column to default columns

var logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .Enrich.WithProperty("TransactionId", transactionGuid)
    .WriteTo.MSSqlServer(connectionString: connectionString
        , tableName: tableName
        , schemaName: "LOG" // Schema should be create in database first
        , columnOptions: customColumnOptions
        , autoCreateSqlTable: true) // Will create the table if a table by that name doesn't exist
    .WriteTo.Console()
    .CreateLogger();

结果应该如下图