I'm trying to determine if it's possible to configure the column options for serilog sink mssqlserver in the appsettings.json
file for an ASP.Net Core 2 project.
I create and configure the logger in the Program.cs
file.
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
//.Enrich.WithProperty("AppName", "One Badass App") // Adds property to XML structure in properties column
.ReadFrom.Configuration(Configuration)
.CreateLogger();
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
I can build the configuration file from the appsettings.json file, which contains a Serilog node with information for which connection string and table to use.
{
"AppSettings": {
"Application": {
"Name": "Payment Processing API",
"Version": "1.0"
}
},
"ConnectionStrings": {
"localPaymentProcessingDb": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true",
"tableName": "Logs"
}
}
]
}
There's an open issue on Github for this, but I haven't found any other information about it.
If column options can't be configured in the appsettings.json
, where and how should they be configured in an ASP.Net Core 2 project?
For accessing Serilog from appsettings in Code, you could bind Serilog node to Serilog class.
Here are the detail steps.
1. Create Serilog Configuration Class
Code:
Bind appsetting.json to class
Access Configuration