12

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

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
mberube.Net
  • 2,120
  • 2
  • 29
  • 39
  • If this is what you mean by expression template, there is an example [here](https://github.com/serilog/serilog-settings-configuration#nested-configuration-sections) – Crowcoder Mar 09 '21 at 16:08
  • No, this is a output template. I look for an expression template as described there : https://github.com/serilog/serilog-expressions. – mberube.Net Mar 09 '21 at 17:30
  • Got it. But I'm still not clear what you are asking since the link you posted has examples of expressions in the json config file. – Crowcoder Mar 09 '21 at 17:58
  • The examples are there for filters and other things but not as sink formatter – mberube.Net Mar 09 '21 at 18:34

1 Answers1

15

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.)

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • 1
    Has anyone submitted a feature request to support this? It would make sense since you can already use the `expression=` syntax with filters in `appsettings.json`. – Zach Blocker Aug 16 '21 at 22:41
  • 1
    @ZachBlocker none I'm aware of, but even an issue with a design proposal would be really welcome over at the _Serilog.Settings.Configuration_ repo – Nicholas Blumhardt Aug 16 '21 at 23:45
  • @ZachBlocker still no news? I am looking for the same feature, but still can't find anything – maradev21 Oct 05 '21 at 09:04
  • 2
    @maradev21 yes, there's been some progress - the syntax is shown in: https://github.com/serilog/serilog-settings-configuration/pull/281, but you'll need to install the `3.3.0-dev-00291` or later version of Serilog.Settings.Configuration from NuGet – Nicholas Blumhardt Oct 06 '21 at 02:11
  • 3
    How do you specify the theme? I've tried this but I can't get it to work `"theme": "Serilog.Templates.Themes.TemplateTheme::Code"` – Ryan Jan 07 '22 at 23:31
  • 1
    @Ryan it is now possible with Serilog.Settings.Configuration v3.4.0, assuming you add the assembly name (https://github.com/serilog/serilog-settings-configuration/issues/287) – dlrlc Sep 20 '22 at 20:47
  • Maybe I'm missing something, but this doesn't seem to work with conditional blocks. `{#if @l = 'Error'}...{#end}` always gets spit out with my output :/ – micka190 Jun 01 '23 at 14:56