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

Value written into custom column with double quotes #464

Closed miksh7 closed 1 year ago

miksh7 commented 1 year ago

We tried to log MachineName into a custom column defined as below

    <Columns>
      <add ColumnName="MachineName"
           PropertyName="MachineName"
           DataType="varchar"
           DataLength="256" />
    </Columns>

When we used Serilog.Enrichers.Environment with

Log.Logger = new LoggerConfiguration()
    .Enrich.WithMachineName()
    // ...other configuration...
    .CreateLogger();

the MachineName was saved into table with double quotes, e.g. "MyComputer".

When we used a custom property

Log.Logger = new LoggerConfiguration()
    .Enrich.WithProperty("MachineName", Environment.MachineName)
    // ...other configuration...
    .CreateLogger();

the MachineName was saved into table as expected without double quotes, e.g. MyComputer.

Latest serilog & sink versions were used, same issue on NET Framework 4.7.2 and NET6. Ideally, we'd like to use serilog enrichers. Any suggestion how to fix the case with Serilog.Enrichers.Environment ?

ckadluba commented 1 year ago

I tried to reproduce your problem by adding the environment enricher to WorkerServiceDemo included with the sink source. The sample is on .NET 6. The problem did not occurr. The machine name column was filles correctly without double quotes.

Does it really also occurr on .NET 6? Can you provide a complete sample program to demonstrate the error?

miksh7 commented 1 year ago

Let me use your sample project to reproduce the issue.

miksh7 commented 1 year ago

Eventually, I was able to reproduce the issue. First of all, I confirm that MachineName provided by Serilog.Enrichers.Environment is written into DB correctly, i.e. without double quotes.

The issue occurred in another enricher Serilog.Enrichers.Context which has the same method .WithMachineName(). Since in most cases we enable this enrichment in the config file then we mistakenly blamed Serilog.Enrichers.Environment

    "Enrich": [
      "FromLogContext",
      "WithMachineName"
    ],

I leave it to you to decide whether the bug is yours or not. Thank you!

ckadluba commented 1 year ago

Thanks for the feedback. Since the bug seems to be in Serilog.Enrichers.Context I kindly ask you to report it in the respective repo. I will close this issue because this problem is not related to the MSSQL sink.