Skip to content
This repository was archived by the owner on Oct 17, 2018. It is now read-only.

Commit c48173c

Browse files
committed
Add event ids to all log calls
1 parent 0b808ca commit c48173c

18 files changed

+701
-251
lines changed

src/Microsoft.AspNet.DataProtection/AuthenticatedEncryption/CngCbcAuthenticatedEncryptionOptions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ private BCryptAlgorithmHandle GetHmacAlgorithmHandle(ILogger logger)
111111
throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(HashAlgorithm));
112112
}
113113

114-
if (logger.IsVerboseLevelEnabled())
115-
{
116-
logger.LogVerboseF($"Opening CNG algorithm '{HashAlgorithm}' from provider '{HashAlgorithmProvider}' with HMAC.");
117-
}
118-
114+
logger.OpeningCNGAlgorithmFromProviderWithHMAC(HashAlgorithm, HashAlgorithmProvider);
119115
BCryptAlgorithmHandle algorithmHandle = null;
120116

121117
// Special-case cached providers
@@ -152,10 +148,7 @@ private BCryptAlgorithmHandle GetSymmetricBlockCipherAlgorithmHandle(ILogger log
152148
throw Error.Common_PropertyMustBeNonNegative(nameof(EncryptionAlgorithmKeySize));
153149
}
154150

155-
if (logger.IsVerboseLevelEnabled())
156-
{
157-
logger.LogVerboseF($"Opening CNG algorithm '{EncryptionAlgorithm}' from provider '{EncryptionAlgorithmProvider}' with chaining mode CBC.");
158-
}
151+
logger.OpeningCNGAlgorithmFromProviderWithChainingModeCBC(EncryptionAlgorithm, EncryptionAlgorithmProvider);
159152

160153
BCryptAlgorithmHandle algorithmHandle = null;
161154

src/Microsoft.AspNet.DataProtection/AuthenticatedEncryption/CngGcmAuthenticatedEncryptionOptions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ private BCryptAlgorithmHandle GetSymmetricBlockCipherAlgorithmHandle(ILogger log
9292

9393
BCryptAlgorithmHandle algorithmHandle = null;
9494

95-
if (logger.IsVerboseLevelEnabled())
96-
{
97-
logger.LogVerboseF($"Opening CNG algorithm '{EncryptionAlgorithm}' from provider '{EncryptionAlgorithmProvider}' with chaining mode GCM.");
98-
}
99-
95+
logger.OpeningCNGAlgorithmFromProviderWithChainingModeGCM(EncryptionAlgorithm, EncryptionAlgorithmProvider);
10096
// Special-case cached providers
10197
if (EncryptionAlgorithmProvider == null)
10298
{

src/Microsoft.AspNet.DataProtection/AuthenticatedEncryption/ManagedAuthenticatedEncryptionOptions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ private Func<KeyedHashAlgorithm> GetKeyedHashAlgorithmFactory(ILogger logger)
8686
throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(ValidationAlgorithmType));
8787
}
8888

89-
if (logger.IsVerboseLevelEnabled())
90-
{
91-
logger.LogVerboseF($"Using managed keyed hash algorithm '{ValidationAlgorithmType.FullName}'.");
92-
}
93-
89+
logger.UsingManagedKeyedHashAlgorithm(ValidationAlgorithmType.FullName);
9490
if (ValidationAlgorithmType == typeof(HMACSHA256))
9591
{
9692
return () => new HMACSHA256();
@@ -118,10 +114,7 @@ private Func<SymmetricAlgorithm> GetSymmetricBlockCipherAlgorithmFactory(ILogger
118114
throw Error.Common_PropertyMustBeNonNegative(nameof(EncryptionAlgorithmKeySize));
119115
}
120116

121-
if (logger.IsVerboseLevelEnabled())
122-
{
123-
logger.LogVerboseF($"Using managed symmetric algorithm '{EncryptionAlgorithmType.FullName}'.");
124-
}
117+
logger.UsingManagedSymmetricAlgorithm(EncryptionAlgorithmType.FullName);
125118

126119
if (EncryptionAlgorithmType == typeof(Aes))
127120
{

src/Microsoft.AspNet.DataProtection/EphemeralDataProtectionProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ public EphemeralDataProtectionProvider(IServiceProvider services)
4848
}
4949

5050
var logger = services.GetLogger<EphemeralDataProtectionProvider>();
51-
if (logger.IsWarningLevelEnabled())
52-
{
53-
logger.LogWarning("Using ephemeral data protection provider. Payloads will be undecipherable upon application shutdown.");
54-
}
51+
logger.UsingEphemeralDataProtectionProvider();
5552

5653
_dataProtectionProvider = new KeyRingBasedDataProtectionProvider(keyringProvider, services);
5754
}

