5
5
using PostSharp . Aspects ;
6
6
using PostSharp . Patterns . Diagnostics ;
7
7
using PostSharp . Serialization ;
8
- using Serilog . Context ;
8
+ using static PostSharp . Patterns . Diagnostics . SemanticMessageBuilder ;
9
9
10
10
[ assembly: InstrumentOutgoingRequestsAspect ( AttributeTargetAssemblies = "System.Net.Http" ,
11
11
AttributeTargetTypes = "System.Net.Http.HttpClient" ,
@@ -16,27 +16,24 @@ namespace ClientExample
16
16
[ PSerializable ]
17
17
internal class InstrumentOutgoingRequestsAspect : MethodInterceptionAspect
18
18
{
19
- private static readonly Logger logger = Logger . GetLogger ( LoggingRoles . Tracing , typeof ( HttpClient ) ) ;
20
-
21
-
19
+ private static readonly LogSource logger = LogSource . Get ( ) ;
22
20
23
21
public override async Task OnInvokeAsync ( MethodInterceptionArgs args )
24
22
{
25
- HttpClient http = ( HttpClient ) args . Instance ;
26
-
27
- string operationId = Guid . NewGuid ( ) . ToString ( ) ;
28
-
29
- http . DefaultRequestHeaders . Remove ( "Request-Id" ) ;
30
- http . DefaultRequestHeaders . Add ( "Request-Id" , operationId . ToString ( ) ) ;
23
+ var http = ( HttpClient ) args . Instance ;
31
24
32
- string verb = Trim ( args . Method . Name , "Async" ) ;
25
+
26
+ var verb = Trim ( args . Method . Name , "Async" ) ;
33
27
34
- using ( LogContext . PushProperty ( "OperationId" , operationId ) )
35
- using ( LogActivity activity = logger . OpenActivity ( "{Verb} {Url}" , verb , args . Arguments [ 0 ] ) )
28
+ using ( var activity = logger . Default . OpenActivity ( Semantic ( verb , ( "Url" , args . Arguments [ 0 ] ) ) ) )
36
29
{
37
30
try
38
31
{
39
- Task t = base . OnInvokeAsync ( args ) ;
32
+
33
+ http . DefaultRequestHeaders . Remove ( "Request-Id" ) ;
34
+ http . DefaultRequestHeaders . Add ( "Request-Id" , activity . ContextId ) ;
35
+
36
+ var t = base . OnInvokeAsync ( args ) ;
40
37
41
38
// We need to call Suspend/Resume because we're calling LogActivity from an aspect and
42
39
// aspects are not automatically enhanced.
@@ -55,15 +52,16 @@ public override async Task OnInvokeAsync( MethodInterceptionArgs args )
55
52
}
56
53
57
54
58
- HttpResponseMessage response = ( HttpResponseMessage ) args . ReturnValue ;
55
+ var response = ( HttpResponseMessage ) args . ReturnValue ;
59
56
57
+
60
58
if ( response . IsSuccessStatusCode )
61
59
{
62
- activity . SetSuccess ( "Success: { StatusCode} ", response . StatusCode ) ;
60
+ activity . SetOutcome ( LogLevel . Info , Semantic ( "Succeeded" , ( " StatusCode", response . StatusCode ) ) ) ;
63
61
}
64
62
else
65
63
{
66
- activity . SetFailure ( "Failure: { StatusCode} ", response . StatusCode ) ;
64
+ activity . SetOutcome ( LogLevel . Warning , Semantic ( "Failed" , ( " StatusCode", response . StatusCode ) ) ) ;
67
65
}
68
66
69
67
}
0 commit comments