serilog-mssql / serilog-sinks-mssqlserver

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

Additional Column (CorrelationId) Returning Null Value #527

Closed ryanrolfe closed 1 month ago

ryanrolfe commented 7 months ago

Bug Report / Support Request Template

If you are opening a feature request, you can ignore this template. Bug reports and requests for assistance usually require the same basic information described below. This will help us more quickly reproduce and investigate the problem you're reporting. (If you are using Serilog.Sinks.MSSqlServerCore, that package is deprecated, please switch to Serilog.Sinks.MSSqlServer before reporting an issue.)

Please clearly describe what the SQL Sink is doing incorrectly: When adding an additional column for the CorrelationId, NULL is stored in the database table. This code works with version 6.3.0 of Serilog.Sinks.MSSqlServer but later versions don't save the CorrelationId. I can see the CorrelationId in both the LogEvent and Properties Columns. I have tried with and without the PropertyName being specified and also with different DataLength values. Other columns such as RequestUri work as expected.

Please clearly describe the expected behavior: The CorrelationId column should be populated

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

  • Serilog: 3.1.1
  • Serilog.Sinks.MSSqlServer: 6.6.0 (Issue occurs on version 6.4.0 and above)
  • (configuration, etc.)

Target framework and operating system:

[X] .NET 6 [ ] .NET Framework 4.8 [ ] .NET Framework 4.7 [ ] .NET Framework 4.6 OS: Windows 10

Provide a simple reproduction of your Serilog configuration code: var columnOptions = new ColumnOptions { AdditionalColumns = new Collection { new SqlColumn { ColumnName = "CorrelationId", PropertyName = "CorrelationId", DataType = SqlDbType.NVarChar}, new SqlColumn { ColumnName = "RequestUri", DataType = SqlDbType.NVarChar}, new SqlColumn { ColumnName = Constants.ApplicationKey, DataType = SqlDbType.VarChar }, new SqlColumn { ColumnName = Constants.InstanceKey, DataType = SqlDbType.VarChar } } };

serilogConfig .WriteTo .MSSqlServer( connectionString, sinkOptions: new MSSqlServerSinkOptions { TableName = "serilog", SchemaName = "dbo", AutoCreateSqlTable = true, EagerlyEmitFirstEvent = Convert.ToBoolean(appenderConfig.Args["eagerlyEmitFirstEvent"]), BatchPostingLimit = Convert.ToInt32(appenderConfig.Args["batchPostingLimit"]) }, columnOptions: columnOptions, restrictedToMinimumLevel: level);

Provide a simple reproduction of your Serilog configuration file, if any:

Provide a simple reproduction of your application code:

ryanrolfe commented 7 months ago

Just to provide an update on this, the DataType for the CorrelationId column needs to be SqlDbType.UniqueIdentifier in order for it to be populated in the database table.

This appears to be a breaking change between version 6.3.0 and 6.4.0.

ckadluba commented 7 months ago

Hi @ryanrolfe!

Thank you for telling us your finding. Can you please provide a small sample program which generates a log event that leads to a NULL value in the CorrelationId column? Thank you!

ckadluba commented 5 months ago

Perhaps this problem is fixed with #515 which is fixed in the latest dev build on nuget.org. Can you please confirm this?

pkokay commented 2 months ago

I am not the OP, but have a similar problem with versions 6.4.0+ of the Serilog.Sinks.MSSqlServer package, and the newest dev version did not fix it for me. Would it be best to open a different issue and provide a sample program?

ckadluba commented 1 month ago

@pkokay yes, please do that. I will close this issue.