Closed ogorodnikovInfopulse closed 1 year ago
Hello @ogorodnikovInfopulse!
Thanks for your input.
I wonder if there is a real use case for setting LevelSwitch from the configuration. The point of LogLevelSwitch is to pass an object which you can control elsewhere in your program to dynamically adjust the level during runtime from there (see blog post from Nicholas Blumhardt https://nblumhardt.com/2014/10/dynamically-changing-the-serilog-level/).
If you would read LevelSwitch from a static configuration value you would not be able to change the level later at runtime. If you just need to restrict the log level using a fixed configuration value, you can use restrictedToMinimumLevel
.
Everything worked on v6.2.0.
I want to have a config file where I can change log level without restarting my app. Now I manually set the levelSwitch (tableOptions.LevelSwitch = levelSwitch
). I expect to not set manually tableOptions.LevelSwitch. I expect to read this setting directly from file. I guess if you add param levelSwitch to ctor internal MSSqlServerSinkOptions
- everything will work as I do manually.
I just realize that Serilog.Settings.Configuration indeed has some support for this.
I will take a look into this.
I could not reproduce your workaround with the config you posted. Could you please help us by supplying a full sample project which demonstrates your workaround?
csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.0-dev-02010" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="6.3.0" />
</ItemGroup>
<ItemGroup>
<None Update="serilog.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Prgram:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Serilog;
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("serilog.json", false, reloadOnChange: true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom
.Configuration(configuration)
.CreateLogger();
serilog.json:
{
"Serilog": {
"Using": [
"MsSinkSerilog",
"Serilog.Sinks.MSSqlServer"
],
"LevelSwitches": {
"$dbLogLvl": "Warning"
},
"WriteTo:SomeName": {
"Name": "MyConfiguredSink",
"Args": {
"levelSwitch": "$dbLogLvl", //Workaround to set levelSwitch
"tableOptions": {
"levelSwitch": "$dbLogLvl", //Should be like this starting from 6.2.0 but not working
"tableName": "TH_LOG",
"schemaName": "dbo",
"autoCreateSqlTable": false
}
}
}
}
}
MyConfiguredSink:
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Sinks.MSSqlServer;
using Serilog;
namespace MsSinkSerilog;
public static class SerilogConfig
{
public static LoggerConfiguration MyConfiguredSink(
this LoggerSinkConfiguration loggerSinkConfiguration
, LoggingLevelSwitch levelSwitch
, MSSqlServerSinkOptions tableOptions
//, IConfigurationSection columnOptionsSection
/*, ColumnOptions columnOptions*/
)
{
ArgumentNullException.ThrowIfNull(tableOptions);
var q = tableOptions.LevelSwitch;
if (q == null || q.MinimumLevel != levelSwitch.MinimumLevel)
{
throw new Exception("Need to fix it");
}
tableOptions.LevelSwitch = levelSwitch; //Temp fix. Serilog should fix setting tableOptions.LevelSwitch from config file
return
loggerSinkConfiguration
.MSSqlServer("", sinkOptions: tableOptions/*, columnOptionsSection: columnOptionsSection, columnOptions: columnOptions*/)
;
}
}
Sorry for the delay. I tried but could not get your approach to work.
Can you implement this and make a PR so we can further investigate?
Closing issue due to inactivity.
Bug Report / Support Request Template
Cannot set levelSwitch from config file
Write levelSwitch in MSSqlServerSinkOptions block in config file
.NET 6 OS: Windows
} }
public static LoggerConfiguration DbPerTenant( this LoggerSinkConfiguration loggerSinkConfiguration, MSSqlServerSinkOptions tableOptions, IConfigurationSection columnOptionsSection, /, ColumnOptions columnOptions/ LoggingLevelSwitch levelSwitch) { tableOptions.LevelSwitch = levelSwitch; //Temp fix return loggerSinkConfiguration .Map(TenantEnricher.TENANT_CONN_STR_PROPERTY_NAME, (connStr, wt) => wt.Async(a => a.MSSqlServer(connStr, sinkOptions: tableOptions, columnOptionsSection: columnOptionsSection/, columnOptions: columnOptions/)) ); }