Skip to content

Commit 5df4ce9

Browse files
committed
Use static field for the current log provider for backward compatibility
1 parent 868cf02 commit 5df4ce9

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/Hangfire.Core/App_Packages/LibLog.1.4/LibLog.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ public interface ILogProvider
329329
public static class LogProvider
330330
{
331331
private static Lazy<ILogProvider?> _resolvedLogProvider = new Lazy<ILogProvider?>(ResolveLogProvider, LazyThreadSafetyMode.PublicationOnly);
332+
private static ILogProvider? _currentLogProvider;
332333

333334
/// <summary>
334335
/// Gets a logger for the specified type.
@@ -379,17 +380,23 @@ public static ILog GetLogger(string name)
379380
/// <returns>An instance of <see cref="ILogProvider"/>, or <see langword="null" /> when it is not registered.</returns>
380381
public static ILogProvider GetCurrentLogProvider()
381382
{
382-
return GlobalConfiguration.Configuration.ResolveService<ILogProvider>();
383+
return _currentLogProvider ?? _resolvedLogProvider.Value ?? NoOpLogProvider.Instance;
383384
}
384385

385386
/// <summary>
386387
/// Sets the current log provider.
387388
/// </summary>
388389
/// <param name="logProvider">The log provider.</param>
389-
[Obsolete]
390390
public static void SetCurrentLogProvider(ILogProvider? logProvider)
391391
{
392-
GlobalConfiguration.Configuration.UseLogProvider(logProvider ?? NoOpLogProvider.Instance);
392+
// We can not use GlobalConfiguration.Configuration.Register here as with other
393+
// static members, like JobStorage.Current and JobActivator.Current, because we
394+
// should make the value accessible as soon as possible to allow other services
395+
// that can be configured with the IGlobalConfiguration pipeline to use the new
396+
// value, if they are based on obsolete LogProvider.GetLogger method.
397+
// Some contexts, like modern .NET Core applications flush the current
398+
// GlobalConfiguration.Configuration value only after everything is configured.
399+
_currentLogProvider = logProvider; // ?? NoOpLogProvider.Instance?
393400
}
394401

395402
internal delegate bool IsLoggerAvailable();

src/Hangfire.Core/GlobalConfigurationExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public static IGlobalConfiguration<TLogProvider> UseLogProvider<TLogProvider>(
9999
if (provider == null) throw new ArgumentNullException(nameof(provider));
100100

101101
configuration.RegisterService<ILogProvider>(provider);
102-
return configuration.Use(provider, entryAction: null);
102+
103+
// Using LogProvider.SetCurrentLogProvider for backward compatibility, since there are
104+
// still a lot of consumers of the obsolete `LogProvider.GetLogger` method.
105+
return configuration.Use(provider, static x => LogProvider.SetCurrentLogProvider(x));
103106
}
104107

105108
public static IGlobalConfiguration<NLogLogProvider> UseNLogLogProvider(

0 commit comments

Comments
 (0)