diff --git a/src/Umbraco.Core/Runtime/CoreRuntime.cs b/src/Umbraco.Core/Runtime/CoreRuntime.cs old mode 100644 new mode 100755 index a8cf18cd23..cf2712974d --- a/src/Umbraco.Core/Runtime/CoreRuntime.cs +++ b/src/Umbraco.Core/Runtime/CoreRuntime.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Configuration; +using System.Reflection; using System.Threading; using System.Web; using LightInject; @@ -12,6 +13,7 @@ using Umbraco.Core.Configuration; using Umbraco.Core.Exceptions; using Umbraco.Core.IO; using Umbraco.Core.Logging; +using Umbraco.Core.Logging.Serilog; using Umbraco.Core.Migrations.Upgrade; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Dtos; @@ -29,26 +31,29 @@ namespace Umbraco.Core.Runtime /// should be possible to use this runtime in console apps. public class CoreRuntime : IRuntime { - private readonly UmbracoApplicationBase _app; private BootLoader _bootLoader; private RuntimeState _state; /// /// Initializes a new instance of the class. /// - /// The Umbraco HttpApplication. - public CoreRuntime(UmbracoApplicationBase umbracoApplication) - { - _app = umbracoApplication ?? throw new ArgumentNullException(nameof(umbracoApplication)); - } + public CoreRuntime() + { } /// public virtual void Boot(ServiceContainer container) { - // some components may want to initialize with the UmbracoApplicationBase - // well, they should not - we should not do this - // TODO remove this eventually. - container.RegisterInstance(_app); + container.ConfigureUmbracoCore(); // also sets Current.Container + + // register the essential stuff, + // ie the global application logger + // (profiler etc depend on boot manager) + var logger = GetLogger(); + container.RegisterInstance(logger); + // now it is ok to use Current.Logger + + ConfigureUnhandledException(logger); + ConfigureAssemblyResolve(logger); Compose(container); @@ -115,6 +120,46 @@ namespace Umbraco.Core.Runtime //sa.Scope?.Dispose(); } + /// + /// Gets a logger. + /// + protected virtual ILogger GetLogger() + { + return SerilogLogger.CreateWithDefaultConfiguration(); + } + + protected virtual void ConfigureUnhandledException(ILogger logger) + { + //take care of unhandled exceptions - there is nothing we can do to + // prevent the launch process to go down but at least we can try + // and log the exception + AppDomain.CurrentDomain.UnhandledException += (_, args) => + { + var exception = (Exception)args.ExceptionObject; + var isTerminating = args.IsTerminating; // always true? + + var msg = "Unhandled exception in AppDomain"; + if (isTerminating) msg += " (terminating)"; + msg += "."; + logger.Error(exception, msg); + }; + } + + protected virtual void ConfigureAssemblyResolve(ILogger logger) + { + // When an assembly can't be resolved. In here we can do magic with the assembly name and try loading another. + // This is used for loading a signed assembly of AutoMapper (v. 3.1+) without having to recompile old code. + AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => + { + // ensure the assembly is indeed AutoMapper and that the PublicKeyToken is null before trying to Load again + // do NOT just replace this with 'return Assembly', as it will cause an infinite loop -> stackoverflow + if (args.Name.StartsWith("AutoMapper") && args.Name.EndsWith("PublicKeyToken=null")) + return Assembly.Load(args.Name.Replace(", PublicKeyToken=null", ", PublicKeyToken=be96cd2c38ef1005")); + return null; + }; + } + + private void AquireMainDom(IServiceFactory container) { using (var timer = ProfilingLogger.DebugDuration("Acquiring MainDom.", "Aquired.")) diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj old mode 100644 new mode 100755 index 2bfa52a80a..cc82f977d1 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -602,9 +602,6 @@ - - - @@ -1455,7 +1452,6 @@ - diff --git a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs old mode 100644 new mode 100755 index 770dead600..533e2f1a55 --- a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs +++ b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs @@ -14,6 +14,7 @@ using Umbraco.Core.Runtime; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Examine; +using Umbraco.Web; namespace Umbraco.Tests.Runtimes { @@ -63,14 +64,6 @@ namespace Umbraco.Tests.Runtimes return new TestRuntime(this); } - // the application's logger is created by the application - // through GetLogger, that custom application can override - protected override ILogger GetLogger() - { - //return Mock.Of(); - return new DebugDiagnosticsLogger(); - } - // don't register anything against AppDomain protected override void ConfigureUnhandledException(ILogger logger) { } @@ -80,8 +73,18 @@ namespace Umbraco.Tests.Runtimes public class TestRuntime : CoreRuntime { public TestRuntime(UmbracoApplicationBase umbracoApplication) - : base(umbracoApplication) - { } + : base() + { + _umbracoApplication = umbracoApplication; + } + + // the application's logger is created by the application + // through GetLogger, that custom application can override + protected override ILogger GetLogger() + { + //return Mock.Of(); + return new DebugDiagnosticsLogger(); + } public override void Compose(ServiceContainer container) { @@ -115,6 +118,7 @@ namespace Umbraco.Tests.Runtimes } private MainDom _mainDom; + private readonly UmbracoApplicationBase _umbracoApplication; public override void Boot(ServiceContainer container) { diff --git a/src/Umbraco.Core/Logging/WebProfiler.cs b/src/Umbraco.Web/Logging/WebProfiler.cs old mode 100644 new mode 100755 similarity index 97% rename from src/Umbraco.Core/Logging/WebProfiler.cs rename to src/Umbraco.Web/Logging/WebProfiler.cs index 7284ea05bd..77b57e768d --- a/src/Umbraco.Core/Logging/WebProfiler.cs +++ b/src/Umbraco.Web/Logging/WebProfiler.cs @@ -3,8 +3,10 @@ using System.Threading; using System.Web; using StackExchange.Profiling; using StackExchange.Profiling.SqlFormatters; +using Umbraco.Core; +using Umbraco.Core.Logging; -namespace Umbraco.Core.Logging +namespace Umbraco.Web.Logging { /// /// Implements by using the MiniProfiler framework. diff --git a/src/Umbraco.Core/Logging/WebProfilerComponent.cs b/src/Umbraco.Web/Logging/WebProfilerComponent.cs old mode 100644 new mode 100755 similarity index 96% rename from src/Umbraco.Core/Logging/WebProfilerComponent.cs rename to src/Umbraco.Web/Logging/WebProfilerComponent.cs index 338a7e72d6..c6c7b9a545 --- a/src/Umbraco.Core/Logging/WebProfilerComponent.cs +++ b/src/Umbraco.Web/Logging/WebProfilerComponent.cs @@ -1,8 +1,10 @@ using System; using System.Web; +using Umbraco.Core; using Umbraco.Core.Components; +using Umbraco.Core.Logging; -namespace Umbraco.Core.Logging +namespace Umbraco.Web.Logging { internal class WebProfilerComponent : UmbracoComponentBase, IUmbracoCoreComponent { diff --git a/src/Umbraco.Core/Logging/WebProfilerProvider.cs b/src/Umbraco.Web/Logging/WebProfilerProvider.cs old mode 100644 new mode 100755 similarity index 99% rename from src/Umbraco.Core/Logging/WebProfilerProvider.cs rename to src/Umbraco.Web/Logging/WebProfilerProvider.cs index 35ecd002c8..7b12c2f5bd --- a/src/Umbraco.Core/Logging/WebProfilerProvider.cs +++ b/src/Umbraco.Web/Logging/WebProfilerProvider.cs @@ -3,7 +3,7 @@ using System.Threading; using System.Web; using StackExchange.Profiling; -namespace Umbraco.Core.Logging +namespace Umbraco.Web.Logging { /// /// This is a custom MiniProfiler WebRequestProfilerProvider (which is generally the default) that allows diff --git a/src/Umbraco.Web/Runtime/WebRuntime.cs b/src/Umbraco.Web/Runtime/WebRuntime.cs old mode 100644 new mode 100755 index 78610c01b8..da062367e9 --- a/src/Umbraco.Web/Runtime/WebRuntime.cs +++ b/src/Umbraco.Web/Runtime/WebRuntime.cs @@ -1,12 +1,12 @@ using System; using System.Web; using LightInject; -using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Runtime; +using Umbraco.Web.Logging; namespace Umbraco.Web.Runtime { @@ -16,6 +16,7 @@ namespace Umbraco.Web.Runtime /// On top of CoreRuntime, handles all of the web-related runtime aspects of Umbraco. public class WebRuntime : CoreRuntime { + private readonly UmbracoApplicationBase _umbracoApplication; private IProfiler _webProfiler; /// @@ -23,8 +24,10 @@ namespace Umbraco.Web.Runtime /// /// public WebRuntime(UmbracoApplicationBase umbracoApplication) - : base(umbracoApplication) - { } + : base() + { + _umbracoApplication = umbracoApplication; + } /// public override void Boot(ServiceContainer container) @@ -54,6 +57,10 @@ namespace Umbraco.Web.Runtime /// public override void Compose(ServiceContainer container) { + // some components may want to initialize with the UmbracoApplicationBase + // well, they should not - we should not do this + // TODO remove this eventually. + container.RegisterInstance(_umbracoApplication); base.Compose(container); // replace CoreRuntime's IProfiler registration diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj old mode 100644 new mode 100755 index 71a285e031..88131ad4e4 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -111,6 +111,9 @@ + + + @@ -181,6 +184,7 @@ + diff --git a/src/Umbraco.Core/UmbracoApplicationBase.cs b/src/Umbraco.Web/UmbracoApplicationBase.cs old mode 100644 new mode 100755 similarity index 75% rename from src/Umbraco.Core/UmbracoApplicationBase.cs rename to src/Umbraco.Web/UmbracoApplicationBase.cs index 1304244ece..080002b18e --- a/src/Umbraco.Core/UmbracoApplicationBase.cs +++ b/src/Umbraco.Web/UmbracoApplicationBase.cs @@ -4,11 +4,12 @@ using System.Threading; using System.Web; using System.Web.Hosting; using LightInject; +using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Logging; using Umbraco.Core.Logging.Serilog; -namespace Umbraco.Core +namespace Umbraco.Web { /// /// Provides an abstract base class for the Umbraco HttpApplication. @@ -22,14 +23,6 @@ namespace Umbraco.Core /// protected abstract IRuntime GetRuntime(); - /// - /// Gets a logger. - /// - protected virtual ILogger GetLogger() - { - return SerilogLogger.CreateWithDefaultConfiguration(); - } - // events - in the order they trigger // were part of the BootManager architecture, would trigger only for the initial @@ -61,54 +54,12 @@ namespace Umbraco.Core // create the container for the application, and configure. // the boot manager is responsible for registrations var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); // also sets Current.Container - - // register the essential stuff, - // ie the global application logger - // (profiler etc depend on boot manager) - var logger = GetLogger(); - container.RegisterInstance(logger); - // now it is ok to use Current.Logger - - ConfigureUnhandledException(logger); - ConfigureAssemblyResolve(logger); // get runtime & boot _runtime = GetRuntime(); _runtime.Boot(container); } - protected virtual void ConfigureUnhandledException(ILogger logger) - { - //take care of unhandled exceptions - there is nothing we can do to - // prevent the entire w3wp process to go down but at least we can try - // and log the exception - AppDomain.CurrentDomain.UnhandledException += (_, args) => - { - var exception = (Exception)args.ExceptionObject; - var isTerminating = args.IsTerminating; // always true? - - var msg = "Unhandled exception in AppDomain"; - if (isTerminating) msg += " (terminating)"; - msg += "."; - logger.Error(exception, msg); - }; - } - - protected virtual void ConfigureAssemblyResolve(ILogger logger) - { - // When an assembly can't be resolved. In here we can do magic with the assembly name and try loading another. - // This is used for loading a signed assembly of AutoMapper (v. 3.1+) without having to recompile old code. - AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => - { - // ensure the assembly is indeed AutoMapper and that the PublicKeyToken is null before trying to Load again - // do NOT just replace this with 'return Assembly', as it will cause an infinite loop -> stackoverflow - if (args.Name.StartsWith("AutoMapper") && args.Name.EndsWith("PublicKeyToken=null")) - return Assembly.Load(args.Name.Replace(", PublicKeyToken=null", ", PublicKeyToken=be96cd2c38ef1005")); - return null; - }; - } - // called by ASP.NET (auto event wireup) once per app domain // do NOT set instance data here - only static (see docs) // sender is System.Web.HttpApplicationFactory, evargs is EventArgs.Empty