Serilog: Unable to register an Enricher in app.config

Created on 18 Jan 2018  路  3Comments  路  Source: serilog/serilog

Does this issue relate to a new feature or an existing bug?

  • [X] Bug or error in my implementation understanding
  • [ ] New Feature

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.

  • [ ] netCore 2.0
  • [ ] netCore 1.0
  • [ ] 4.7
  • [ ] 4.6.x
  • [X] 4.5.x

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.

Most helpful comment

Hi Lars,

your approach is correct and you are close enough, but you

  • wrote serilog:enrich:with:UtcTimestamp instead of serilog:enrich:WithUtcTimestamp
  • are missing a key in appSettings that tells Serilog to look for your extension methods in the proper assembly (as explained here : https://github.com/serilog/serilog/wiki/AppSettings#using-sink-extensions-from-additional-assemblies ).

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

All 3 comments

Hi Lars,

your approach is correct and you are close enough, but you

  • wrote serilog:enrich:with:UtcTimestamp instead of serilog:enrich:WithUtcTimestamp
  • are missing a key in appSettings that tells Serilog to look for your extension methods in the proper assembly (as explained here : https://github.com/serilog/serilog/wiki/AppSettings#using-sink-extensions-from-additional-assemblies ).

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 馃榾

Was this page helpful?
0 / 5 - 0 ratings