serilog-settings-comparison icon indicating copy to clipboard operation
serilog-settings-comparison copied to clipboard

Provide example of Interface/Abstract class with values

Open Alxandr opened this issue 6 years ago • 4 comments

I'm trying to translate the following to config:

new JsonFormatter(renderMessage: true)

I came across this repo, and I imagine I can figure out how to do this, but it would be great if there was an example of this kind of value ready.

Alxandr avatar Mar 07 '19 14:03 Alxandr

Hi Aleksander ,

It is not supported right now in any of the Settings providers for Serilog right now, unfortunately (calling a constructor and passing parameters is not doable at the moment).

Some workarounds you can use :

  • store a static property that would contain that instance properly initialized , similar to https://github.com/tsimbalar/serilog-settings-comparison/blob/master/test/Serilog.Settings.Comparison.Tests/Support/Formatting/CustomFormatters.cs

https://github.com/tsimbalar/serilog-settings-comparison/blob/309f034fa448002d2e9e986a78ff51f6db59b9d1/test/Serilog.Settings.Comparison.Tests/Support/Formatting/CustomFormatters.cs#L1-L9

  • reference it through the "static" member access syntax as described here https://github.com/tsimbalar/serilog-settings-comparison/blob/master/docs/README.md#public-static-properties , this should look like this :

https://github.com/tsimbalar/serilog-settings-comparison/blob/309f034fa448002d2e9e986a78ff51f6db59b9d1/test/Serilog.Settings.Comparison.Tests/3-Advanced%20settings%20formats/332-ImplementationViaStaticProperty.config#L1-L8

or

https://github.com/tsimbalar/serilog-settings-comparison/blob/309f034fa448002d2e9e986a78ff51f6db59b9d1/test/Serilog.Settings.Comparison.Tests/3-Advanced%20settings%20formats/332-ImplementationViaStaticProperty.json#L1-L19

I hope this helps !

tsimbalar avatar Mar 07 '19 15:03 tsimbalar

Bummer. Thanks for this info though :)

Alxandr avatar Mar 08 '19 07:03 Alxandr

I don't know if that's a common enough scenario to ask for the creation of some static properties in JsonFormatter in the core Serilog repo : JsonFormatter.WithRenderedMessage or something like that

public static ITextFormatter WithRenderedMessage {get;} = new JsonFormatter(renderMessage: true);

which would allow to use it directly in the settings providers with something like :

 <?xml version="1.0" encoding="utf-8"?> 
 <configuration> 
   <appSettings>
     <add key="serilog:write-to:DummyWithFormatter.formatter" value="Serilog.Formatting.Json.JsonFormatter::WithRenderedMessage, Serilog" /> 
   </appSettings> 
</configuration> 

@nblumhardt : would that make sense, or would that just add noise in the core repo ?

tsimbalar avatar Mar 08 '19 10:03 tsimbalar

@tsimbalar Or provide a way to use constructors.

"formatter": {
  "@type": "Serilog.Formatting.Json.JsonFormatter",
  "renderMessage": true
}

This is basically the logic already used to allow doing DymmyWithFormatter.formatter already, just recursively.

Alxandr avatar Mar 08 '19 10:03 Alxandr