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

Custom property columns not populating with ForContext unless converted to string #584

Closed psiservices-ketan-javia closed 1 month ago

psiservices-ketan-javia commented 1 month ago

Given this config

"columnOptionsSection": {
    "additionalColumns": [
      {
        "AllowNull": true,
        "ColumnName": "ColA",
        "DataType": "varchar",
        "DataLength": 100
      },
      {
        "AllowNull": true,
        "ColumnName": "ColB",
        "DataType": "nvarchar",
        "DataLength": 50
      }
    ]
  }

Code doing logging below only populates the columns if I call ToString() on both properties.

  var pid = Guid.NewGuid();
  var cid = 12345;
  _logger.ForContext("ColA", pid.ToString()) // column values are null if I remove ToString()
              .ForContext("ColB", cid.ToString())
      .Information("Hello", pid, cid);

I would assume it would convert to string. Not a huge deal either way but took a while to figure this odd behavoir out. Not sure if I am missing any settings.

Packages

    <PackageReference Include="Serilog" Version="4.0.2" />
    <PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
    <PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
    <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
    <PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
    <PackageReference Include="Serilog.Sinks.MSSqlServer" Version="7.0.1"/>
    <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />

.netCore 8 OS: MacOS / Windows

psiservices-ketan-javia commented 1 month ago

Even normal logging statements (no ForContext, just message template with those 2 columns) behave the same way.

ckadluba commented 1 month ago

Hi @psiservices-ketan-javia!

Have you tried to activate SelfLog as described in our documentation under Troubleshooting?

Without doing any debugging, I guess the reason is that you are passing the values to the Information() method with the wrong types.

psiservices-ketan-javia commented 1 month ago

Yes I have the debugging enabled. Changing the column types to match my variable types works. So it appears that Serilog doesn't implticlty try to convert to match the target data type.

ckadluba commented 1 month ago

No, it does not. You have to pass the property values with the correct types.