src/Microsoft.AspNet.DataProtection/KeyManagement/DefaultKeyResolver.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ private bool CanCreateAuthenticatedEncryptor(IKey key)
5454
}
5555
catch (Exception ex)
5656
{
57-
if (_logger.IsWarningLevelEnabled())
58-
{
59-
_logger.LogWarningF(ex, $"Key {key.KeyId:B} is ineligible to be the default key because its {nameof(IKey.CreateEncryptorInstance)} method failed.");
60-
}
57+
_logger.KeyIsIneligibleToBeTheDefaultKeyBecauseItsMethodFailed(key.KeyId, nameof(IKey.CreateEncryptorInstance), ex);
6158
return false;
6259
}
6360
}
@@ -72,18 +69,12 @@ private IKey FindDefaultKey(DateTimeOffset now, IEnumerable<IKey> allKeys, out I
7269

7370
if (preferredDefaultKey != null)
7471
{
75-
if (_logger.IsVerboseLevelEnabled())
76-
{
77-
_logger.LogVerboseF($"Considering key {preferredDefaultKey.KeyId:B} with expiration date {preferredDefaultKey.ExpirationDate:u} as default key.");
78-
}
72+
_logger.ConsideringKeyWithExpirationDateAsDefaultKey(preferredDefaultKey.KeyId, preferredDefaultKey.ExpirationDate);
7973

8074
// if the key has been revoked or is expired, it is no longer a candidate
8175
if (preferredDefaultKey.IsRevoked || preferredDefaultKey.IsExpired(now) || !CanCreateAuthenticatedEncryptor(preferredDefaultKey))
8276
{
83-
if (_logger.IsVerboseLevelEnabled())
84-
{
85-
_logger.LogVerboseF($"Key {preferredDefaultKey.KeyId:B} is no longer under consideration as default key because it is expired, revoked, or cannot be deciphered.");
86-
}
77+
_logger.KeyIsNoLongerUnderConsiderationAsDefault(preferredDefaultKey.KeyId);
8778
preferredDefaultKey = null;
8879
}
8980
}
@@ -104,9 +95,9 @@ private IKey FindDefaultKey(DateTimeOffset now, IEnumerable<IKey> allKeys, out I
10495
&& !key.IsExpired(now + _keyPropagationWindow)
10596
&& !key.IsRevoked);
10697

107-
if (callerShouldGenerateNewKey && _logger.IsVerboseLevelEnabled())
98+
if (callerShouldGenerateNewKey)
10899
{
109-
_logger.LogVerbose("Default key expiration imminent and repository contains no viable successor. Caller should generate a successor.");
100+
_logger.DefaultKeyExpirationImminentAndRepository();
110101
}
111102

112103
fallbackKey = null;
@@ -127,10 +118,7 @@ orderby key.CreationDate ascending
127118
where !key.IsRevoked && CanCreateAuthenticatedEncryptor(key)
128119
select key).FirstOrDefault();
129120

