@@ -18,6 +18,13 @@ public void SetIsSafeWhenNoContextIsActive()
18
18
dc . Set ( Some . String ( "name" ) , Some . Int32 ( ) ) ;
19
19
}
20
20
21
+ [ Fact ]
22
+ public void SetExceptionIsSafeWhenNoContextIsActive ( )
23
+ {
24
+ var dc = new DiagnosticContext ( Some . Logger ( ) ) ;
25
+ dc . SetException ( new Exception ( "test" ) ) ;
26
+ }
27
+
21
28
[ Fact ]
22
29
public async Task PropertiesAreCollectedInAnActiveContext ( )
23
30
{
@@ -39,6 +46,48 @@ public async Task PropertiesAreCollectedInAnActiveContext()
39
46
Assert . False ( collector . TryComplete ( out _ ) ) ;
40
47
}
41
48
49
+ [ Fact ]
50
+ public void ExceptionIsCollectedInAnActiveContext ( )
51
+ {
52
+ var dc = new DiagnosticContext ( Some . Logger ( ) ) ;
53
+ var collector = dc . BeginCollection ( ) ;
54
+
55
+ var setException = new Exception ( "before collect" ) ;
56
+ dc . SetException ( setException ) ;
57
+
58
+ Assert . True ( collector . TryComplete ( out _ , out var collectedException ) ) ;
59
+ Assert . Same ( setException , collectedException ) ;
60
+ }
61
+
62
+ [ Fact ]
63
+ public void ExceptionIsNotCollectedAfterTryComplete ( )
64
+ {
65
+ var dc = new DiagnosticContext ( Some . Logger ( ) ) ;
66
+ var collector = dc . BeginCollection ( ) ;
67
+ collector . TryComplete ( out _ , out _ ) ;
68
+ dc . SetException ( new Exception ( Some . String ( "after collect" ) ) ) ;
69
+
70
+ var tryComplete2 = collector . TryComplete ( out _ , out var collectedException2 ) ;
71
+
72
+ Assert . False ( tryComplete2 ) ;
73
+ Assert . Null ( collectedException2 ) ;
74
+ }
75
+
76
+ [ Fact ]
77
+ public void ExceptionIsNotCollectedAfterDispose ( )
78
+ {
79
+ var dc = new DiagnosticContext ( Some . Logger ( ) ) ;
80
+ var collector = dc . BeginCollection ( ) ;
81
+ collector . Dispose ( ) ;
82
+
83
+ dc . SetException ( new Exception ( "after dispose" ) ) ;
84
+
85
+ var tryComplete = collector . TryComplete ( out _ , out var collectedException ) ;
86
+
87
+ Assert . True ( tryComplete ) ;
88
+ Assert . Null ( collectedException ) ;
89
+ }
90
+
42
91
[ Fact ]
43
92
public void ExistingPropertiesCanBeUpdated ( )
44
93
{
@@ -53,5 +102,18 @@ public void ExistingPropertiesCanBeUpdated()
53
102
var scalar = Assert . IsType < ScalarValue > ( prop . Value ) ;
54
103
Assert . Equal ( 20 , scalar . Value ) ;
55
104
}
105
+
106
+ [ Fact ]
107
+ public void ExistingExceptionCanBeUpdated ( )
108
+ {
109
+ var dc = new DiagnosticContext ( Some . Logger ( ) ) ;
110
+ var collector = dc . BeginCollection ( ) ;
111
+
112
+ dc . SetException ( new Exception ( "ex1" ) ) ;
113
+ dc . SetException ( new Exception ( "ex2" ) ) ;
114
+
115
+ Assert . True ( collector . TryComplete ( out _ , out var collectedException ) ) ;
116
+ Assert . Equal ( "ex2" , collectedException . Message ) ;
117
+ }
56
118
}
57
119
}
0 commit comments