diff --git a/src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactory.cs b/src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactory.cs index 39b6c0e55d..947ab4b56c 100644 --- a/src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactory.cs +++ b/src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactory.cs @@ -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. using System; @@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption BCryptAlgorithmHandle algorithmHandle = null; - _logger?.OpeningCNGAlgorithmFromProviderWithChainingModeGCM(configuration.EncryptionAlgorithm, configuration.EncryptionAlgorithmProvider); + _logger.OpeningCNGAlgorithmFromProviderWithChainingModeGCM(configuration.EncryptionAlgorithm, configuration.EncryptionAlgorithmProvider); // Special-case cached providers if (configuration.EncryptionAlgorithmProvider == null) { diff --git a/src/DataProtection/DataProtection/src/LoggingServiceProviderExtensions.cs b/src/DataProtection/DataProtection/src/LoggingServiceProviderExtensions.cs index 199f9fd35c..9b50b6ab11 100644 --- a/src/DataProtection/DataProtection/src/LoggingServiceProviderExtensions.cs +++ b/src/DataProtection/DataProtection/src/LoggingServiceProviderExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace System { @@ -37,7 +38,7 @@ namespace System { // Compiler won't allow us to use static types as the type parameter // for the call to CreateLogger, so we'll duplicate its logic here. - return services?.GetService()?.CreateLogger(type.FullName); + return services?.GetService()?.CreateLogger(type.FullName) ?? NullLogger.Instance; } } } diff --git a/src/DataProtection/DataProtection/src/XmlEncryption/DpapiNGXmlDecryptor.cs b/src/DataProtection/DataProtection/src/XmlEncryption/DpapiNGXmlDecryptor.cs index 653beffad1..7be3394284 100644 --- a/src/DataProtection/DataProtection/src/XmlEncryption/DpapiNGXmlDecryptor.cs +++ b/src/DataProtection/DataProtection/src/XmlEncryption/DpapiNGXmlDecryptor.cs @@ -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 // sensitive information. - _logger?.ExceptionOccurredTryingToDecryptElement(ex); + _logger.ExceptionOccurredTryingToDecryptElement(ex); throw; } } diff --git a/src/DataProtection/DataProtection/src/XmlEncryption/DpapiXmlDecryptor.cs b/src/DataProtection/DataProtection/src/XmlEncryption/DpapiXmlDecryptor.cs index 6241263350..4bc9a1b354 100644 --- a/src/DataProtection/DataProtection/src/XmlEncryption/DpapiXmlDecryptor.cs +++ b/src/DataProtection/DataProtection/src/XmlEncryption/DpapiXmlDecryptor.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption throw new ArgumentNullException(nameof(encryptedElement)); } - _logger?.DecryptingSecretElementUsingWindowsDPAPI(); + _logger.DecryptingSecretElementUsingWindowsDPAPI(); 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 // sensitive information. - _logger?.ExceptionOccurredTryingToDecryptElement(ex); + _logger.ExceptionOccurredTryingToDecryptElement(ex); throw; } } diff --git a/src/DataProtection/DataProtection/src/XmlEncryption/NullXmlEncryptor.cs b/src/DataProtection/DataProtection/src/XmlEncryption/NullXmlEncryptor.cs index 0f3100b859..1633cd2150 100644 --- a/src/DataProtection/DataProtection/src/XmlEncryption/NullXmlEncryptor.cs +++ b/src/DataProtection/DataProtection/src/XmlEncryption/NullXmlEncryptor.cs @@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption throw new ArgumentNullException(nameof(plaintextElement)); } - _logger?.EncryptingUsingNullEncryptor(); + _logger.EncryptingUsingNullEncryptor(); // // diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index 20f9ff7b0b..ec6d3d84a0 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -21,6 +21,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.StackTrace.Sources; using Microsoft.Net.Http.Headers; @@ -42,7 +43,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal private IServiceProvider _applicationServices; private ExceptionDispatchInfo _applicationServicesException; - private ILogger _logger; + private ILogger _logger = NullLogger.Instance; private bool _stopped; private bool _startedServer; @@ -317,7 +318,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal } _stopped = true; - _logger?.Shutdown(); + _logger.Shutdown(); var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token; if (!cancellationToken.CanBeCanceled) @@ -364,7 +365,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal } catch (Exception ex) { - _logger?.ServerShutdownException(ex); + _logger.ServerShutdownException(ex); } } diff --git a/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj b/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj index 00037213e0..3ea91fde9f 100644 --- a/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj +++ b/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj @@ -16,9 +16,7 @@ - - Component - + diff --git a/src/Middleware/Rewrite/src/Internal/ApacheModRewrite/ApacheModRewriteRule.cs b/src/Middleware/Rewrite/src/Internal/ApacheModRewrite/ApacheModRewriteRule.cs index 04abcb1221..d93875fb7f 100644 --- a/src/Middleware/Rewrite/src/Internal/ApacheModRewrite/ApacheModRewriteRule.cs +++ b/src/Middleware/Rewrite/src/Internal/ApacheModRewrite/ApacheModRewriteRule.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite if (!initMatchRes.Success) { - context.Logger?.ModRewriteNotMatchedRule(); + context.Logger.ModRewriteNotMatchedRule(); return; } @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite var condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchRes.BackReferences); if (!condResult.Success) { - context.Logger?.ModRewriteNotMatchedRule(); + context.Logger.ModRewriteNotMatchedRule(); return; } @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite // 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 - context.Logger?.ModRewriteMatchedRule(); + context.Logger.ModRewriteMatchedRule(); foreach (var action in Actions) { diff --git a/src/Middleware/Rewrite/src/Internal/IISUrlRewrite/IISUrlRewriteRule.cs b/src/Middleware/Rewrite/src/Internal/IISUrlRewrite/IISUrlRewriteRule.cs index 4636c2187c..d77f93d294 100644 --- a/src/Middleware/Rewrite/src/Internal/IISUrlRewrite/IISUrlRewriteRule.cs +++ b/src/Middleware/Rewrite/src/Internal/IISUrlRewrite/IISUrlRewriteRule.cs @@ -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. using Microsoft.AspNetCore.Http; @@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite if (!initMatchResults.Success) { - context.Logger?.UrlRewriteNotMatchedRule(Name); + context.Logger.UrlRewriteNotMatchedRule(Name); return; } @@ -62,14 +62,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchResults.BackReferences); if (!condResult.Success) { - context.Logger?.UrlRewriteNotMatchedRule(Name); + context.Logger.UrlRewriteNotMatchedRule(Name); return; } } - context.Logger?.UrlRewriteMatchedRule(Name); + context.Logger.UrlRewriteMatchedRule(Name); // at this point we know the rule passed, evaluate the replacement. Action.ApplyAction(context, initMatchResults?.BackReferences, condResult?.BackReferences); } } -} \ No newline at end of file +} diff --git a/src/Middleware/Rewrite/src/Internal/RedirectRule.cs b/src/Middleware/Rewrite/src/Internal/RedirectRule.cs index 2b4825c4b1..cf7ae0767c 100644 --- a/src/Middleware/Rewrite/src/Internal/RedirectRule.cs +++ b/src/Middleware/Rewrite/src/Internal/RedirectRule.cs @@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString.ToUriComponent(); } - context.Logger?.RedirectedRequest(newPath); + context.Logger.RedirectedRequest(newPath); } } } diff --git a/src/Middleware/Rewrite/src/Internal/RedirectToHttpsRule.cs b/src/Middleware/Rewrite/src/Internal/RedirectToHttpsRule.cs index 46d434d128..ae481c8824 100644 --- a/src/Middleware/Rewrite/src/Internal/RedirectToHttpsRule.cs +++ b/src/Middleware/Rewrite/src/Internal/RedirectToHttpsRule.cs @@ -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. using System.Text; @@ -35,8 +35,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal response.StatusCode = StatusCode; response.Headers[HeaderNames.Location] = newUrl.ToString(); context.Result = RuleResult.EndResponse; - context.Logger?.RedirectedToHttps(); + context.Logger.RedirectedToHttps(); } } } -} \ No newline at end of file +} diff --git a/src/Middleware/Rewrite/src/Internal/RedirectToWwwRule.cs b/src/Middleware/Rewrite/src/Internal/RedirectToWwwRule.cs index a445d7227e..d368d61446 100644 --- a/src/Middleware/Rewrite/src/Internal/RedirectToWwwRule.cs +++ b/src/Middleware/Rewrite/src/Internal/RedirectToWwwRule.cs @@ -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. using System; @@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal response.StatusCode = _statusCode; response.Headers[HeaderNames.Location] = newUrl; context.Result = RuleResult.EndResponse; - context.Logger?.RedirectedToWww(); + context.Logger.RedirectedToWww(); } } -} \ No newline at end of file +} diff --git a/src/Middleware/Rewrite/src/Internal/RewriteRule.cs b/src/Middleware/Rewrite/src/Internal/RewriteRule.cs index f33f3a2b40..79416b548f 100644 --- a/src/Middleware/Rewrite/src/Internal/RewriteRule.cs +++ b/src/Middleware/Rewrite/src/Internal/RewriteRule.cs @@ -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. using System; @@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal } } - context.Logger?.RewrittenRequest(result); + context.Logger.RewrittenRequest(result); } } } diff --git a/src/Middleware/Rewrite/src/Internal/UrlActions/AbortAction.cs b/src/Middleware/Rewrite/src/Internal/UrlActions/AbortAction.cs index 32367f5f7b..d5c3182fb1 100644 --- a/src/Middleware/Rewrite/src/Internal/UrlActions/AbortAction.cs +++ b/src/Middleware/Rewrite/src/Internal/UrlActions/AbortAction.cs @@ -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. using Microsoft.AspNetCore.Rewrite.Logging; @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions { context.HttpContext.Abort(); 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); } } } diff --git a/src/Middleware/Rewrite/src/Internal/UrlActions/CustomResponseAction.cs b/src/Middleware/Rewrite/src/Internal/UrlActions/CustomResponseAction.cs index 4fc7d1615c..5fb4253b24 100644 --- a/src/Middleware/Rewrite/src/Internal/UrlActions/CustomResponseAction.cs +++ b/src/Middleware/Rewrite/src/Internal/UrlActions/CustomResponseAction.cs @@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions context.Result = RuleResult.EndResponse; - context.Logger?.CustomResponse(context.HttpContext.Request.GetEncodedUrl()); + context.Logger.CustomResponse(context.HttpContext.Request.GetEncodedUrl()); } } } diff --git a/src/Middleware/Rewrite/src/RewriteContext.cs b/src/Middleware/Rewrite/src/RewriteContext.cs index 061c45a18f..09b20c057f 100644 --- a/src/Middleware/Rewrite/src/RewriteContext.cs +++ b/src/Middleware/Rewrite/src/RewriteContext.cs @@ -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. using System.Text; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.Rewrite { @@ -26,7 +27,7 @@ namespace Microsoft.AspNetCore.Rewrite /// /// Gets and sets the logger /// - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } = NullLogger.Instance; /// /// A shared result that is set appropriately by each rule for the next action that diff --git a/src/Servers/Kestrel/Core/src/Internal/HttpsConnectionAdapter.cs b/src/Servers/Kestrel/Core/src/Internal/HttpsConnectionAdapter.cs index 3986380e60..46ad0ae132 100644 --- a/src/Servers/Kestrel/Core/src/Internal/HttpsConnectionAdapter.cs +++ b/src/Servers/Kestrel/Core/src/Internal/HttpsConnectionAdapter.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Features; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal { @@ -68,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal } _options = options; - _logger = loggerFactory?.CreateLogger(); + _logger = loggerFactory?.CreateLogger() ?? (ILogger)NullLogger.Instance; } public bool IsHttps => true; @@ -181,13 +182,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal } catch (OperationCanceledException) { - _logger?.LogDebug(2, CoreStrings.AuthenticationTimedOut); + _logger.LogDebug(2, CoreStrings.AuthenticationTimedOut); sslStream.Dispose(); return _closedAdaptedConnection; } catch (Exception ex) when (ex is IOException || ex is AuthenticationException) { - _logger?.LogDebug(1, ex, CoreStrings.AuthenticationFailed); + _logger.LogDebug(1, ex, CoreStrings.AuthenticationFailed); sslStream.Dispose(); return _closedAdaptedConnection; }