1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Net . Http ;
4
+ using System . Runtime . Serialization ;
5
+ using System . Text ;
3
6
using System . Threading . Tasks ;
4
7
using ClientExample ;
5
8
using PostSharp . Aspects ;
6
9
using PostSharp . Patterns . Diagnostics ;
10
+ using PostSharp . Patterns . Formatters ;
7
11
using PostSharp . Serialization ;
8
12
using static PostSharp . Patterns . Diagnostics . SemanticMessageBuilder ;
9
13
10
- [ assembly: InstrumentOutgoingRequestsAspect ( AttributeTargetAssemblies = "System.Net.Http" ,
14
+ // The following attribute intercepts all calls to the specified methods of HttpClient.
15
+ [ assembly: InstrumentOutgoingRequestsAspect (
16
+ AttributeTargetAssemblies = "System.Net.Http" ,
11
17
AttributeTargetTypes = "System.Net.Http.HttpClient" ,
12
18
AttributeTargetMembers = "regex:(Get*|Delete|Post|Push|Patch)Async" ) ]
13
19
@@ -16,7 +22,7 @@ namespace ClientExample
16
22
[ PSerializable ]
17
23
internal class InstrumentOutgoingRequestsAspect : MethodInterceptionAspect
18
24
{
19
- private static readonly LogSource logger = LogSource . Get ( ) ;
25
+ private static readonly LogSource logSource = LogSource . Get ( ) ;
20
26
21
27
public override async Task OnInvokeAsync ( MethodInterceptionArgs args )
22
28
{
@@ -25,13 +31,66 @@ public override async Task OnInvokeAsync( MethodInterceptionArgs args )
25
31
26
32
var verb = Trim ( args . Method . Name , "Async" ) ;
27
33
28
- using ( var activity = logger . Default . OpenActivity ( Semantic ( verb , ( "Url" , args . Arguments [ 0 ] ) ) ) )
34
+ using ( var activity = logSource . Default . OpenActivity ( Semantic ( verb , ( "Url" , args . Arguments [ 0 ] ) ) ) )
29
35
{
30
36
try
31
37
{
38
+ // TODO: this implementation conflicts with System.Diagnostics.Activity.
32
39
40
+
41
+ // Remove headers.
33
42
http . DefaultRequestHeaders . Remove ( "Request-Id" ) ;
34
- http . DefaultRequestHeaders . Add ( "Request-Id" , activity . ContextId ) ;
43
+ http . DefaultRequestHeaders . Remove ( "Correlation-Context" ) ;
44
+
45
+
46
+ // Set Request-Id header.
47
+ http . DefaultRequestHeaders . Add ( "Request-Id" , activity . Context . SyntheticId ) ;
48
+
49
+
50
+ // Generate the Correlation-Context header.
51
+ UnsafeStringBuilder correlationContextBuilder = null ;
52
+ var propertyNames = new HashSet < string > ( ) ;
53
+ try
54
+ {
55
+ activity . Context . ForEachProperty ( ( LoggingProperty property , object value , ref object state ) =>
56
+ {
57
+ if ( property . IsBaggage )
58
+ {
59
+ if ( correlationContextBuilder == null )
60
+ {
61
+ propertyNames = new HashSet < string > ( ) ;
62
+ correlationContextBuilder = new UnsafeStringBuilder ( 1024 ) ;
63
+ }
64
+
65
+ if ( propertyNames . Add ( property . Name ) )
66
+ {
67
+
68
+ if ( correlationContextBuilder . Length > 0 )
69
+ {
70
+ correlationContextBuilder . Append ( ", " ) ;
71
+ }
72
+
73
+ correlationContextBuilder . Append ( property . Name ) ;
74
+ correlationContextBuilder . Append ( '=' ) ;
75
+
76
+ var formatter =
77
+ property . Formatter ?? LoggingServices . Formatters . Get ( value . GetType ( ) ) ;
78
+
79
+ formatter . Write ( correlationContextBuilder , value ) ;
80
+ }
81
+ }
82
+ } ) ;
83
+
84
+ if ( correlationContextBuilder != null )
85
+ {
86
+ http . DefaultRequestHeaders . Add ( "Correlation-Context" , correlationContextBuilder . ToString ( ) ) ;
87
+ }
88
+ }
89
+ finally
90
+ {
91
+ correlationContextBuilder ? . Dispose ( ) ;
92
+ }
93
+
35
94
36
95
var t = base . OnInvokeAsync ( args ) ;
37
96
@@ -78,16 +137,7 @@ public override async Task OnInvokeAsync( MethodInterceptionArgs args )
78
137
79
138
}
80
139
81
- private static string Trim ( string s , string suffix )
82
- {
83
- if ( s . EndsWith ( suffix ) )
84
- {
85
- return s . Substring ( 0 , s . Length - suffix . Length ) ;
86
- }
87
- else
88
- {
89
- return s ;
90
- }
91
- }
140
+ private static string Trim ( string s , string suffix )
141
+ => s . EndsWith ( suffix ) ? s . Substring ( 0 , s . Length - suffix . Length ) : s ;
92
142
}
93
143
}
0 commit comments