Serilog - appsettings.json

Here is a sample appSettings.json with configurations in place for Serilog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"NotesDatabaseSettings": {
"ConnectionString": "Server=.; Database=Notes; integrated security=true;Trust Server Certificate=true;",
"IsSSL": true
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"NoteConnectionString": "Server=.; Database=Notes; integrated security=true;Trust Server Certificate=true;"
},
"Serilog": {
// This is used to initialize Serilog logger.
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore.Hosting.Diagnostics": "Error",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
// this enables logging to a file
{
"Name": "File",
"Args": {
"path": "e:\\t\\NoteLog.txt",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
// note: WriteTo is an array. you can send output to multiple destinations
],
"Enrich": [
//
// this enriches the log with additional information
//
"WithMachineName",
"WithProcessId",
"WithThreadId"
]
}
}

You can see a number of different sections

  • NotesDatabaseSettings - ignore.
  • Logging - this is standard stuff. It exists in all of my asp.net core configuration files.
  • AllowedHosts - Ignore
  • ConnectionStrings - Ignore - I have some code cleanup - to consolidate this block and NotesDatabaseSttings. But that is not serilog related.
  • Serilog - this is code I added to hookup serilog.

The serilog block has a number of different sections

  • MinimumLevel - is where you set the logging level. Development might be set to Information, production might be set to Error. I think this configures serilog to output to the console.
  • WriteTo - This is used to configure Sync’s - places to send out logging data.. The first is “File” that is, the File sink. That is to configure logs into text files. I’m sending the logs to e:\t\NoteLog.txt. RollingInterval setups a daily file naming. And the formatter sends the output into a json format. Notice we wrap the file specification in brackets. This allows you to configure multiple logs.
  • Enrich instructs serilog to add a MachineName, ProcessId, and ThreadId to the logging.