Fix obsolete constructor in RecurringHostedServiceBase (#12172)

* Use NullLogger in obsoleted constructor

* Create missing logger during execution instead
This commit is contained in:
Ronald Barendse
2022-03-24 11:34:29 +01:00
committed by GitHub
parent c1552cf7fc
commit d0823d4236
2 changed files with 11 additions and 18 deletions

View File

@@ -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<object> Logger => CreateLogger<object>();
public static ILogger<T> CreateLogger<T>()
{
return _loggerFactory?.CreateLogger<T>() ?? NullLoggerFactory.Instance.CreateLogger<T>();
}
public static ILogger<T> CreateLogger<T>() => s_loggerFactory?.CreateLogger<T>() ?? NullLoggerFactory.Instance.CreateLogger<T>();
public static ILogger CreateLogger(Type type) => s_loggerFactory?.CreateLogger(type) ?? NullLogger.Instance;
}
}

View File

@@ -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<ILoggerFactory>().CreateLogger(GetType());
}
: this(null, period, delay)
{ }
/// <inheritdoc/>
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;