From 037580b305d0b0771dbe7f5e0f40dfdceeae62ec Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Thu, 24 Mar 2022 11:34:29 +0100 Subject: [PATCH] Fix obsolete constructor in RecurringHostedServiceBase (#12172) * Use NullLogger in obsoleted constructor * Create missing logger during execution instead (cherry picked from commit d0823d4236b527882571dfd04ed59bcbe52a9717) --- .../UmbracoPackage/.template.config/template.json | 2 +- .../UmbracoProject/.template.config/template.json | 2 +- src/Directory.Build.props | 10 +++++----- src/Umbraco.Core/StaticApplicationLogging.cs | 14 +++++--------- .../HostedServices/RecurringHostedServiceBase.cs | 15 ++++++--------- 5 files changed, 18 insertions(+), 25 deletions(-) diff --git a/build/templates/UmbracoPackage/.template.config/template.json b/build/templates/UmbracoPackage/.template.config/template.json index 1889fa706e..64da9a2553 100644 --- a/build/templates/UmbracoPackage/.template.config/template.json +++ b/build/templates/UmbracoPackage/.template.config/template.json @@ -24,7 +24,7 @@ "version": { "type": "parameter", "datatype": "string", - "defaultValue": "9.4.0", + "defaultValue": "9.4.1", "description": "The version of Umbraco to load using NuGet", "replaces": "UMBRACO_VERSION_FROM_TEMPLATE" }, diff --git a/build/templates/UmbracoProject/.template.config/template.json b/build/templates/UmbracoProject/.template.config/template.json index 780d560154..a2d6400f8f 100644 --- a/build/templates/UmbracoProject/.template.config/template.json +++ b/build/templates/UmbracoProject/.template.config/template.json @@ -57,7 +57,7 @@ "version": { "type": "parameter", "datatype": "string", - "defaultValue": "9.4.0", + "defaultValue": "9.4.1", "description": "The version of Umbraco to load using NuGet", "replaces": "UMBRACO_VERSION_FROM_TEMPLATE" }, diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 19773adb37..328f3c2278 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,12 +1,12 @@ - + - 9.4.0 - 9.4.0 - 9.4.0 - 9.4.0 + 9.4.1 + 9.4.1 + 9.4.1 + 9.4.1 9.0 en-US Umbraco CMS diff --git a/src/Umbraco.Core/StaticApplicationLogging.cs b/src/Umbraco.Core/StaticApplicationLogging.cs index 73078b0f42..d7dfc8dd9a 100644 --- a/src/Umbraco.Core/StaticApplicationLogging.cs +++ b/src/Umbraco.Core/StaticApplicationLogging.cs @@ -6,18 +6,14 @@ namespace Umbraco.Cms.Core { public static class StaticApplicationLogging { - private static ILoggerFactory _loggerFactory; + private static ILoggerFactory s_loggerFactory; - public static void Initialize(ILoggerFactory loggerFactory) - { - _loggerFactory = loggerFactory; - } + public static void Initialize(ILoggerFactory loggerFactory) => s_loggerFactory = loggerFactory; public static ILogger Logger => CreateLogger(); - public static ILogger CreateLogger() - { - return _loggerFactory?.CreateLogger() ?? NullLoggerFactory.Instance.CreateLogger(); - } + public static ILogger CreateLogger() => s_loggerFactory?.CreateLogger() ?? NullLoggerFactory.Instance.CreateLogger(); + + public static ILogger CreateLogger(Type type) => s_loggerFactory?.CreateLogger(type) ?? NullLogger.Instance; } } diff --git a/src/Umbraco.Infrastructure/HostedServices/RecurringHostedServiceBase.cs b/src/Umbraco.Infrastructure/HostedServices/RecurringHostedServiceBase.cs index c1c7cdf3cf..5247a125bc 100644 --- a/src/Umbraco.Infrastructure/HostedServices/RecurringHostedServiceBase.cs +++ b/src/Umbraco.Infrastructure/HostedServices/RecurringHostedServiceBase.cs @@ -4,10 +4,9 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Umbraco.Cms.Web.Common.DependencyInjection; +using Umbraco.Cms.Core; namespace Umbraco.Cms.Infrastructure.HostedServices { @@ -46,11 +45,8 @@ namespace Umbraco.Cms.Infrastructure.HostedServices // Scheduled for removal in V11 [Obsolete("Please use constructor that takes an ILogger instead")] protected RecurringHostedServiceBase(TimeSpan period, TimeSpan delay) - { - _period = period; - _delay = delay; - _logger = StaticServiceProvider.Instance.GetRequiredService().CreateLogger(GetType()); - } + : this(null, period, delay) + { } /// public Task StartAsync(CancellationToken cancellationToken) @@ -82,7 +78,8 @@ namespace Umbraco.Cms.Infrastructure.HostedServices } catch (Exception ex) { - _logger.LogError(ex, "Unhandled exception in recurring hosted service."); + ILogger logger = _logger ?? StaticApplicationLogging.CreateLogger(GetType()); + logger.LogError(ex, "Unhandled exception in recurring hosted service."); } finally { @@ -108,7 +105,7 @@ namespace Umbraco.Cms.Infrastructure.HostedServices { if (disposing) { - _timer?.Dispose(); + _timer?.Dispose(); } _disposedValue = true;