From 611276a5ebee9b30e62dad6b7ab9335cf53a8ce8 Mon Sep 17 00:00:00 2001 From: Nikolaj Date: Wed, 16 Sep 2020 14:29:33 +0200 Subject: [PATCH] Fake SiriLogger for now and start using MSLogger more places --- src/Umbraco.Core/Logging/DisposableTimer.cs | 5 +- src/Umbraco.Core/Logging/ProfilingLogger.cs | 16 +++-- .../CompositionExtensions_Essentials.cs | 6 +- .../Runtime/CoreRuntime.cs | 4 +- src/Umbraco.Infrastructure/RuntimeState.cs | 6 +- .../Search/ExamineComponent.cs | 7 +- .../UmbracoExamine/IndexInitializer.cs | 2 +- src/Umbraco.Tests/Web/UmbracoHelperTests.cs | 2 +- .../UmbracoCoreServiceCollectionExtensions.cs | 68 ++++++++++--------- src/Umbraco.Web/UmbracoApplicationBase.cs | 2 +- 10 files changed, 65 insertions(+), 53 deletions(-) diff --git a/src/Umbraco.Core/Logging/DisposableTimer.cs b/src/Umbraco.Core/Logging/DisposableTimer.cs index a7dfbd254f..a076d15fae 100644 --- a/src/Umbraco.Core/Logging/DisposableTimer.cs +++ b/src/Umbraco.Core/Logging/DisposableTimer.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using Microsoft.Extensions.Logging; namespace Umbraco.Core.Logging { @@ -8,7 +9,7 @@ namespace Umbraco.Core.Logging /// public class DisposableTimer : DisposableObjectSlim { - private readonly ILogger _logger; + private readonly Microsoft.Extensions.Logging.ILogger _logger; private readonly LogLevel _level; private readonly Type _loggerType; private readonly int _thresholdMilliseconds; @@ -20,7 +21,7 @@ namespace Umbraco.Core.Logging private readonly string _timingId; // internal - created by profiling logger - internal DisposableTimer(ILogger logger, LogLevel level, IProfiler profiler, Type loggerType, + internal DisposableTimer(Microsoft.Extensions.Logging.ILogger logger, LogLevel level, IProfiler profiler, Type loggerType, string startMessage, string endMessage, string failMessage = null, int thresholdMilliseconds = 0) { diff --git a/src/Umbraco.Core/Logging/ProfilingLogger.cs b/src/Umbraco.Core/Logging/ProfilingLogger.cs index b20a9ae57a..386fcda619 100644 --- a/src/Umbraco.Core/Logging/ProfilingLogger.cs +++ b/src/Umbraco.Core/Logging/ProfilingLogger.cs @@ -1,4 +1,6 @@ using System; +using Microsoft.Extensions.Logging; + namespace Umbraco.Core.Logging { @@ -10,7 +12,7 @@ namespace Umbraco.Core.Logging /// /// Gets the underlying implementation. /// - public ILogger Logger { get; } + public Microsoft.Extensions.Logging.ILogger Logger { get; } /// /// Gets the underlying implementation. @@ -20,7 +22,7 @@ namespace Umbraco.Core.Logging /// /// Initializes a new instance of the class. /// - public ProfilingLogger(ILogger logger, IProfiler profiler) + public ProfilingLogger(Microsoft.Extensions.Logging.ILogger logger, IProfiler profiler) { Logger = logger ?? throw new ArgumentNullException(nameof(logger)); Profiler = profiler ?? throw new ArgumentNullException(nameof(profiler)); @@ -43,29 +45,29 @@ namespace Umbraco.Core.Logging public DisposableTimer DebugDuration(string startMessage) { - return Logger.IsEnabled(typeof(T), LogLevel.Debug) + return Logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug) ? DebugDuration(startMessage, "Completed.") : null; } public DisposableTimer DebugDuration(string startMessage, string completeMessage, string failMessage = null, int thresholdMilliseconds = 0) { - return Logger.IsEnabled(typeof(T), LogLevel.Debug) + return Logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug) ? new DisposableTimer(Logger, LogLevel.Debug, Profiler, typeof(T), startMessage, completeMessage, failMessage, thresholdMilliseconds) : null; } public DisposableTimer DebugDuration(Type loggerType, string startMessage, string completeMessage, string failMessage = null, int thresholdMilliseconds = 0) { - return Logger.IsEnabled(loggerType, LogLevel.Debug) + return Logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug) ? new DisposableTimer(Logger, LogLevel.Debug, Profiler, loggerType, startMessage, completeMessage, failMessage, thresholdMilliseconds) : null; } #region ILogger - public bool IsEnabled(Type reporting, LogLevel level) - => Logger.IsEnabled(reporting, level); + public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel level) + => Logger.IsEnabled(level); public void LogCritical(Exception exception, string messageTemplate, params object[] propertyValues) => Logger.LogCritical(exception, messageTemplate, propertyValues); diff --git a/src/Umbraco.Infrastructure/CompositionExtensions_Essentials.cs b/src/Umbraco.Infrastructure/CompositionExtensions_Essentials.cs index b2ded07034..aba46baf8f 100644 --- a/src/Umbraco.Infrastructure/CompositionExtensions_Essentials.cs +++ b/src/Umbraco.Infrastructure/CompositionExtensions_Essentials.cs @@ -1,10 +1,12 @@ -using Umbraco.Core.Cache; +using Microsoft.Extensions.Logging; +using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Configuration; using Umbraco.Core.Hosting; using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Umbraco.Core { @@ -37,7 +39,7 @@ namespace Umbraco.Core IDbProviderFactoryCreator dbProviderFactoryCreator, IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo) - { + { composition.RegisterUnique(logger); composition.RegisterUnique(profiler); composition.RegisterUnique(profilingLogger); diff --git a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs index ec59837570..0cb22c4560 100644 --- a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs +++ b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs @@ -173,7 +173,7 @@ namespace Umbraco.Core.Runtime var typeLoader = new TypeLoader(TypeFinder, AppCaches.RuntimeCache, new DirectoryInfo(HostingEnvironment.LocalTempPath), _loggerFactory.CreateLogger("TypeLoader"), ProfilingLogger); // re-create the state object with the essential services - _state = new RuntimeState(Configs.Global(), UmbracoVersion, databaseFactory, Logger); + _state = new RuntimeState(Configs.Global(), UmbracoVersion, databaseFactory, _loggerFactory.CreateLogger()); // create the composition composition = new Composition(register, typeLoader, ProfilingLogger, _state, Configs, IOHelper, AppCaches); @@ -384,7 +384,7 @@ namespace Umbraco.Core.Runtime /// /// This is strictly internal, for tests only. protected internal virtual IUmbracoDatabaseFactory CreateDatabaseFactory() - => new UmbracoDatabaseFactory(Logger, _globalSettings, _connectionStrings, new Lazy(() => _factory.GetInstance()), DbProviderFactoryCreator); + => new UmbracoDatabaseFactory(_loggerFactory.CreateLogger(), _loggerFactory, _globalSettings, _connectionStrings, new Lazy(() => _factory.GetInstance()), DbProviderFactoryCreator); #endregion diff --git a/src/Umbraco.Infrastructure/RuntimeState.cs b/src/Umbraco.Infrastructure/RuntimeState.cs index b23e5ad9f1..6adb9b852d 100644 --- a/src/Umbraco.Infrastructure/RuntimeState.cs +++ b/src/Umbraco.Infrastructure/RuntimeState.cs @@ -1,9 +1,9 @@ using System; using System.Threading; using Semver; +using Microsoft.Extensions.Logging; using Umbraco.Core.Configuration; using Umbraco.Core.Exceptions; -using Umbraco.Core.Logging; using Umbraco.Core.Migrations.Upgrade; using Umbraco.Core.Persistence; @@ -17,7 +17,7 @@ namespace Umbraco.Core private readonly IGlobalSettings _globalSettings; private readonly IUmbracoVersion _umbracoVersion; private readonly IUmbracoDatabaseFactory _databaseFactory; - private readonly ILogger _logger; + private readonly ILogger _logger; /// /// The initial @@ -31,7 +31,7 @@ namespace Umbraco.Core /// /// Initializes a new instance of the class. /// - public RuntimeState(IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoDatabaseFactory databaseFactory, ILogger logger) + public RuntimeState(IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoDatabaseFactory databaseFactory, ILogger logger) { _globalSettings = globalSettings; _umbracoVersion = umbracoVersion; diff --git a/src/Umbraco.Infrastructure/Search/ExamineComponent.cs b/src/Umbraco.Infrastructure/Search/ExamineComponent.cs index 297d7ac2aa..619edebd26 100644 --- a/src/Umbraco.Infrastructure/Search/ExamineComponent.cs +++ b/src/Umbraco.Infrastructure/Search/ExamineComponent.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Services.Changes; using Umbraco.Core.Sync; using Umbraco.Web.Cache; using Umbraco.Examine; +using Microsoft.Extensions.Logging; namespace Umbraco.Web.Search { @@ -28,7 +29,7 @@ namespace Umbraco.Web.Search private readonly ServiceContext _services; private readonly IMainDom _mainDom; private readonly IProfilingLogger _pLogger; - private readonly ILogger _logger; + private readonly Microsoft.Extensions.Logging.ILogger _logger; private readonly IUmbracoIndexesCreator _indexCreator; @@ -39,7 +40,7 @@ namespace Umbraco.Web.Search public ExamineComponent(IMainDom mainDom, IExamineManager examineManager, IProfilingLogger profilingLogger, - ILogger logger, + Microsoft.Extensions.Logging.ILogger logger, IScopeProvider scopeProvider, IUmbracoIndexesCreator indexCreator, ServiceContext services, IContentValueSetBuilder contentValueSetBuilder, @@ -78,7 +79,7 @@ namespace Umbraco.Web.Search _logger.LogInformation("Examine shutdown not registered, this AppDomain is not the MainDom, Examine will be disabled"); //if we could not register the shutdown examine ourselves, it means we are not maindom! in this case all of examine should be disabled! - Suspendable.ExamineEvents.SuspendIndexers(_logger as ILogger); + Suspendable.ExamineEvents.SuspendIndexers(_logger); return; //exit, do not continue } diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs index 81964eb714..0304f4005f 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs @@ -160,7 +160,7 @@ namespace Umbraco.Tests.UmbracoExamine public static IProfilingLogger GetMockProfilingLogger() { - return new ProfilingLogger(Mock.Of(), Mock.Of()); + return new ProfilingLogger(Mock.Of(), Mock.Of()); } public static Core.Logging.ILogger GetMockLogger() diff --git a/src/Umbraco.Tests/Web/UmbracoHelperTests.cs b/src/Umbraco.Tests/Web/UmbracoHelperTests.cs index 38e6657892..9485fc5a89 100644 --- a/src/Umbraco.Tests/Web/UmbracoHelperTests.cs +++ b/src/Umbraco.Tests/Web/UmbracoHelperTests.cs @@ -38,7 +38,7 @@ namespace Umbraco.Tests.Web NoAppCache.Instance, new DirectoryInfo(ioHelper.MapPath("~/App_Data/TEMP")), Mock.Of(), - new ProfilingLogger(Mock.Of(), Mock.Of()) + new ProfilingLogger(Mock.Of(), Mock.Of()) ) ); diff --git a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs index 2d2230dcc8..d0f47a0da9 100644 --- a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs @@ -30,6 +30,7 @@ using Umbraco.Core.Runtime; using Umbraco.Web.Common.AspNetCore; using Umbraco.Web.Common.Extensions; using Umbraco.Web.Common.Profiler; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Umbraco.Extensions { @@ -208,7 +209,7 @@ namespace Umbraco.Extensions AppCaches appCaches, ILoggingConfiguration loggingConfiguration, // TODO: Yep that's extremely ugly - Func getRuntime, + Func getRuntime, out IFactory factory) { if (services is null) throw new ArgumentNullException(nameof(services)); @@ -248,7 +249,7 @@ namespace Umbraco.Extensions var globalSettings = configs.Global(); var umbracoVersion = new UmbracoVersion(globalSettings); - var typeFinder = CreateTypeFinder(logger, Current.LoggerFactory, profiler, webHostEnvironment, entryAssembly, configs.TypeFinder()); + var typeFinder = CreateTypeFinder(Current.LoggerFactory, profiler, webHostEnvironment, entryAssembly, configs.TypeFinder()); var runtime = getRuntime( configs, @@ -268,16 +269,16 @@ namespace Umbraco.Extensions return services; } - private static ITypeFinder CreateTypeFinder(Core.Logging.ILogger logger, ILoggerFactory loggerFactory, IProfiler profiler, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, ITypeFinderSettings typeFinderSettings) + private static ITypeFinder CreateTypeFinder(ILoggerFactory loggerFactory, IProfiler profiler, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, ITypeFinderSettings typeFinderSettings) { var runtimeHashPaths = new RuntimeHashPaths(); runtimeHashPaths.AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin"))); - var runtimeHash = new RuntimeHash(new ProfilingLogger(logger, profiler), runtimeHashPaths); + var runtimeHash = new RuntimeHash(new ProfilingLogger(loggerFactory.CreateLogger("RuntimeHash"), profiler), runtimeHashPaths); return new TypeFinder(loggerFactory.CreateLogger(), new DefaultUmbracoAssemblyProvider(entryAssembly), runtimeHash, new TypeFinderConfig(typeFinderSettings)); } private static IRuntime GetCoreRuntime( - Configs configs, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, Core.Logging.ILogger logger, ILoggerFactory loggerFactory, + Configs configs, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, ILogger logger, ILoggerFactory loggerFactory, IProfiler profiler, Core.Hosting.IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo, ITypeFinder typeFinder, AppCaches appCaches, IDbProviderFactoryCreator dbProviderFactoryCreator) { @@ -289,10 +290,10 @@ namespace Umbraco.Extensions var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); var mainDomLock = appSettingMainDomLock == "SqlMainDomLock" || isWindows == false - ? (IMainDomLock)new SqlMainDomLock(logger, globalSettings, connStrings, dbProviderFactoryCreator, hostingEnvironment) - : new MainDomSemaphoreLock(logger, hostingEnvironment); + ? (IMainDomLock)new SqlMainDomLock(loggerFactory.CreateLogger(), loggerFactory, globalSettings, connStrings, dbProviderFactoryCreator, hostingEnvironment) + : new MainDomSemaphoreLock(loggerFactory.CreateLogger(), hostingEnvironment); - var mainDom = new MainDom(logger, mainDomLock); + var mainDom = new MainDom(loggerFactory.CreateLogger(), mainDomLock); var coreRuntime = new CoreRuntime( configs, @@ -317,7 +318,7 @@ namespace Umbraco.Extensions Configs configs, IWebHostEnvironment webHostEnvironment, ILoggingConfiguration loggingConfiguration, - out Core.Logging.ILogger logger, + out ILogger logger, out IIOHelper ioHelper, out Core.Hosting.IHostingEnvironment hostingEnvironment, out IBackOfficeInfo backOfficeInfo, @@ -343,32 +344,37 @@ namespace Umbraco.Extensions /// Create and configure the logger /// /// - private static Core.Logging.ILogger AddLogger(IServiceCollection services, Core.Hosting.IHostingEnvironment hostingEnvironment, ILoggingConfiguration loggingConfiguration) + private static ILogger AddLogger(IServiceCollection services, Core.Hosting.IHostingEnvironment hostingEnvironment, ILoggingConfiguration loggingConfiguration) { - // Create a serilog logger - var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration); + // // Create a serilog logger + // var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration); + // + // // Wire up all the bits that serilog needs. We need to use our own code since the Serilog ext methods don't cater to our needs since + // // we don't want to use the global serilog `Log` object and we don't have our own ILogger implementation before the HostBuilder runs which + // // is the only other option that these ext methods allow. + // // I have created a PR to make this nicer https://github.com/serilog/serilog-extensions-hosting/pull/19 but we'll need to wait for that. + // // Also see : https://github.com/serilog/serilog-extensions-hosting/blob/dev/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs + // + // services.AddSingleton(services => new SerilogLoggerFactory(logger.SerilogLog, false)); - // Wire up all the bits that serilog needs. We need to use our own code since the Serilog ext methods don't cater to our needs since - // we don't want to use the global serilog `Log` object and we don't have our own ILogger implementation before the HostBuilder runs which - // is the only other option that these ext methods allow. - // I have created a PR to make this nicer https://github.com/serilog/serilog-extensions-hosting/pull/19 but we'll need to wait for that. - // Also see : https://github.com/serilog/serilog-extensions-hosting/blob/dev/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs + var factory = LoggerFactory.Create(builder => + { + builder.AddSerilog(); + }); - services.AddSingleton(services => new SerilogLoggerFactory(logger.SerilogLog, false)); + // // This won't (and shouldn't) take ownership of the logger. + // services.AddSingleton(logger.SerilogLog); + // + // // Registered to provide two services... + // var diagnosticContext = new DiagnosticContext(logger.SerilogLog); + // + // // Consumed by e.g. middleware + // services.AddSingleton(diagnosticContext); + // + // // Consumed by user code + // services.AddSingleton(diagnosticContext); - // This won't (and shouldn't) take ownership of the logger. - services.AddSingleton(logger.SerilogLog); - - // Registered to provide two services... - var diagnosticContext = new DiagnosticContext(logger.SerilogLog); - - // Consumed by e.g. middleware - services.AddSingleton(diagnosticContext); - - // Consumed by user code - services.AddSingleton(diagnosticContext); - - return logger; + return factory.CreateLogger("FakeSiriLogger"); } private static IProfiler GetWebProfiler(Umbraco.Core.Hosting.IHostingEnvironment hostingEnvironment) diff --git a/src/Umbraco.Web/UmbracoApplicationBase.cs b/src/Umbraco.Web/UmbracoApplicationBase.cs index 711d534d8e..1136f6bdd1 100644 --- a/src/Umbraco.Web/UmbracoApplicationBase.cs +++ b/src/Umbraco.Web/UmbracoApplicationBase.cs @@ -119,7 +119,7 @@ namespace Umbraco.Web runtimeHashPaths.AddFile(new FileInfo(hostingEnvironment.MapPathContentRoot("~/App_Code"))); // global.asax (the app domain also monitors this, if it changes will do a full restart) runtimeHashPaths.AddFile(new FileInfo(hostingEnvironment.MapPathContentRoot("~/global.asax"))); - var runtimeHash = new RuntimeHash(new ProfilingLogger(logger, profiler), runtimeHashPaths); + var runtimeHash = new RuntimeHash(new ProfilingLogger(_loggerFactory.CreateLogger("RuntimeHash"), profiler), runtimeHashPaths); return new TypeFinder(_loggerFactory.CreateLogger(), new DefaultUmbracoAssemblyProvider( // GetEntryAssembly was actually an exposed API by request of the aspnetcore team which works in aspnet core because a website // in that case is essentially an exe. However in netframework there is no entry assembly, things don't really work that way since