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 SQL Columns not populating #471

Closed kalyaniravi14 closed 1 year ago

kalyaniravi14 commented 1 year 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: Using Serilog with MSSQLServer sink in asp.net core 6.0. created a custom column "APPName" in the Log table and Tried to use Enrich.WithProperty("APPName","XYZ") to populate the Log table with APPName column. but it is not populating the custom column. here is the code used in program.cs var _logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).Enrich.FromLogContext() .Enrich.WithProperty("APPName","XYZ") .CreateLogger(); builder.Logging.AddSerilog(_logger);

Please clearly describe the expected behavior: expecting to populate custom column [APPName] with value "XYZ" along with existing columns [Id] ,[Message] ,[MessageTemplate] ,[Level] ,[TimeStamp] ,[Exception] ,[Properties]

List the names and versions of all Serilog packages used in the project: Serilog.AspNetCore Version="6.1.0" Serilog.Settings.Configuration Version="3.4.0" Serilog.Sinks.MSSqlServer Version="6.3.0"

Target framework and operating system: [ ] .NET 6

OS: Windows

Provide a simple reproduction of your Serilog configuration code: here is the code is Program.cs file var _logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).Enrich.FromLogContext() .Enrich.WithProperty("APPName","XYZ") .CreateLogger(); builder.Logging.AddSerilog(_logger);

Provide a simple reproduction of your Serilog configuration file, if any: here is the code in appsettings.json file "Serilog": { "Using": [ "Serilog.Sinks.MSSqlServer" ], "Minimumlevel": { "Default": "Information" }, "Enrich": [ "FromLogContext", "WithProperty" ], "WriteTo": [ { "Name": "MSSqlServer", "Args": { "ConnectionString": "Data Source=LocalDB;Initial Catalog=.....", "tableName": "Logs"
} } ] }

Provide a simple reproduction of your application code: Tried using
LogContext.PushProperty("{APPName}", "XYZ")

kalyaniravi14 commented 1 year ago

UPdating the config file with columnOption section also did not work "Serilog": { "Using": [ "Serilog.Sinks.MSSqlServer" ], "Minimumlevel": { "Default": "Information" }, "Enrich": [ "FromLogContext", "WithProperty" ], "WriteTo": [ { "Name": "MSSqlServer", "Args": { "ConnectionString": "Data Source=....", "tableName": "Logs" }, "columnOptionsSection": { "additionalColumns": [ { "ColumnName": "APPName", "DataType": "nvarchar", "AllowNull": 50 } ] } }

akjal commented 1 year ago

Can you try adding this to your Serilog DB connection string ;TrustServerCertificate=true

ckadluba commented 1 year ago

Hi @kalyaniravi14!

Can you please provide a simple but full program which demonstrates your problem. The code above is incomplete.

ciuly commented 1 year ago

You need to place columnOptionsSection inside Args. Also tableName should be inside a sinkOptionsSection which should also be in Args. The following WriteTo section works on current latest 6.3.0, it will create the table with the default columns + a new one called SomeIntPropertyName of type int, nullable, and will be filled with property values from the log lines that match "SomeIntPropertyName"

      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "...",
          "restrictedToMinimumLevel": "Information",
          "sinkOptionsSection": {
            "tableName": "Logs",
            "schemaName": "logs",
            "autoCreateSqlTable": true
          },
          "columnOptionsSection": {
            "additionalColumns": [
              { "ColumnName": "SomeIntPropertyName", "DataType": "int", "AllowNull": true }
            ]
          }
        }
      }

UPdating the config file with columnOption section also did not work "Serilog": { "Using": [ "Serilog.Sinks.MSSqlServer" ], "Minimumlevel": { "Default": "Information" }, "Enrich": [ "FromLogContext", "WithProperty" ], "WriteTo": [ { "Name": "MSSqlServer", "Args": { "ConnectionString": "Data Source=....", "tableName": "Logs" }, "columnOptionsSection": { "additionalColumns": [ { "ColumnName": "APPName", "DataType": "nvarchar", "AllowNull": 50 } ] } }

ckadluba commented 1 year ago

Thank you @ciuly for the answer!

@kalyaniravi14 does this solution solve your problem?