Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serilog not writing log to MSSql database when entry is longer than specific value #436

Closed
vuqluskr opened this issue Dec 1, 2022 · 1 comment

Comments

@vuqluskr
Copy link

vuqluskr commented Dec 1, 2022

Bug Report

Please clearly describe what the SQL Sink is doing incorrectly:

Serilog.Sinks.MSSqlServer, version 6.0.0

I created custom log table (automatically created), here is my configuration:

{
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "myConnectionString",
          "tableName": "TestLog",
          "autoCreateSqlTable": true,
          "columnOptionsSection": {
            "removeStandardColumns": [
              "MessageTemplate",
              "Properties",
              "Message",
              "Exception"
            ],
            "customColumns": [
              {
                "ColumnName": "CommandName",
                "DataType": "varchar",
                "DataLength": 50
              },
              {
                "ColumnName": "Client",
                "DataType": "varchar",
                "DataLength": 5000
              },
              {
                "ColumnName": "ServiceType",
                "DataType": "varchar",
                "DataLength": 20
              },
              {
                "ColumnName": "CommandType",
                "DataType": "varchar",
                "DataLength": 20
              },
              {
                "ColumnName": "RequestBody",
                "DataType": "varchar",
                "DataLength": 8000
              },
              {
                "ColumnName": "ResponseBody",
                "DataType": "varchar",
                "DataLength": 8000
              },
              {
                "ColumnName": "HttpResponseCode",
                "DataType": "int",
                "DataLength": 10
              }

            ]
          }
        }
      },

I use it like this:
_logger .ForContext("CommandName", command.Name) .ForContext("Client", client) .ForContext("Date", DateTime.Now) .ForContext("ServiceType", command.ServiceType) .ForContext("CommandType", command.CommandType) .ForContext("HttpResponseCode", (int)response.StatusCode) .ForContext("RequestBody", requestBody) .ForContext("ResponseBody", responseBody) .Error("test");
The problem is, in my case ResponseBody is ~6.8k symbols. When I comment out the line
.ForContext("ResponseBody", responseBody)
logging works. I tried to set DataLength to -1 (or 8k like in current case) and re-create the table, still not working.

Please clearly describe the expected behavior:

To be able to log entries with more than (I guess) 5000 symbols.

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

  • Serilog: 2.12.0
  • Serilog.AspNetCore: 6.0.1
  • Serilog.Sinks.MSSqlServer: 6.0.0
  • Serilog.Sinks.Elasticsearch: 8.4.1
  • Serilog.Sinks.Console: 4.1.0
  • Serilog.Settings.Configuration:3.4.0
  • (configuration, etc.) :
{
  "Logging": {
    "LogLevel": {
      "Default": "Error",
      "Microsoft.AspNetCore": "Error"
    }
  },
  "Serilog": {
    "MinimumLevel": "Error",
    "Override": {
      "Microsoft": "Error",
      "System": "Error",
      "Microsoft.Hosting.Lifetime": "Error",
      "Microsoft.EntityFrameworkCore": "Error",
      "Elastic": "Error"
    },
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "myConntectionString",
          "tableName": "TestLog",
          "autoCreateSqlTable": true,
          "columnOptionsSection": {
            "removeStandardColumns": [
              "MessageTemplate",
              "Properties",
              "Message",
              "Exception"
            ],
            "customColumns": [
              {
                "ColumnName": "CommandName",
                "DataType": "varchar",
                "DataLength": 50
              },
              {
                "ColumnName": "Client",
                "DataType": "varchar",
                "DataLength": 5000
              },
              {
                "ColumnName": "ServiceType",
                "DataType": "varchar",
                "DataLength": 20
              },
              {
                "ColumnName": "CommandType",
                "DataType": "varchar",
                "DataLength": 20
              },
              {
                "ColumnName": "RequestBody",
                "DataType": "varchar",
                "DataLength": 8000
              },
              {
                "ColumnName": "ResponseBody",
                "DataType": "varchar",
                "DataLength": 8000
              },
              {
                "ColumnName": "HttpResponseCode",
                "DataType": "int",
                "DataLength": 10
              }

            ]
          }
        }
      },
      {
        "Name": "Elasticsearch"
      }
    ]
  }

Program.cs:

builder.Host.UseSerilog((ctx, cfg) =>
{
    cfg.Enrich.FromLogContext()
        .WriteTo.Debug()
        .WriteTo.Console()
    .WriteTo.Elasticsearch(ConfigureElasticSink(ctx.Configuration))
        .Enrich.WithProperty("Environment", ctx.HostingEnvironment)
        .ReadFrom.Configuration(ctx.Configuration);
        //.CreateLogger();
    //cfg.WriteTo.Elasticsearch(ConfigureElasticSink(ctx.Configuration));
    //cfg.ReadFrom.Configuration(ctx.Configuration);

});
builder.Services.AddLogging();

Target framework and operating system:

[ ] .NET Core 6.0
OS: Windows 10 Pro

Provide a simple reproduction of your Serilog configuration code:

can be found above

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

can be found above

Provide a simple reproduction of your application code:

can be found above

@ckadluba
Copy link
Member

ckadluba commented Dec 1, 2022

Hi @vuqluskr!

The sink is able to truncate values exceeding the specified DataLength but only for the standard columns Message and MessageTemplate (as described here: https://github.com/serilog-mssql/serilog-sinks-mssqlserver#message).

We have currently have no plans or resources to implement a truncation for addidional columns like in your scenario but are always open for a pull request.

If you look for a way to work around this, take a look at Serilog enrichers. These are custom methods called upon log event generation which allow you to do modifications to the log event data (like truncating a value).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants