Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LevelSwitch does not update through appsetting.json #504

juanes1220 started this conversation in General
Discussion options

Hello, I'm trying to get the LevelSwitch property to update through the appsetting.json file but I haven't had any luck.
This is how I'm setting Serilog

"Serilog": {
 "Using": [
 "Serilog.Sinks.MSSqlServer"
 ],
 "LevelSwitches": { "controlSwitch": "Information" },
 "MinimumLevel": {
 "Default": "Debug",
 "Override": {
 "Microsoft": "Information",
 "Microsoft.AspNetCore": "Warning",
 "Microsoft.EntityFrameworkCore": "Warning"
 }
 },
 "WriteTo": [
 {
 "Name": "MSSqlServer",
 "Args": {
 "connectionString": "ConnectionName",
 "sinkOptionsSection": {
 "tableName": "Logs",
 "autoCreateSqlTable": false,
 "levelSwitch": "$controlSwitch"
 },
 "restrictedToMinimumLevel": "Information",
 "columnOptionsSection": {
 "disableTriggers": true,
 "removeStandardColumns": [ "Id", "MessageTemplate" ],
 "additionalColumns": [
 {
 "ColumnName": "DeviceSerial",
 "DataType": "bigint",
 "AllowNull": true,
 "PropertyName": "CorrelationId"
 }
 ],
 "properties": {
 "DataType": "xml"
 }
 }
 }
 }
 ],
 "Enrich": [
 "FromLogContext",
 {
 "Name": "WithCorrelationId",
 "Args": {
 "headerName": "serial",
 "addValueIfHeaderAbsent": true
 }
 }
 ],
 "Properties": {
 "Application": "ApplicationName"
 }
 }

When I change the controlSwitch from Information to Debug, I should see debug events in the database but this isn't happening. I tested the same setup with the Console sink and it does apply the level change during runtime.

The sample code doesn't have any examples on how to set this up in the appsettings file but I found this in one of the examples

 private static void UseLevelSwitchToModifyLogLevelDuringRuntime(LoggingLevelSwitch levelSwitch)
 {
 levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Error;
 Log.Information("This should not be logged");
 Log.Error("This should be logged");
 levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Information;
 Log.Information("This should be logged again");
 }

Does the package support changing the log level through the appsettings or do I have to set it through code?

Thanks in advance for your help.

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hi @juanes1220!

Thanks for the heads up! We had such an issue reported a while ago (#470) but then closed it recently because I was not able to reproduce the problem quickly and the reporter did not respond anymore.

From what I understood from the Serilog core documents and other Sinks, there should be a possibility to set the switch in the config file during runtime. It just seems not to work currently with our MSSQL sink.

Can you please give us a complete small sample app to reproduce your problem?

You must be logged in to vote
1 reply
Comment options

Hello, thanks for your quick response. Here is the link to the sample that you requested. https://github.com/juanes1220/DatabaseLogTest . I used postman to query the endpoint using https://localhost:5001/api/databaselogtest/test . The program logs two messages, one at information level, which will always log and one at the debug level which will only log if the controlSwitch in the appsettings is set to debug. The project also has the Console sink enabled and it detects the controlSwitch change from Information to Debug and vice versa. If you look at the database log entries, they are always at the information level regardless of the controlSwitch state. I hope this helps.

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

AltStyle によって変換されたページ (->オリジナル) /