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

Custom format string not accepted by the message template parser #1353

Closed
EamonHetherton opened this issue Aug 3, 2019 · 2 comments · Fixed by #1366
Closed

Custom format string not accepted by the message template parser #1353

EamonHetherton opened this issue Aug 3, 2019 · 2 comments · Fixed by #1366

Comments

@EamonHetherton
Copy link

This might be a little esoteric but as I understand the message formatting is intended to follow standard .net formatting:

Message templates are a superset of .NET format strings. That means every valid .NET format string is also a valid message template.
blog

So in that spirit I'm raising an issue that serilog does not support using the ";" section separator for numeric formats.

likely related to #1323, #1325

I'm currently migrating some old code from Diagnostics.Trace to serilog and found a particular message was failing

The message logged a number of process metrics in a table-like fashion using alignment with the following format string:
"{0,-25} {1,10:#,##0} {2,10:#,##0} {3,10:+#,##0;-#,##0;0} {4,10:0.00}"

With serilog:
Log.Information("{0,-25} {1,10:#,##0} {2,10:#,##0} {3,10:+#,##0;-#,##0;0} {4,10:0.00}", "Process.HandleCount:", 1364, 1370, 6, 0.4398826979472);
produces

Process.HandleCount:            1364       1370 {3,10:+#,##0;-#,##0;0} 0.4398826979472

vs

Trace.WriteLine("{0,-25} {1,10:#,##0} {2,10:#,##0} {3,10:+#,##0;-#,##0;0} {4,10:0.00}", "Process.HandleCount:", 1364, 1370, 6, 0.4398826979472);
that produces the expected

Process.HandleCount:           1,364      1,370         +6       0.44

The main problem is the "{3,15:+#,##0;-#,##0;0}" part which is not identified as a format string but instead rendered literally.

Actually looking at that output now I realise that it has also ignored other parts of the numeric string formats.

Currently not sure whether this is core Serilog behaviour or the sink(s). I'm currently using the File sink.

@nblumhardt
Copy link
Member

Thanks for the heads-up, Eamon - sounds like a bug. Are you running 2.8.0? Cheers!

@EamonHetherton
Copy link
Author

Yeah, 2.8.0.

Quite the collection actually :)

<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0-dev-00847" />
<PackageReference Include="Serilog.Sinks.File.Header" Version="1.0.0-dev" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Trace" Version="2.1.0" />
<PackageReference Include="SerilogMetrics" Version="2.1.0" />
<PackageReference Include="SerilogTimings" Version="2.2.0" />

@nblumhardt nblumhardt changed the title custom Numeric format strings not honored Custom format string not accepted by the message template parser Oct 2, 2019
martinh2011 added a commit to martinh2011/serilog that referenced this issue Oct 4, 2019
fix serilog#1353 Custom format string not accepted by the message
template parser.

Custom numeric format strings may contain plus '+' signs.  That
character was not recognised by the parser.
martinh2011 added a commit to martinh2011/serilog that referenced this issue Oct 5, 2019
martinh2011 added a commit to martinh2011/serilog that referenced this issue Oct 5, 2019
fix serilog#1353 Custom format string not accepted by the message
template parser.

Custom numeric format strings may contain plus '+' signs.  That
character was not recognised by the parser.
Twinki14 pushed a commit to Twinki14/CitizenFX.Extensions.Client.Serilog that referenced this issue Dec 30, 2023
Twinki14 pushed a commit to Twinki14/CitizenFX.Extensions.Client.Serilog that referenced this issue Dec 30, 2023
fix serilog#1353 Custom format string not accepted by the message
template parser.

Custom numeric format strings may contain plus '+' signs.  That
character was not recognised by the parser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants