serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
283 stars 148 forks source link

Error if enrich nullable int colums by null value. Started from 6.1.0 #458

Closed ogorodnikovInfopulse closed 1 year ago

ogorodnikovInfopulse commented 1 year ago

Bug Report

Error started on vesion 6.1.0

Please clearly describe what the SQL Sink is doing incorrectly:

  • if Log table has nullable int column and enrich it with null, error in AdditionalColumnDataGenerator.GetAdditionalColumnNameAndValue on scalarValue.Value.GetType() as scalarValue.Value is null

Please clearly describe the expected behavior:

  • Write log without any errors

List the names and versions of all Serilog packages used in the project:

Target framework and operating system:

  • .NET Core 6
using Serilog;
using Serilog.Context;
using Serilog.Sinks.MSSqlServer;
using System.Data;

var logDB = @"Data Source=....";
var sinkOpts = new MSSqlServerSinkOptions();
sinkOpts.TableName = "Logs113";
sinkOpts.AutoCreateSqlTable = true;

sinkOpts.BatchPeriod = TimeSpan.FromMilliseconds(100);

var columnOpts = new ColumnOptions();
columnOpts.Store.Remove(StandardColumn.Properties);
columnOpts.Store.Add(StandardColumn.LogEvent);

columnOpts.AdditionalColumns = new List<SqlColumn> 
{ 
    new SqlColumn(columnName: "someId", SqlDbType.Int, allowNull: true) //config nullable int
};

var log = new LoggerConfiguration()
    .WriteTo.MSSqlServer(
        connectionString: logDB,
        sinkOptions: sinkOpts,
        columnOptions: columnOpts
    )
    .Enrich.FromLogContext() //add enricher
    .CreateLogger();

LogContext.PushProperty("someId", null); //enrich
log.Information("www"); //write log

while (true) //wait writing log
{
    System.Threading.Thread.Sleep(100);
}
ckadluba commented 1 year ago

Hi @ogorodnikovInfopulse!

Thanks for reporting this. I was able to reproduce the bug. Working on a fix now.

ckadluba commented 1 year ago

Fixed in release 6.2.0. Please try it and let us know if it works for you.

vzalamea commented 1 year ago

6.2.0 worked for me. Thank you for fixing so quickly!!

ckadluba commented 1 year ago

Glad to hear that :). Thanks a lot for the feedback.

ogorodnikovInfopulse commented 1 year ago

Fixed, thanks