'How to use Expression Template with appSettings in Serilog

I've a file sink configured in the appsettings.json of my application.
It works perfectly but now I want to add an expression template to format the output in my file.
As I can see, there's no way to set expression template using configuration files.
If this is not possible, is there a way to use inline configuration for my file sink but to keep the file path into the configuration file ?

Thanks



Solution 1:[1]

As of version 3.3.0 of Serilog.Settings.Configuration, this is now possible:

{
  "Name": "Console",
  "Args": {
    "formatter": {
      "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
      "template": "[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}"
    }
  }
}

For earlier versions there's no direct support, but if you put the template into a static property somewhere:

public static class Formatters
{
    public static ITextFormatter Output { get; } = new ExpressionTemplate(...);
}

Then you can pass that value through JSON configuration by naming the static property:

{
    "Name": "Console",
    "Args": { "formatter"" "YourApp.Formatters::Output, YourApp" }
}

(Check the arguments accepted by the sink to see what the name of the formatter argument is - but it should be formatter as above in most cases.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hakan Fıstık