设置Serilog以将日志写入SQL Server数据库表

时间:2017-10-25 20:21:54

标签: c# sql-server logging serilog

我正在尝试设置Serilog将日志写入我的db表。

以下是我尝试设置日志记录到SQL Server的细分。

1)安装Nuget Packages,Serilog和Serilog.Sinks.MSSqlServer

2)我建立了一个名为Logger.cs的类,其中包含Serilog的所有配置:

public static class Logger
{
    private static readonly ILogger _Logger;

    static Logger()
    {

        var connStr = "data source=DESKTOP-BLAH;Initial Catalog=DBNAME;Trusted_Connection=True;";

        _Logger = new LoggerConfiguration()
            .WriteTo.MSSqlServer(connStr, "ApplicationLogs", 
            columnOptions: GetSqlColumnOptions(), restrictedToMinimumLevel: LogEventLevel.Debug, batchPostingLimit: 1)
            .CreateLogger();

        Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

    }

    public static ColumnOptions GetSqlColumnOptions()
    {
        var colOptions = new ColumnOptions();

        colOptions.Store.Remove(StandardColumn.Properties);
        colOptions.Store.Remove(StandardColumn.MessageTemplate);
        colOptions.Store.Remove(StandardColumn.Message);
        colOptions.Store.Remove(StandardColumn.Exception);
        colOptions.Store.Remove(StandardColumn.TimeStamp);
        colOptions.Store.Remove(StandardColumn.Level);

        colOptions.AdditionalDataColumns = new Collection<DataColumn>
        {
            new DataColumn{DataType = typeof(DateTime), ColumnName = "LogTimeStamp"},
            new DataColumn{DataType = typeof(Int32), ColumnName = "RecordNum"},
            new DataColumn{DataType = typeof(string), ColumnName = "ComputerName"},
            new DataColumn{DataType = typeof(DateTime), ColumnName = "ProcessTimeStamp"},
            new DataColumn{DataType = typeof(string), ColumnName = "LogGroup"},
            new DataColumn{DataType = typeof(string), ColumnName = "Type"},
            new DataColumn{DataType = typeof(Int32), ColumnName = "EventId"},
            new DataColumn{DataType = typeof(string), ColumnName = "UserId"},
            new DataColumn{DataType = typeof(Int32), ColumnName = "Line"},
            new DataColumn{DataType = typeof(string), ColumnName = "Description"},
            new DataColumn{DataType = typeof(string), ColumnName = "Source"},
            new DataColumn{DataType = typeof(string), ColumnName = "Data"},
            new DataColumn{DataType = typeof(DateTime), ColumnName = "AddTimestamp"},
            new DataColumn{DataType = typeof(string), ColumnName = "DeviceID"}

        };
        return colOptions;
    }


    public static void WriteError(ApplicationLog infoToLog)
    {
        try
        {        
            _Logger.Error(LogEventLevel.Error,
                    "{LogTimestamp}{RecordNum}{ComputerName}{ProcessTimeStamp}{LogGroup}" +
                    "{Type}{EventId}{UserId}{Line}" +
                    "{Description}{Source}{Data}{AddTimestamp}{DeviceID}",
                    infoToLog.LogTimestamp, infoToLog.RecordNum,
                    infoToLog.ComputerName, infoToLog.ProcessTimestamp,
                    infoToLog.LogGroup, infoToLog.Type,
                    infoToLog.EventId, infoToLog.UserId,
                    infoToLog.Line, infoToLog.Description,
                    infoToLog.Source, infoToLog.Data,
                    infoToLog.AddTimestamp, infoToLog.DeviceId);
        }
        catch(Exception ex)
        {

        }
    }
}

3)然后我相信我应该好好去,我这样设置:

ApplicationLog appLog = new ApplicationLog()
{
    LogTimestamp = DateTime.Now,
    RecordNum = 1,
    ComputerName = Environment.MachineName,
    ProcessTimestamp = DateTime.Now,
    LogGroup = "10",
    Type = "T",
    EventId = 23,
    UserId = Environment.UserName,
    Line = 0,
    Description = "Duplicate Failed to Upload",
    Source = "FooController.cs",
    Data = "DBNAME",
    AddTimestamp = DateTime.Now,
    DeviceId = "15"

};

Logger.WriteError(appLog);

4)已经在我的数据库中创建了表,只是等待数据从日志记录填充。

但是,它似乎仍然没有保存在我的SQL Server数据库表中。

我想知道我是否错过了任何步骤或我在这里设置的错误?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用serilog的自记录功能来查看发生了什么吗?

如果您使用的是.net core,请在Program.main()中放入以下内容

/** For stylistics only **/
td {
  border: 1px solid;
  padding: .5em 1em;
}

答案 1 :(得分:0)

您应该写

_Logger.Dispose();

之后

_Logger.Error(LogEventLevel.Error,
                "{LogTimestamp}{RecordNum}{ComputerName}{ProcessTimeStamp}{LogGroup}" +
                "{Type}{EventId}{UserId}{Line}" +
                "{Description}{Source}{Data}{AddTimestamp}{DeviceID}",
                infoToLog.LogTimestamp, infoToLog.RecordNum,
                infoToLog.ComputerName, infoToLog.ProcessTimestamp,
                infoToLog.LogGroup, infoToLog.Type,
                infoToLog.EventId, infoToLog.UserId,
                infoToLog.Line, infoToLog.Description,
                infoToLog.Source, infoToLog.Data,
                infoToLog.AddTimestamp, infoToLog.DeviceId);