Skip to content

Commit 4002b96

Browse files
authored
Add stopwatch logging to request handler (#70)
1 parent 70be3b8 commit 4002b96

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<CredentialProviderVersion>0.1.12</CredentialProviderVersion>
4+
<CredentialProviderVersion>0.1.13</CredentialProviderVersion>
55
</PropertyGroup>
66
</Project>

CredentialProvider.Microsoft/RequestHandlers/RequestHandlerBase.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ protected RequestHandlerBase(ILogger logger)
4848
/// <inheritdoc cref="IRequestHandler.HandleResponseAsync"/>
4949
public async Task HandleResponseAsync(IConnection connection, Message message, IResponseHandler responseHandler, CancellationToken cancellationToken)
5050
{
51+
Stopwatch timer = new Stopwatch();
52+
timer.Start();
53+
5154
Connection = connection;
5255
CancellationToken = cancellationToken;
5356

5457
TRequest request = MessageUtilities.DeserializePayload<TRequest>(message);
5558

5659
try {
5760
TResponse response = null;
58-
Logger.Verbose(string.Format(Resources.HandlingRequest, message.Type, message.Method, message.Payload.ToString(Formatting.None)));
61+
Logger.Verbose(string.Format(Resources.HandlingRequest, message.Type, message.Method, timer.ElapsedMilliseconds, message.Payload.ToString(Formatting.None)));
5962
try {
6063
using (GetProgressReporter(connection, message, cancellationToken))
6164
{
@@ -64,21 +67,15 @@ public async Task HandleResponseAsync(IConnection connection, Message message, I
6467
}
6568
catch (Exception ex) when (cancellationToken.IsCancellationRequested)
6669
{
70+
// NuGet will handle canceling event but verbose logs in this case might be interesting.
6771
Logger.Verbose(string.Format(Resources.RequestHandlerCancelingExceptionMessage, ex.InnerException, ex.Message));
68-
Logger.Verbose(Resources.CancellationRequested);
69-
70-
// We have been canceled by NuGet. Send a cancellation response.
71-
var cancelMessage = MessageUtilities.Create(message.RequestId, MessageType.Cancel, message.Method);
72-
await connection.SendAsync(cancelMessage, CancellationToken.None);
73-
74-
// We must guarantee that exactly one terminating message is sent, so do not fall through to send
75-
// the normal response, but also do not rethrow.
7672
return;
7773
}
78-
79-
Logger.Verbose(string.Format(Resources.SendingResponse, message.Type, message.Method));
74+
Logger.Verbose(string.Format(Resources.SendingResponse, message.Type, message.Method, timer.ElapsedMilliseconds));
8075
// If we did not send a cancel message, we must submit the response even if cancellationToken is canceled.
8176
await responseHandler.SendResponseAsync(message, response, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);
77+
78+
Logger.Verbose(string.Format(Resources.TimeElapsedAfterSendingResponse, message.Type, message.Method, timer.ElapsedMilliseconds));
8279
}
8380
catch (Exception ex) when (LogExceptionAndReturnFalse(ex))
8481
{
@@ -100,6 +97,7 @@ bool LogExceptionAndReturnFalse(Exception ex)
10097
}
10198

10299
CancellationToken = CancellationToken.None;
100+
timer.Stop();
103101
}
104102

105103
public abstract Task<TResponse> HandleRequestAsync(TRequest request);

CredentialProvider.Microsoft/Resources.Designer.cs

Lines changed: 21 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CredentialProvider.Microsoft/Resources.resx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
<value>Handling auth request, Uri: {0}, IsRetry: {1}, IsNonInteractive: {2}, CanShowDialog: {3}</value>
204204
</data>
205205
<data name="HandlingRequest" xml:space="preserve">
206-
<value>Handling '{0}' '{1}' - {2}</value>
206+
<value>Handling '{0}' '{1}'. Time elapsed in ms: {2} - Payload: {3}</value>
207207
</data>
208208
<data name="InvalidatingCachedSessionToken" xml:space="preserve">
209209
<value>Invalidating SessionToken cache for {0}</value>
@@ -230,7 +230,7 @@
230230
<value>Running in stand-alone mode</value>
231231
</data>
232232
<data name="SendingResponse" xml:space="preserve">
233-
<value>Sending response: '{0}' '{1}'</value>
233+
<value>Sending response: '{0}' '{1}'. Time elapsed in ms: {2}</value>
234234
</data>
235235
<data name="SessionTokenCacheDisabled" xml:space="preserve">
236236
<value>SessionToken cache disabled by environment</value>
@@ -418,16 +418,19 @@ Device Flow Authentication Timeout
418418
<data name="CacheException" xml:space="preserve">
419419
<value>Exception happened when trying to get cached token. Invalidating cache. Message: {0}</value>
420420
</data>
421-
<data name="CancellationRequested" xml:space="preserve">
422-
<value>Cancellation requested. Sending cancellation response.</value>
423-
</data>
424421
<data name="BuildTaskNoMatchingPrefixes" xml:space="preserve">
425422
<value>none</value>
426423
</data>
427424
<data name="RequestHandlerCancelingExceptionMessage" xml:space="preserve">
428-
<value>Cancellation exception happened while handling request. Exception: {0}, Message: {1}</value>
425+
<value>Ignoring a cancellation exception during request handling. Exception: {0}, Message: {1}</value>
429426
</data>
430427
<data name="ShuttingDown" xml:space="preserve">
431428
<value>Credential provider shutting down.</value>
432429
</data>
430+
<data name="StartRequestHandlerStopwatch" xml:space="preserve">
431+
<value>Starting stopwatch for {1} {2}</value>
432+
</data>
433+
<data name="TimeElapsedAfterSendingResponse" xml:space="preserve">
434+
<value>Time elapsed in milliseconds after sending response '{0}' '{1}': {2}</value>
435+
</data>
433436
</root>

0 commit comments

Comments
 (0)