5
5
using System . Diagnostics ;
6
6
using System . Globalization ;
7
7
using System . IO ;
8
- using System . Linq ;
9
8
using Newtonsoft . Json ;
10
9
using Newtonsoft . Json . Linq ;
11
10
using Serilog . Events ;
12
- using Serilog . Parsing ;
13
11
using Xunit ;
14
12
// ReSharper disable AccessToDisposedClosure
15
13
// ReSharper disable ParameterOnlyUsedForPreconditionCheck.Local
@@ -18,7 +16,7 @@ namespace Serilog.Sinks.Seq.Tests;
18
16
19
17
public class SeqCompactJsonFormatterTests
20
18
{
21
- JObject AssertValidJson ( Action < ILogger > act , IFormatProvider ? formatProvider = null , Action < JObject > ? assert = null )
19
+ static JObject AssertValidJson ( Action < ILogger > act , IFormatProvider ? formatProvider = null , Action < JObject > ? assert = null )
22
20
{
23
21
var sw = new StringWriter ( ) ;
24
22
var logger = new LoggerConfiguration ( )
@@ -48,40 +46,21 @@ public void PropertiesFormatCorrectlyForTheFormatProvider(string cultureName)
48
46
{
49
47
var cultureInfo = new CultureInfo ( cultureName ) ;
50
48
51
- var number = Math . PI * 10000 ;
49
+ const double number = Math . PI * 10000 ;
52
50
var date = new DateTime ( 2024 , 7 , 19 , 10 , 00 , 59 ) ;
53
- var currency = 12345.67M ;
51
+ const decimal currency = 12345.67M ;
54
52
55
- // Culture-specific formatting differs by .NET version (and possibly OS).
56
- var messageBuilder = new StringWriter ( cultureInfo ) ;
57
- messageBuilder . Write ( "{0:n} {1} {2:C}" , number , date , currency ) ;
58
- var expectedMessage = messageBuilder . ToString ( ) ;
53
+ // Culture-specific formatting differs by .NET version platform.
54
+ var expectedNumber = number . ToString ( "n" , cultureInfo ) ;
55
+ var expectedCurrency = currency . ToString ( "C" , cultureInfo ) ;
59
56
60
57
AssertValidJson ( log => log . Information ( "{a:n} {b} {c:C}" , number , date , currency ) , cultureInfo , evt =>
61
58
{
62
- Assert . Equal ( expectedMessage , evt [ "@m" ] ! . Value < string > ( ) ) ;
63
- Assert . Null ( evt [ "@r" ] ) ;
59
+ Assert . Equal ( expectedNumber , evt [ "@r" ] ! [ 0 ] ! . Value < string > ( ) ) ;
60
+ Assert . Equal ( expectedCurrency , evt [ "@r" ] ! [ 1 ] ! . Value < string > ( ) ) ;
64
61
} ) ;
65
62
}
66
-
67
- [ Fact ]
68
- public void MessageNotRenderedForDefaultFormatProvider ( )
69
- {
70
- AssertValidJson ( log => log . Information ( "{a}" , 1.234 ) , null , evt =>
71
- {
72
- Assert . Null ( evt [ "@m" ] ) ;
73
- } ) ;
74
- }
75
-
76
- [ Fact ]
77
- public void MessageNotRenderedForExplicitInvariantCulture ( )
78
- {
79
- AssertValidJson ( log => log . Information ( "{a}" , 1.234 ) , CultureInfo . InvariantCulture , evt =>
80
- {
81
- Assert . Null ( evt [ "@m" ] ) ;
82
- } ) ;
83
- }
84
-
63
+
85
64
[ Fact ]
86
65
public void AnEmptyEventIsValidJson ( )
87
66
{
@@ -150,9 +129,7 @@ public void TraceAndSpanIdsGenerateValidJson()
150
129
{
151
130
var traceId = ActivityTraceId . CreateRandom ( ) ;
152
131
var spanId = ActivitySpanId . CreateRandom ( ) ;
153
- var evt = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null ,
154
- new MessageTemplate ( Enumerable . Empty < MessageTemplateToken > ( ) ) , Enumerable . Empty < LogEventProperty > ( ) ,
155
- traceId , spanId ) ;
132
+ var evt = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , MessageTemplate . Empty , [ ] , traceId , spanId ) ;
156
133
var json = AssertValidJson ( log => log . Write ( evt ) ) ;
157
134
Assert . Equal ( traceId . ToHexString ( ) , json [ "@tr" ] ) ;
158
135
Assert . Equal ( spanId . ToHexString ( ) , json [ "@sp" ] ) ;
0 commit comments