I am attempting to load my Serilog configuration from appsettings.json using the Serilog.Settings.Configuration
prerelease NuGet package. However when I load it using Configuration.GetSection, the value continues to come back as null.
Here is what I have in my Program.cs file:
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(
(hostingContext, loggingBuilder) =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog(
new LoggerConfiguration()
.ReadFrom.ConfigurationSection(hostingContext.Configuration.GetSection("Serilog"))
.CreateLogger()
);
}
);
Here is what I have in my appsettings.json file:
{
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Verbose",
"System": "Verbose",
"Microsoft.AspNetCore.Authentication": "Verbose"
}
},
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "*my_connectionString*",
"tableName": "ErrorLog"
}
}
]
}}
NuGet packages installed:
I configure it like that:
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration))
.Build();
by default Serilog
look for Serilog
section in configuration
I configured like this
public static void Main(string[] args)
{
var _config = new ConfigurationBuilder()
.AddJsonFile("loggerconfig.json")//mantained separate
config file.
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(_config)
.CreateLogger();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog()
.UseStartup<Startup>();
and this is the better way according to Serilog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With