Remove use of null-conditional operator for logger (#6579)

This commit is contained in:
Kahbazi 2019-06-18 22:48:43 +04:30 committed by Chris Ross
parent 9077b38805
commit bff78bdd11
17 changed files with 40 additions and 38 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption
BCryptAlgorithmHandle algorithmHandle = null; BCryptAlgorithmHandle algorithmHandle = null;
_logger?.OpeningCNGAlgorithmFromProviderWithChainingModeGCM(configuration.EncryptionAlgorithm, configuration.EncryptionAlgorithmProvider); _logger.OpeningCNGAlgorithmFromProviderWithChainingModeGCM(configuration.EncryptionAlgorithm, configuration.EncryptionAlgorithmProvider);
// Special-case cached providers // Special-case cached providers
if (configuration.EncryptionAlgorithmProvider == null) if (configuration.EncryptionAlgorithmProvider == null)
{ {

View File

@ -4,6 +4,7 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace System namespace System
{ {
@ -37,7 +38,7 @@ namespace System
{ {
// Compiler won't allow us to use static types as the type parameter // Compiler won't allow us to use static types as the type parameter
// for the call to CreateLogger<T>, so we'll duplicate its logic here. // for the call to CreateLogger<T>, so we'll duplicate its logic here.
return services?.GetService<ILoggerFactory>()?.CreateLogger(type.FullName); return services?.GetService<ILoggerFactory>()?.CreateLogger(type.FullName) ?? NullLogger.Instance;
} }
} }
} }

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
{ {
// It's OK for us to log the error, as we control the exception, and it doesn't contain // It's OK for us to log the error, as we control the exception, and it doesn't contain
// sensitive information. // sensitive information.
_logger?.ExceptionOccurredTryingToDecryptElement(ex); _logger.ExceptionOccurredTryingToDecryptElement(ex);
throw; throw;
} }
} }

View File

@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
throw new ArgumentNullException(nameof(encryptedElement)); throw new ArgumentNullException(nameof(encryptedElement));
} }
_logger?.DecryptingSecretElementUsingWindowsDPAPI(); _logger.DecryptingSecretElementUsingWindowsDPAPI();
try try
{ {
@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
{ {
// It's OK for us to log the error, as we control the exception, and it doesn't contain // It's OK for us to log the error, as we control the exception, and it doesn't contain
// sensitive information. // sensitive information.
_logger?.ExceptionOccurredTryingToDecryptElement(ex); _logger.ExceptionOccurredTryingToDecryptElement(ex);
throw; throw;
} }
} }

View File

@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
throw new ArgumentNullException(nameof(plaintextElement)); throw new ArgumentNullException(nameof(plaintextElement));
} }
_logger?.EncryptingUsingNullEncryptor(); _logger.EncryptingUsingNullEncryptor();
// <unencryptedKey> // <unencryptedKey>
// <!-- This key is not encrypted. --> // <!-- This key is not encrypted. -->

View File

@ -21,6 +21,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.StackTrace.Sources; using Microsoft.Extensions.StackTrace.Sources;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -42,7 +43,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
private IServiceProvider _applicationServices; private IServiceProvider _applicationServices;
private ExceptionDispatchInfo _applicationServicesException; private ExceptionDispatchInfo _applicationServicesException;
private ILogger _logger; private ILogger _logger = NullLogger.Instance;
private bool _stopped; private bool _stopped;
private bool _startedServer; private bool _startedServer;
@ -317,7 +318,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
} }
_stopped = true; _stopped = true;
_logger?.Shutdown(); _logger.Shutdown();
var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token; var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token;
if (!cancellationToken.CanBeCanceled) if (!cancellationToken.CanBeCanceled)
@ -364,7 +365,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger?.ServerShutdownException(ex); _logger.ServerShutdownException(ex);
} }
} }

View File

