Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept an IFormatProvider #150

Open
taspeotis opened this issue May 14, 2021 · 3 comments
Open

Accept an IFormatProvider #150

taspeotis opened this issue May 14, 2021 · 3 comments

Comments

@taspeotis
Copy link

taspeotis commented May 14, 2021

I'd like to render a currency value in my log output. For the sake of discussion:

decimal currentBalance = 12345.67M;

Log.Logger.Information("Your balance is {CurrentBalance:C}", currentBalance);

As best I can tell Seq uses the invariant culture which renders currencies like ¤12,345.67 (or (¤12,345.67) for negative). I sympathise with the choice of invariant culture as a good default but its currency format is not very native to me.

Serilog's documentation suggests the onus is on sinks to support format providers, and I can't see any way to give Seq a CustomDecimalFormatter or similar.

For now I am using {CurrentBalance:$#,0.00} as a workaround, I'd prefer to use C and get a en-AU/en-US style currency.

If I've overlooked how to specify a format provider for this sink please let me know, otherwise I submit "Specify Format Provider" as a feature request for your consideration.

@nblumhardt
Copy link
Member

Thanks for the suggestion, Todd - makes perfect sense 👍

There are a few moving parts to this; Seq uses Serilog.Formatting.Compact.CompactJsonFormatter to format its payloads:

https://github.com/serilog/serilog-sinks-seq/blob/3152e1d59fbbbc07ea97f3f3cba3e48914659a8b/src/Serilog.Sinks.Seq/Sinks/Seq/SeqPayloadFormatter.cs#L40

CompactJsonFormatter renders any message template tokens that have formats associated with them:

https://github.com/serilog/serilog-formatting-compact/blob/dev/src/Serilog.Formatting.Compact/Formatting/Compact/CompactJsonFormatter.cs#L84

But unfortunately, it doesn't supply the third IFormatProvider parameter to PropertyToken.Render().

Right now the quickest option will probably be for us to take over the formatting responsibility here in the Seq sink, and accept an IFormatProvider through WriteTo.Seq(), but I suspect longer-term we'll need to consider some changes in Serilog.Formatting.Compact to accommodate this.

@nblumhardt nblumhardt changed the title Specify Format Provider Accept an IFormatProvider Jan 13, 2022
@nblumhardt
Copy link
Member

7.x and later support this.

@nblumhardt
Copy link
Member

Sorry I mistook this for ITextFormatter, which is now supported; I'm not sure if it's enough to apply the custom format provider (but may be, I'll look into it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants