0

As the title says, I'm struggling with Serilog's subloggers configuration (from appsettings.json).

I am trying to have two subloggers: the first one is going to log only information level logs that I sent from services, and the second one must log everything (likely to ASP.NET Core default logging) to a console.

Now I will post two appsettings.json snippets that I tried and explain what they are doing

With this one both subloggers are logging only logs sent from services (probably due global Microsoft error namespace overriding):

"Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "Error"
        }
    },
    "WriteTo": [
        {
            "Name": "MSSqlServer",
            "Args": {
                "connectionString": "Server=localhost\\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
                "tableName": "ActivityLogs",
                "autoCreateSqlTable": false,
                "autoCreateSqlDatabase": false,
                "columnOptionsSection": {
                    "customColumns": [
                        {
                            "ColumnName": "UserId",
                            "DataType": "nvarchar"
                        },
                        {
                            "ColumnName": "ItemId",
                            "DataType": "int"
                        }
                    ]
                }
            }
        },
        {
            "Name": "Console",
            "Args": {
                "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
            },
            "MinimumLevel": "Verbose"
        }
    ]
}

This one causes console log everything as needed, but the database sublogger doesn't log anything:

"Serilog": {
     "MinimumLevel": {
     "Default": "Information"
     },
     "WriteTo": [
      {
       "Name": "MSSqlServer",
       "Args": {
         "connectionString": "Server=localhost01\\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
        "tableName": "ActivityLogs",
        "autoCreateSqlTable": false,
        "autoCreateSqlDatabase": false,
        "columnOptionsSection": {
          "customColumns": [
            {
              "ColumnName": "UserId",
              "DataType": "nvarchar"
            },
            {
              "ColumnName": "ItemId",
              "DataType": "int"
            }
          ]
        }
      }
    },
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
      },
      "MinimumLevel": "Verbose"
    }
  ]
}

I understand where the problem can be, but still can't fix that after many attempts

2
  • can you elaborate on this --> "probably due global microsoft error namespace overriding" Since you think this is the problem it might help to have more explanation. Commented Feb 15, 2024 at 20:46
  • @topsail this two json configurations differ only in "Override": { "Microsoft": "Error" } section(second one doesnt have at all) as i think by overriding microsoft error i can exclude unnecessary logging and therefore i see only logs called from my code. But i cant explain why removing this one causes database logging not working Hope i could explain it clearly Commented Feb 15, 2024 at 20:52

1 Answer 1

0

Okay, it's seems that i got it working properly

this is a working appsettings.json configuration:

"Serilog": {
  "MinimumLevel": {
    "Default": "Information"
  },
  "Using": [ "Serilog.Expressions" ],
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "configureLogger": {
          "WriteTo": [
            {
              "Name": "MSSqlServer",
              "Args": {
                "connectionString": "Server=localhost\SQLEXPRESS01;Database=AuctionDb;Trusted_Connection=TRUE;Encrypt=False",
                "tableName": "ActivityLogs",
                "autoCreateSqlTable": false,
                "autoCreateSqlDatabase": false,
                "columnOptionsSection": {
                  "customColumns": [
                    {
                      "ColumnName": "UserId",
                      "DataType": "nvarchar"
                    },
                    {
                      "ColumnName": "ItemId",
                      "DataType": "int"
                    }
                  ]
                }
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByExcluding",
              "Args": {
                "expression": "StartsWith(SourceContext, 'Microsoft.')"
              }
            }
          ]
        }
      }
    }
  ]
}

Solution was to filter out Microsoft. namespace errors with Serilog.Expressions package and ensuring that it will be used in Using section of Serilog configuration.

Sign up to request clarification or add additional context in comments.

4 Comments

This does not seem to match your original problem (which was NO logging) since by adding an excluding filter you are doing less logging (which means the problem was TOO MUCH logging). However, glad it is working!
@topsail Yeah, you a right, serilog tried to log events from Microsoft namespace which dont have any property matching the custom columns I added to my database sink and due to the fact that this columns dont allow null values serilog was throwing an exception. I wasnt aware that there is self-logging feature in serilog, it really helped me to debug this issue. And it took me a while to find a way to override Microsoft namespace ONLY for the database sink.
I see. This makes me wonder if you could have got the original error (due to the column nullability) by looking at the Serilog self-logging.
if you want I can try to reproduce this situation and send you exception serilog was throwing after enabling self-logging

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.