【问题标题】:Serilog mssql sink SqlColumn data typeSerilog mssql sink SqlColumn 数据类型
【发布时间】:2019-04-03 05:16:43
【问题描述】:

我正在尝试在我的 SQL Server 日志记录中添加一个附加列。在示例中,它们给出的内容与以下内容非常相似:

var columnOptions = new ColumnOptions
        {
            AdditionalDataColumns = new Collection<SqlColumn>
            {
                new SqlColumn { DataType = SqlDbType.NVarChar, DataLength = 20, ColumnName = "B" }
            }
        }

我遇到的问题是我的编译器一直在抱怨 SqlColumn 类型的用法,说它是未知的。我正在使用.Net 4.6.1。我需要在使用中添加什么?我已经有了以下内容:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using System.Net.Http.Formatting;
using Dapper;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Jwt;
using Newtonsoft.Json.Serialization;
using Owin;
using Serilog;
using Serilog.Filters;
using Serilog.Sinks.MSSqlServer;
using SerilogWeb.Classic.WebApi;

编辑

我刚刚尝试构建一个全新的 web api 项目,以下代码也不起作用,由于某种原因我无法访问 SqlColumn 类型。请注意,错误是命名空间中不存在该类型(不是构造函数引起的错误)。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
using Serilog;
using Serilog.Sinks.MSSqlServer;

[assembly: OwinStartup(typeof(TestProject.Startup))]

namespace TestProject
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            var x = new Serilog.Sinks.MSSqlServer.SqlColumn();
        }
    }
}

【问题讨论】:

  • 它定义在 Serilog.Sinks.MSSqlServer 命名空间根据来源:github.com/serilog/serilog-sinks-mssqlserver/blob/… 你有正确的 Nuget 吗?也许这个nuget.org/packages/Serilog.Sinks.MSSqlServer/5.1.2
  • @mortb,我什至试图明确指定它,但它错误地说 The type or namespace name 'SqlColumn' does not exist in the namespace 'Serilog.Sinks.MSSqlServer' (are you missing an assembly reference?)
  • 另外,我使用的是 5.1.2 版本的 NuGet 包
  • 我可以在 Serilog.Sinks.MSSqlServer 上看到的唯一可用类如下:ColumnOptions、MSSqlServerAuditSink、MSSqlServerSink、StandardColumn 和 XmlPropertyFormatter。

标签: c# serilog


【解决方案1】:

您需要将Serilog.Sinks.MSSqlServer 更新到预发布版本5.1.3-dev-00232,因为它在版本5.1.2中不存在

Install-Package Serilog.Sinks.MSSqlServer -Version 5.1.3-dev-00232

或者您可以使用DataColumn 类型添加列 5.1.2 如下

var columnOptions = new ColumnOptions
{
    AdditionalDataColumns = new Collection<DataColumn>
    {
        new DataColumn {DataType = typeof (string), ColumnName = "User"},
        new DataColumn {DataType = typeof (string), ColumnName = "Other"},
    }
};

var log = new LoggerConfiguration()
    .WriteTo.MSSqlServer(@"Server=.\SQLEXPRESS;Database=LogEvents;Trusted_Connection=True;", "Logs", columnOptions: columnOptions)
    .CreateLogger();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多