1010using log4net . Core ;
1111using Xunit ;
1212
13- namespace Elastic . CommonSchema . Log4net . Tests
14- {
15- [ CollectionDefinition ( "EcsProperties" , DisableParallelization = true ) ]
16- public class EcsFieldsAsPropertiesTests : LogTestsBase
17- {
18- private const string FixedTraceId = "my-trace-id" ;
19-
20- [ Fact ]
21- public void EcsFieldInLogProperties ( )
22- {
23- void LogAndAssert ( ILog log , Func < List < string > > getLogEvents )
24- {
25- log . Info ( "DummyText" ) ;
26-
27- var logEvents = getLogEvents ( ) ;
28- logEvents . Should ( ) . HaveCount ( 1 ) ;
29-
30- var ( _, info ) = ToEcsEvents ( logEvents ) . First ( ) ;
31- info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
32- }
33-
34- // Testing these in one unit test because ThreadContext overwrites LogicalThreadContext which overwrites GlobalContext.
35- // Testing these in isolation introduces race issues when we remove the property
36-
37- LogicalThreadContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
38- try
39- {
40- TestLogger ( LogAndAssert ) ;
41- }
42- finally
43- {
44- LogicalThreadContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
45- }
46-
47- ThreadContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
48- try
49- {
50- TestLogger ( LogAndAssert ) ;
51- }
52- finally
53- {
54- ThreadContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
55- }
56- TestLogger ( ( log , getLogEvents ) =>
57- {
58- var loggingEvent = new LoggingEvent ( GetType ( ) , log . Logger . Repository , log . Logger . Name , Level . Info , "DummyText" , null ) ;
59- loggingEvent . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
60- log . Logger . Log ( loggingEvent ) ;
61-
62- var logEvents = getLogEvents ( ) ;
63- logEvents . Should ( ) . HaveCount ( 1 ) ;
64-
65- var ( _, info ) = ToEcsEvents ( logEvents ) . First ( ) ;
66- info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
67- } ) ;
68-
69- GlobalContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
70- try
71- {
72- TestLogger ( LogAndAssert ) ;
73- }
74- finally
75- {
76- GlobalContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
77- }
78-
79- }
13+ namespace Elastic . CommonSchema . Log4net . Tests ;
8014
15+ [ CollectionDefinition ( "EcsProperties" , DisableParallelization = true ) ]
16+ public class EcsFieldsAsPropertiesTests : LogTestsBase
17+ {
18+ private const string FixedTraceId = "my-trace-id" ;
8119
82- [ Fact ]
83- public void EcsFieldsInThreadContextStack ( ) => TestLogger ( ( log , getLogEvents ) =>
20+ [ Fact ]
21+ public void EcsFieldInLogProperties ( )
22+ {
23+ void LogAndAssert ( ILog log , Func < List < string > > getLogEvents )
8424 {
85- using var _ = ThreadContext . Stacks [ LogTemplateProperties . TraceId ] . Push ( FixedTraceId ) ;
86-
8725 log . Info ( "DummyText" ) ;
8826
8927 var logEvents = getLogEvents ( ) ;
9028 logEvents . Should ( ) . HaveCount ( 1 ) ;
9129
9230 var ( _, info ) = ToEcsEvents ( logEvents ) . First ( ) ;
9331 info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
94- } ) ;
32+ }
33+
34+ // Testing these in one unit test because ThreadContext overwrites LogicalThreadContext which overwrites GlobalContext.
35+ // Testing these in isolation introduces race issues when we remove the property
9536
96- [ Fact ]
97- public void EcsFieldsInLogicalTheadContextStack ( ) => TestLogger ( ( log , getLogEvents ) =>
37+ LogicalThreadContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
38+ try
39+ {
40+ TestLogger ( LogAndAssert ) ;
41+ }
42+ finally
9843 {
99- using var _ = LogicalThreadContext . Stacks [ LogTemplateProperties . TraceId ] . Push ( FixedTraceId ) ;
44+ LogicalThreadContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
45+ }
10046
101- log . Info ( "DummyText" ) ;
47+ ThreadContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
48+ try
49+ {
50+ TestLogger ( LogAndAssert ) ;
51+ }
52+ finally
53+ {
54+ ThreadContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
55+ }
56+ TestLogger ( ( log , getLogEvents ) =>
57+ {
58+ var loggingEvent = new LoggingEvent ( GetType ( ) , log . Logger . Repository , log . Logger . Name , Level . Info , "DummyText" , null ) ;
59+ loggingEvent . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
60+ log . Logger . Log ( loggingEvent ) ;
10261
10362 var logEvents = getLogEvents ( ) ;
10463 logEvents . Should ( ) . HaveCount ( 1 ) ;
@@ -107,5 +66,43 @@ public void EcsFieldsInLogicalTheadContextStack() => TestLogger((log, getLogEven
10766 info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
10867 } ) ;
10968
69+ GlobalContext . Properties [ LogTemplateProperties . TraceId ] = FixedTraceId ;
70+ try
71+ {
72+ TestLogger ( LogAndAssert ) ;
73+ }
74+ finally
75+ {
76+ GlobalContext . Properties . Remove ( LogTemplateProperties . TraceId ) ;
77+ }
78+
11079 }
80+
81+ [ Fact ]
82+ public void EcsFieldsInThreadContextStack ( ) => TestLogger ( ( log , getLogEvents ) =>
83+ {
84+ using var _ = ThreadContext . Stacks [ LogTemplateProperties . TraceId ] . Push ( FixedTraceId ) ;
85+
86+ log . Info ( "DummyText" ) ;
87+
88+ var logEvents = getLogEvents ( ) ;
89+ logEvents . Should ( ) . HaveCount ( 1 ) ;
90+
91+ var ( _, info ) = ToEcsEvents ( logEvents ) . First ( ) ;
92+ info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
93+ } ) ;
94+
95+ [ Fact ]
96+ public void EcsFieldsInLogicalTheadContextStack ( ) => TestLogger ( ( log , getLogEvents ) =>
97+ {
98+ using var _ = LogicalThreadContext . Stacks [ LogTemplateProperties . TraceId ] . Push ( FixedTraceId ) ;
99+
100+ log . Info ( "DummyText" ) ;
101+
102+ var logEvents = getLogEvents ( ) ;
103+ logEvents . Should ( ) . HaveCount ( 1 ) ;
104+
105+ var ( _, info ) = ToEcsEvents ( logEvents ) . First ( ) ;
106+ info . TraceId . Should ( ) . Be ( FixedTraceId ) ;
107+ } ) ;
111108}
0 commit comments