Does this issue relate to a new feature or an existing bug?
What version of Serilog is affected? Please list the related NuGet package.
package id="Serilog" version="2.5.0" targetFramework="net452"
What is the target framework and operating system? See target frameworks & net standard matrix.
Please describe the current behavior?
I have created an simple Enricher that I want to activate via the app.config too. When I call it via c# like this:
ILogger logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.Enrich.WithThreadId()
.Enrich.FromLogContext()
.Enrich.WithUtcTimestamp().CreateLogger();
it works like expected. But when I try the following:
<add key="serilog:enrich:with:UtcTimestamp" value="FMAsrsLib"/>
it does not work.
Please describe the expected behavior?
I would have expected that the "add key" line allows me to add the enricher by config. Have tried with other variations for the "value" with no success.
If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem
Enricher class:
namespace Serilog
{
public class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
logEvent.AddPropertyIfAbsent(pf.CreateProperty("UtcTimestamp", logEvent.Timestamp.UtcDateTime));
}
}
}
Configurator class:
namespace Serilog
{
public static class UtcTimestampConfigurationExtension
{
public static LoggerConfiguration WithUtcTimestamp(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<UtcTimestampEnricher>();
}
}
}
Originally the classes have been in a different namespace. Use of the Serilog namespace was a desparate try.
Finally after several edits it is readable.
Hi Lars,
your approach is correct and you are close enough, but you
serilog:enrich:with:UtcTimestamp instead of serilog:enrich:WithUtcTimestamp If your class UtcTimestampConfigurationExtension is in the assembly (or project) ThisIsMyProject.ClassLibrary then you will need something like:
<appSettings>
<add key="serilog:using:ThisIsMyProject.ClassLibrary" value="ThisIsMyProject.ClassLibrary" />
<add key="serilog:enrich:WithUtcTimestamp" value="" />
</appSettings>
I hope this helps !
You can have a look at https://github.com/tsimbalar/serilog-settings-comparison/blob/master/docs/README.md#method-discovery (it's a work in progress) that shows the different syntax for configuration
Thanks for the clear description. Works now as planed. If I had found/noticed the second link when googling I would have been able to solve it myself. The page makes it much clearer with the example.
The page makes it much clearer with the example.
This makes me very happy 馃榾
Most helpful comment
Hi Lars,
your approach is correct and you are close enough, but you
serilog:enrich:with:UtcTimestampinstead ofserilog:enrich:WithUtcTimestampIf your class
UtcTimestampConfigurationExtensionis in the assembly (or project)ThisIsMyProject.ClassLibrarythen you will need something like:I hope this helps !
You can have a look at https://github.com/tsimbalar/serilog-settings-comparison/blob/master/docs/README.md#method-discovery (it's a work in progress) that shows the different syntax for configuration