Skip to content

Commit e6f3622

Browse files
authored
fixing log messages for L2 (#1070)
* fix log to log 0 for empty cache * add milliseconds instead of ticks * fix msal verbose log * use debug instead of trace
1 parent 9ae5f8a commit e6f3622

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/Microsoft.Identity.Web/TokenAcquisition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ private void Log(
759759
_logger.LogInformation(message);
760760
break;
761761
case Client.LogLevel.Verbose:
762-
_logger.LogInformation(message);
762+
_logger.LogDebug(message);
763763
break;
764764
default:
765765
break;

src/Microsoft.Identity.Web/TokenCacheProviders/Distributed/MsalDistributedTokenCacheAdapter.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey)
9090
{
9191
// check memory cache first
9292
byte[]? result = (byte[])_memoryCache.Get(cacheKey);
93-
_logger.LogDebug($"[MsIdWeb] MemoryCache: Read {cacheKey} cache size: {result?.Length}. ");
93+
_logger.LogDebug($"[MsIdWeb] MemoryCache: Read {cacheKey} cache size: {result?.Length ?? 0}. ");
9494

9595
if (result == null)
9696
{
@@ -103,7 +103,7 @@ protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey)
103103
cacheKey).ConfigureAwait(false);
104104
}).Measure().ConfigureAwait(false);
105105

106-
_logger.LogDebug($"[MsIdWeb] DistributedCache: Read time in Ticks: {measure.Ticks}");
106+
_logger.LogDebug($"[MsIdWeb] DistributedCache: Read time in MilliSeconds: {measure.MilliSeconds}. ");
107107

108108
// back propagate to memory cache
109109
if (result != null)
@@ -114,9 +114,9 @@ protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey)
114114
Size = result?.Length,
115115
};
116116

117-
_logger.LogDebug($"[MsIdWeb] Back propagate from Distributed to Memory, cache size: {result?.Length}");
117+
_logger.LogDebug($"[MsIdWeb] Back propagate from Distributed to Memory, cache size: {result?.Length ?? 0}. ");
118118
_memoryCache.Set(cacheKey, result, memoryCacheEntryOptions);
119-
_logger.LogDebug($"[MsIdWeb] MemoryCache: Count: {_memoryCache.Count}");
119+
_logger.LogDebug($"[MsIdWeb] MemoryCache: Count: {_memoryCache.Count}. ");
120120
}
121121
}
122122
else
@@ -148,10 +148,10 @@ protected override async Task WriteCacheBytesAsync(string cacheKey, byte[] bytes
148148

149149
// write in both
150150
_memoryCache.Set(cacheKey, bytes, memoryCacheEntryOptions);
151-
_logger.LogDebug($"[MsIdWeb] MemoryCache: Write cacheKey {cacheKey} cache size: {bytes?.Length}. ");
152-
_logger.LogDebug($"[MsIdWeb] MemoryCache: Count: {_memoryCache.Count}");
151+
_logger.LogDebug($"[MsIdWeb] MemoryCache: Write cacheKey {cacheKey} cache size: {bytes?.Length ?? 0}. ");
152+
_logger.LogDebug($"[MsIdWeb] MemoryCache: Count: {_memoryCache.Count}. ");
153153

154-
var measure = await L2OperationWithRetryOnFailureAsync(
154+
await L2OperationWithRetryOnFailureAsync(
155155
"Write",
156156
(cacheKey) => _distributedCache.SetAsync(cacheKey, bytes, _distributedCacheOptions),
157157
cacheKey).Measure().ConfigureAwait(false);
@@ -167,11 +167,11 @@ private async Task L2OperationWithRetryOnFailureAsync(
167167
try
168168
{
169169
var measure = await cacheOperation(cacheKey).Measure().ConfigureAwait(false);
170-
_logger.LogDebug($"[MsIdWeb] DistributedCache: {operation} cacheKey {cacheKey} cache size {bytes?.Length} InRetry? {inRetry} Time in Ticks: {measure.Ticks}. ");
170+
_logger.LogDebug($"[MsIdWeb] DistributedCache: {operation} cacheKey {cacheKey} cache size {bytes?.Length ?? 0} InRetry? {inRetry} Time in MilliSeconds: {measure.MilliSeconds}. ");
171171
}
172172
catch (Exception ex)
173173
{
174-
_logger.LogError($"[MsIdWeb] DistributedCache: Connection issue. InRetry? {inRetry} Error message: {ex.Message} ");
174+
_logger.LogError($"[MsIdWeb] DistributedCache: Connection issue. InRetry? {inRetry} Error message: {ex.Message}. ");
175175

176176
if (_distributedCacheOptions.OnL2CacheFailure != null && _distributedCacheOptions.OnL2CacheFailure(ex) && !inRetry)
177177
{
@@ -196,11 +196,11 @@ await L2OperationWithRetryOnFailureAsync(
196196
try
197197
{
198198
result = await cacheOperation(cacheKey).ConfigureAwait(false);
199-
_logger.LogDebug($"[MsIdWeb] DistributedCache: {operation} cacheKey {cacheKey} cache size {result?.Length} InRetry? {inRetry}. ");
199+
_logger.LogDebug($"[MsIdWeb] DistributedCache: {operation} cacheKey {cacheKey} cache size {result?.Length ?? 0} InRetry? {inRetry}. ");
200200
}
201201
catch (Exception ex)
202202
{
203-
_logger.LogError($"[MsIdWeb] DistributedCache: Connection issue. InRetry? {inRetry} Error message: {ex.Message} ");
203+
_logger.LogError($"[MsIdWeb] DistributedCache: Connection issue. InRetry? {inRetry} Error message: {ex.Message}. ");
204204

205205
if (_distributedCacheOptions.OnL2CacheFailure != null && _distributedCacheOptions.OnL2CacheFailure(ex) && !inRetry)
206206
{

src/Microsoft.Identity.Web/TokenCacheProviders/MeasureDurationResult.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Diagnostics;
5+
46
namespace Microsoft.Identity.Web.TokenCacheProviders
57
{
68
internal struct MeasureDurationResult
@@ -11,6 +13,14 @@ public MeasureDurationResult(long ticks)
1113
}
1214

1315
public long Ticks { get; }
16+
17+
public double MilliSeconds
18+
{
19+
get
20+
{
21+
return (double)Ticks / (double)Stopwatch.Frequency * 1000.0;
22+
}
23+
}
1424
}
1525

1626
internal struct MeasureDurationResult<TResult>

tests/Microsoft.Identity.Web.Test/UtilityTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public async Task Measurements_Tests()
1818
var epsilon = TimeSpan.FromMilliseconds(30).Ticks;
1919
var measure = await Task.Delay(taskDuration).Measure().ConfigureAwait(false);
2020
Assert.True(Math.Abs(measure.Ticks - taskDuration.Ticks) < epsilon, $"{(measure.Ticks - taskDuration.Ticks) / TimeSpan.TicksPerMillisecond}ms exceeds epsilon of 30ms");
21+
Assert.Equal(measure.Ticks * 1000.0, measure.MilliSeconds);
2122

2223
// measure task with a result
2324
var test = "test";

0 commit comments

Comments
 (0)