问题
I know I can add enrichers this way:
{
"Serilog": {
"Using": [
"Serilog",
"Serilog.Enrichers.Environment",
"Serilog.Enrichers.Process",
...
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId"
]
...
Can I do the same for a property (ApplicationName
)?
回答1:
One way of doing this is with properties. But the configuration for enrichment properties has a caveat that it's a top level configuration and not an enrichment configuration:
{
"Serilog": {
"Using": [
"Serilog",
"Serilog.Enrichers.Environment",
"Serilog.Enrichers.Process",
...
],
"Properties": {
"ApplicationName": "my application"
},
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId"
]
...
This will cause the configuration reader to call something like this:
loggerConfiguration.Enrich.WithProperty("ApplicationName", "my application");
来源:https://stackoverflow.com/questions/50486481/is-there-a-way-to-enrich-the-log-with-a-property-from-configuration