130-
if (_logger.IsVerboseLevelEnabled())
131-
{
132-
_logger.LogVerbose("Repository contains no viable default key. Caller should generate a key with immediate activation.");
133-
}
121+
_logger.RepositoryContainsNoViableDefaultKey();
134122

135123
callerShouldGenerateNewKey = true;
136124
return null;

src/Microsoft.AspNet.DataProtection/KeyManagement/KeyRingBasedDataProtector.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public byte[] Protect(byte[] plaintext)
111111

112112
if (_logger.IsDebugLevelEnabled())
113113
{
114-
_logger.LogDebugF($"Performing protect operation to key {defaultKeyId:B} with purposes {JoinPurposesForLog(Purposes)}.");
114+
_logger.PerformingProtectOperationToKeyWithPurposes(defaultKeyId, JoinPurposesForLog(Purposes));
115115
}
116116

117117
// We'll need to apply the default key id to the template if it hasn't already been applied.
@@ -236,7 +236,7 @@ private byte[] UnprotectCore(byte[] protectedData, bool allowOperationsOnRevoked
236236

237237
if (_logger.IsDebugLevelEnabled())
238238
{
239-
_logger.LogDebugF($"Performing unprotect operation to key {keyIdFromPayload:B} with purposes {JoinPurposesForLog(Purposes)}.");
239+
_logger.PerformingUnprotectOperationToKeyWithPurposes(keyIdFromPayload, JoinPurposesForLog(Purposes));
240240
}
241241

242242
// Find the correct encryptor in the keyring.
@@ -245,10 +245,7 @@ private byte[] UnprotectCore(byte[] protectedData, bool allowOperationsOnRevoked
245245
var requestedEncryptor = currentKeyRing.GetAuthenticatedEncryptorByKeyId(keyIdFromPayload, out keyWasRevoked);
246246
if (requestedEncryptor == null)
247247
{
248-
if (_logger.IsDebugLevelEnabled())
249-
{
250-
_logger.LogDebugF($"Key {keyIdFromPayload:B} was not found in the key ring. Unprotect operation cannot proceed.");
251-
}
248+
_logger.KeyWasNotFoundInTheKeyRingUnprotectOperationCannotProceed(keyIdFromPayload);
252249
throw Error.Common_KeyNotFound(keyIdFromPayload);
253250
}
254251

@@ -264,18 +261,12 @@ private byte[] UnprotectCore(byte[] protectedData, bool allowOperationsOnRevoked
264261
{
265262
if (allowOperationsOnRevokedKeys)
266263
{
267-
if (_logger.IsVerboseLevelEnabled())
268-
{
269-
_logger.LogVerboseF($"Key {keyIdFromPayload:B} was revoked. Caller requested unprotect operation proceed regardless.");
270-
}
264+
_logger.KeyWasRevokedCallerRequestedUnprotectOperationProceedRegardless(keyIdFromPayload);
271265
status = UnprotectStatus.DecryptionKeyWasRevoked;
272266
}
273267
else
274268
{
275-
if (_logger.IsVerboseLevelEnabled())
276-
{
277-
_logger.LogVerboseF($"Key {keyIdFromPayload:B} was revoked. Unprotect operation cannot proceed.");
278-
}
269+
_logger.KeyWasRevokedUnprotectOperationCannotProceed(keyIdFromPayload);
279270
throw Error.Common_KeyRevoked(keyIdFromPayload);
280271
}
281272
}

src/Microsoft.AspNet.DataProtection/KeyManagement/KeyRingProvider.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ private CacheableKeyRing CreateCacheableKeyRingCore(DateTimeOffset now, IKey key
4545
return CreateCacheableKeyRingCoreStep2(now, cacheExpirationToken, defaultKeyPolicy.DefaultKey, allKeys);
4646
}
4747

48-
if (_logger.IsVerboseLevelEnabled())
49-
{
50-
_logger.LogVerbose("Policy resolution states that a new key should be added to the key ring.");
51-
}
48+
_logger.PolicyResolutionStatesThatANewKeyShouldBeAddedToTheKeyRing();
5249

5350
// We shouldn't call CreateKey more than once, else we risk stack diving. This code path shouldn't
5451
// get hit unless there was an ineligible key with an activation date slightly later than the one we
@@ -69,18 +66,12 @@ private CacheableKeyRing CreateCacheableKeyRingCore(DateTimeOffset now, IKey key
6966
var keyToUse = defaultKeyPolicy.DefaultKey ?? defaultKeyPolicy.FallbackKey;
7067
if (keyToUse == null)
7168
{
72-
if (_logger.IsErrorLevelEnabled())
73-
{
74-
_logger.LogError("The key ring does not contain a valid default key, and the key manager is configured with auto-generation of keys disabled.");
75-
}
69+
_logger.KeyRingDoesNotContainValidDefaultKey();
7670
throw new InvalidOperationException(Resources.KeyRingProvider_NoDefaultKey_AutoGenerateDisabled);
7771
}
7872
else
7973
{
80-
if (_logger.IsWarningLevelEnabled())
81-
{
82-
_logger.LogWarningF($"Policy resolution states that a new key should be added to the key ring, but automatic generation of keys is disabled. Using fallback key {keyToUse.KeyId:B} with expiration {keyToUse.ExpirationDate:u} as default key.");
83-
}
74+
_logger.UsingFallbackKeyWithExpirationAsDefaultKey(keyToUse.KeyId, keyToUse.ExpirationDate);
8475
return CreateCacheableKeyRingCoreStep2(now, cacheExpirationToken, keyToUse, allKeys);
8576
}
8677
}
@@ -109,10 +100,7 @@ private CacheableKeyRing CreateCacheableKeyRingCoreStep2(DateTimeOffset now, Can
109100
// Invariant: our caller ensures that CreateEncryptorInstance succeeded at least once
110101
Debug.Assert(defaultKey.CreateEncryptorInstance() != null);
111102

112-
if (_logger.IsVerboseLevelEnabled())
113-
{
114-
_logger.LogVerboseF($"Using key {defaultKey.KeyId:B} as the default key.");
115-
}
103+
_logger.UsingKeyAsDefaultKey(defaultKey.KeyId);
116104

117105
DateTimeOffset nextAutoRefreshTime = now + GetRefreshPeriodWithJitter(_keyManagementOptions.KeyRingRefreshPeriod);
118106

@@ -165,9 +153,9 @@ internal IKeyRing GetCurrentKeyRingCore(DateTime utcNow)
165153
return existingCacheableKeyRing.KeyRing;
166154
}
167155

168-
if (existingCacheableKeyRing != null && _logger.IsVerboseLevelEnabled())
156+
if (existingCacheableKeyRing != null)
169157
{
170-
_logger.LogVerbose("Existing cached key ring is expired. Refreshing.");
158+
_logger.ExistingCachedKeyRingIsExpired();
171159
}
172160

173161
// It's up to us to refresh the cached keyring.
@@ -180,16 +168,13 @@ internal IKeyRing GetCurrentKeyRingCore(DateTime utcNow)
180168
}
181169
catch (Exception ex)
182170
{
183-
if (_logger.IsErrorLevelEnabled())
171+
if (existingCacheableKeyRing != null)
172+
{
173+
_logger.ErrorOccurredWhileRefreshingKeyRing(ex);
174+
}
175+
else
184176
{
185-
if (existingCacheableKeyRing != null)
186-
{
187-
_logger.LogError(ex, "An error occurred while refreshing the key ring. Will try again in 2 minutes.");
188-
}
189-
else
190-
{
191-
_logger.LogError(ex, "An error occurred while reading the key ring.");
192-
}
177+
_logger.ErrorOccurredWhileReadingKeyRing(ex);
193178
}
194179

195180
// Failures that occur while refreshing the keyring are most likely transient, perhaps due to a

0 commit comments

Comments
 (0)