55using Microsoft . Rest ;
66using Polly ;
77using System ;
8+ using System . Diagnostics ;
89using System . Linq ;
910using System . Net ;
1011using System . Net . Http ;
12+ using System . Text ;
1113using System . Threading ;
1214using System . Threading . Tasks ;
1315
@@ -16,11 +18,11 @@ namespace LetsEncrypt.Azure.Core
1618 public static class ArmHelper
1719 {
1820 public static async Task < WebSiteManagementClient > GetWebSiteManagementClient ( IAzureWebAppEnvironment model )
19- {
21+ {
2022 AuthenticationResult token = await GetToken ( model ) ;
2123 var creds = new TokenCredentials ( token . AccessToken ) ;
2224
23- var websiteClient = new WebSiteManagementClient ( model . ManagementEndpoint , creds ) ;
25+ var websiteClient = new WebSiteManagementClient ( model . ManagementEndpoint , creds , new TraceLoggingHandler ( ) ) ;
2426 websiteClient . SubscriptionId = model . SubscriptionId . ToString ( ) ;
2527 return websiteClient ;
2628 }
@@ -44,7 +46,7 @@ private static async Task<AuthenticationResult> GetToken(IAzureEnvironment model
4446 }
4547
4648 public static async Task < HttpClient > GetHttpClient ( IAzureWebAppEnvironment model )
47- {
49+ {
4850 AuthenticationResult token = await GetToken ( model ) ;
4951
5052 var client = HttpClientFactory . Create ( new HttpClientHandler ( ) , new TimeoutHandler ( ) ) ;
@@ -60,7 +62,7 @@ public static Polly.Retry.RetryPolicy<HttpResponseMessage> ExponentialBackoff(in
6062 . HandleResult < HttpResponseMessage > ( ( resp ) =>
6163 {
6264 return IsTransient ( resp . StatusCode ) ;
63-
65+
6466 } )
6567 . WaitAndRetryAsync ( retryCount , retryAttempt =>
6668 TimeSpan . FromSeconds ( Math . Pow ( firstBackOffDelay , retryAttempt ) )
@@ -103,4 +105,60 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
103105 }
104106 }
105107 }
108+
109+ public abstract class MessageHandler : DelegatingHandler
110+ {
111+ protected override async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
112+ {
113+ var corrId = string . Format ( "{0}{1}" , DateTime . Now . Ticks , Thread . CurrentThread . ManagedThreadId ) ;
114+ var requestInfo = string . Format ( "{0} {1}, headers {2}" , request . Method , request . RequestUri , string . Join ( "," , request . Headers
115+ . Where ( s => ! string . Equals ( s . Key , "Authorization" , StringComparison . InvariantCultureIgnoreCase ) )
116+ . Select ( s => $ "{ s . Key } = { string . Join ( "|" , s . Value ) } ")
117+ ) ) ;
118+
119+ byte [ ] requestMessage = null ;
120+ if ( request . Content != null )
121+ {
122+ requestMessage = await request . Content . ReadAsByteArrayAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
123+ }
124+
125+ LogIncommingMessage ( corrId , requestInfo , requestMessage ) ;
126+
127+ var response = await base . SendAsync ( request , cancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
128+
129+ byte [ ] responseMessage = null ;
130+ if ( response . Content != null )
131+ {
132+ responseMessage = await response . Content . ReadAsByteArrayAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
133+ }
134+
135+ LogOutgoingMessage ( corrId , requestInfo , responseMessage ) ;
136+
137+ return response ;
138+ }
139+
140+
141+ protected abstract void LogIncommingMessage ( string correlationId , string requestInfo , byte [ ] message ) ;
142+ protected abstract void LogOutgoingMessage ( string correlationId , string requestInfo , byte [ ] message ) ;
143+ }
144+
145+
146+
147+ public class TraceLoggingHandler : MessageHandler
148+ {
149+
150+ public TraceLoggingHandler ( )
151+ {
152+ }
153+ protected override void LogIncommingMessage ( string correlationId , string requestInfo , byte [ ] message )
154+ {
155+ Trace . TraceInformation ( string . Format ( "{0} - Request: {1}\r \n {2}" , correlationId , requestInfo , message != null ? Encoding . UTF8 . GetString ( message ) : String . Empty ) ) ;
156+ }
157+
158+
159+ protected override void LogOutgoingMessage ( string correlationId , string requestInfo , byte [ ] message )
160+ {
161+ Trace . TraceInformation ( string . Format ( "{0} - Response: {1}\r \n {2}" , correlationId , requestInfo , message != null ? Encoding . UTF8 . GetString ( message ) : String . Empty ) ) ;
162+ }
163+ }
106164}
0 commit comments