@ -16,9 +16,7 @@
<Reference Include="System.ServiceProcess.ServiceController" /> <Reference Include="System.ServiceProcess.ServiceController" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="WebHostService.cs"> <Compile Update="WebHostService.cs" />
<SubType>Component</SubType>
</Compile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
if (!initMatchRes.Success) if (!initMatchRes.Success)
{ {
context.Logger?.ModRewriteNotMatchedRule(); context.Logger.ModRewriteNotMatchedRule();
return; return;
} }
@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
var condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchRes.BackReferences); var condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchRes.BackReferences);
if (!condResult.Success) if (!condResult.Success)
{ {
context.Logger?.ModRewriteNotMatchedRule(); context.Logger.ModRewriteNotMatchedRule();
return; return;
} }
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
// At this point, we know our rule passed, first apply pre conditions, // At this point, we know our rule passed, first apply pre conditions,
// which can modify things like the cookie or env, and then apply the action // which can modify things like the cookie or env, and then apply the action
context.Logger?.ModRewriteMatchedRule(); context.Logger.ModRewriteMatchedRule();
foreach (var action in Actions) foreach (var action in Actions)
{ {

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
if (!initMatchResults.Success) if (!initMatchResults.Success)
{ {
context.Logger?.UrlRewriteNotMatchedRule(Name); context.Logger.UrlRewriteNotMatchedRule(Name);
return; return;
} }
@ -62,14 +62,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchResults.BackReferences); condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchResults.BackReferences);
if (!condResult.Success) if (!condResult.Success)
{ {
context.Logger?.UrlRewriteNotMatchedRule(Name); context.Logger.UrlRewriteNotMatchedRule(Name);
return; return;
} }
} }
context.Logger?.UrlRewriteMatchedRule(Name); context.Logger.UrlRewriteMatchedRule(Name);
// at this point we know the rule passed, evaluate the replacement. // at this point we know the rule passed, evaluate the replacement.
Action.ApplyAction(context, initMatchResults?.BackReferences, condResult?.BackReferences); Action.ApplyAction(context, initMatchResults?.BackReferences, condResult?.BackReferences);
} }
} }
} }

View File

@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString.ToUriComponent(); response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString.ToUriComponent();
} }
context.Logger?.RedirectedRequest(newPath); context.Logger.RedirectedRequest(newPath);
} }
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text; using System.Text;
@ -35,8 +35,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
response.StatusCode = StatusCode; response.StatusCode = StatusCode;
response.Headers[HeaderNames.Location] = newUrl.ToString(); response.Headers[HeaderNames.Location] = newUrl.ToString();
context.Result = RuleResult.EndResponse; context.Result = RuleResult.EndResponse;
context.Logger?.RedirectedToHttps(); context.Logger.RedirectedToHttps();
} }
} }
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
response.StatusCode = _statusCode; response.StatusCode = _statusCode;
response.Headers[HeaderNames.Location] = newUrl; response.Headers[HeaderNames.Location] = newUrl;
context.Result = RuleResult.EndResponse; context.Result = RuleResult.EndResponse;
context.Logger?.RedirectedToWww(); context.Logger.RedirectedToWww();
} }
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
} }
} }
context.Logger?.RewrittenRequest(result); context.Logger.RewrittenRequest(result);
} }
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Rewrite.Logging; using Microsoft.AspNetCore.Rewrite.Logging;
@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions
{ {
context.HttpContext.Abort(); context.HttpContext.Abort();
context.Result = RuleResult.EndResponse; context.Result = RuleResult.EndResponse;
context.Logger?.AbortedRequest(context.HttpContext.Request.Path + context.HttpContext.Request.QueryString); context.Logger.AbortedRequest(context.HttpContext.Request.Path + context.HttpContext.Request.QueryString);
} }
} }
} }

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions
context.Result = RuleResult.EndResponse; context.Result = RuleResult.EndResponse;
context.Logger?.CustomResponse(context.HttpContext.Request.GetEncodedUrl()); context.Logger.CustomResponse(context.HttpContext.Request.GetEncodedUrl());
} }
} }
} }

View File

@ -1,10 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text; using System.Text;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
{ {
@ -26,7 +27,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Gets and sets the logger /// Gets and sets the logger
/// </summary> /// </summary>
public ILogger Logger { get; set; } public ILogger Logger { get; set; } = NullLogger.Instance;
/// <summary> /// <summary>
/// A shared result that is set appropriately by each rule for the next action that /// A shared result that is set appropriately by each rule for the next action that

View File

@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features; using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
{ {
@ -68,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
} }
_options = options; _options = options;
_logger = loggerFactory?.CreateLogger<HttpsConnectionAdapter>(); _logger = loggerFactory?.CreateLogger<HttpsConnectionAdapter>() ?? (ILogger)NullLogger.Instance;
} }
public bool IsHttps => true; public bool IsHttps => true;
@ -181,13 +182,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
_logger?.LogDebug(2, CoreStrings.AuthenticationTimedOut); _logger.LogDebug(2, CoreStrings.AuthenticationTimedOut);
sslStream.Dispose(); sslStream.Dispose();
return _closedAdaptedConnection; return _closedAdaptedConnection;
} }
catch (Exception ex) when (ex is IOException || ex is AuthenticationException) catch (Exception ex) when (ex is IOException || ex is AuthenticationException)
{ {
_logger?.LogDebug(1, ex, CoreStrings.AuthenticationFailed); _logger.LogDebug(1, ex, CoreStrings.AuthenticationFailed);
sslStream.Dispose(); sslStream.Dispose();
return _closedAdaptedConnection; return _closedAdaptedConnection;
} }