diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 58e55b91ae..885cb4bb78 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -36,7 +36,7 @@ namespace Umbraco.Core /// public class CoreBootManager : IBootManager { - internal ServiceContainer Container { get; private set; } + private ServiceContainer _appStartupEvtContainer; protected ProfilingLogger ProfilingLogger { get; private set; } private DisposableTimer _timer; @@ -55,13 +55,17 @@ namespace Umbraco.Core get { return _umbracoApplication; } } + internal ServiceContainer Container + { + get { return _umbracoApplication.Container; } + } + protected IServiceProvider ServiceProvider { get; private set; } public CoreBootManager(UmbracoApplicationBase umbracoApplication) { if (umbracoApplication == null) throw new ArgumentNullException("umbracoApplication"); - _umbracoApplication = umbracoApplication; - Container = new ServiceContainer(); + _umbracoApplication = umbracoApplication; } public virtual IBootManager Initialize() @@ -70,11 +74,10 @@ namespace Umbraco.Core throw new InvalidOperationException("The boot manager has already been initialized"); //Create logger/profiler, and their resolvers, these are special resolvers that can be resolved before frozen so we can start logging - var logger = CreateLogger(); - LoggerResolver.Current = new LoggerResolver(logger) {CanResolveBeforeFrozen = true}; + LoggerResolver.Current = new LoggerResolver(_umbracoApplication.Logger) { CanResolveBeforeFrozen = true }; var profiler = CreateProfiler(); ProfilerResolver.Current = new ProfilerResolver(profiler) {CanResolveBeforeFrozen = true}; - ProfilingLogger = new ProfilingLogger(logger, profiler); + ProfilingLogger = new ProfilingLogger(_umbracoApplication.Logger, profiler); _timer = ProfilingLogger.DebugDuration("Umbraco application starting", "Umbraco application startup complete"); @@ -85,8 +88,8 @@ namespace Umbraco.Core ServiceProvider = new ActivatorServiceProvider(); PluginManager.Current = PluginManager = new PluginManager(ServiceProvider, _cacheHelper.RuntimeCache, ProfilingLogger, true); - //build up IoC - Container = BuildContainer(); + //build up core IoC servoces + ConfigureCoreServices(Container); //set the singleton resolved from the core container ApplicationContext.Current = ApplicationContext = Container.GetInstance(); @@ -103,8 +106,10 @@ namespace Umbraco.Core _appStartupEvtContainer = Container.CreateChildContainer(); _appStartupEvtContainer.BeginScope(); _appStartupEvtContainer.RegisterCollection(PluginManager.ResolveApplicationStartupHandlers()); - + + //build up standard IoC services ConfigureServices(Container); + InitializeResolvers(); InitializeModelMappers(); @@ -119,15 +124,14 @@ namespace Umbraco.Core /// /// Build the core container which contains all core things requird to build an app context /// - private ServiceContainer BuildContainer() + private void ConfigureCoreServices(ServiceContainer container) { - var container = new ServiceContainer(); + container.Register(factory => _umbracoApplication.Logger, new PerContainerLifetime()); + container.Register(factory => ProfilingLogger.Profiler, new PerContainerLifetime()); + container.Register(factory => ProfilingLogger, new PerContainerLifetime()); container.Register(factory => UmbracoConfig.For.UmbracoSettings()); container.Register(factory => _cacheHelper, new PerContainerLifetime()); container.Register(factory => _cacheHelper.RuntimeCache, new PerContainerLifetime()); - container.Register(factory => ProfilingLogger.Logger, new PerContainerLifetime()); - container.Register(factory => ProfilingLogger.Profiler, new PerContainerLifetime()); - container.Register(factory => ProfilingLogger, new PerContainerLifetime()); container.Register(); container.Register(factory => PluginManager, new PerContainerLifetime()); container.Register(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance())); @@ -145,8 +149,6 @@ namespace Umbraco.Core factory.GetInstance(), factory.GetInstance())); container.Register(new PerContainerLifetime()); - - return container; } /// @@ -208,14 +210,6 @@ namespace Umbraco.Core }); } - /// - /// Creates the application's ILogger - /// - protected virtual ILogger CreateLogger() - { - return Logger.CreateWithDefaultLog4NetConfiguration(); - } - /// /// Creates the application's IProfiler /// diff --git a/src/Umbraco.Core/UmbracoApplicationBase.cs b/src/Umbraco.Core/UmbracoApplicationBase.cs index d997c5e83a..d4c186b62a 100644 --- a/src/Umbraco.Core/UmbracoApplicationBase.cs +++ b/src/Umbraco.Core/UmbracoApplicationBase.cs @@ -5,8 +5,10 @@ using System.Web.Hosting; using System.Web.Mvc; using StackExchange.Profiling; using Umbraco.Core.Configuration; +using Umbraco.Core.LightInject; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; +using Umbraco.Core.Logging; namespace Umbraco.Core { @@ -21,6 +23,22 @@ namespace Umbraco.Core public abstract class UmbracoApplicationBase : System.Web.HttpApplication { + /// + /// Umbraco application's IoC container + /// + internal ServiceContainer Container { get; private set; } + + private ILogger _logger; + + /// + /// Constructor + /// + protected UmbracoApplicationBase() + { + //create the container for the application, the boot managers are responsible for registrations + Container = new ServiceContainer(); + } + public event EventHandler ApplicationStarting; public event EventHandler ApplicationStarted; @@ -40,17 +58,11 @@ namespace Umbraco.Core /// internal void StartApplication(object sender, EventArgs e) { - //don't output the MVC version header (security) - MvcHandler.DisableMvcResponseHeader = true; - //boot up the application GetBootManager() .Initialize() .Startup(appContext => OnApplicationStarting(sender, e)) .Complete(appContext => OnApplicationStarted(sender, e)); - - //And now we can dispose of our startup handlers - save some memory - //ApplicationEventsResolver.Current.Dispose(); } /// @@ -112,7 +124,7 @@ namespace Umbraco.Core /// protected virtual void OnApplicationError(object sender, EventArgs e) { - EventHandler handler = ApplicationError; + var handler = ApplicationError; if (handler != null) handler(this, EventArgs.Empty); } @@ -141,7 +153,7 @@ namespace Umbraco.Core /// protected virtual void OnApplicationEnd(object sender, EventArgs e) { - EventHandler handler = ApplicationEnd; + var handler = ApplicationEnd; if (handler != null) handler(this, EventArgs.Empty); } @@ -156,61 +168,13 @@ namespace Umbraco.Core protected abstract IBootManager GetBootManager(); - protected ILogger Logger + /// + /// Returns the logger instance for the application - this will be used throughout the entire app + /// + public virtual ILogger Logger { - get - { - if (LoggerResolver.HasCurrent && LoggerResolver.Current.HasValue) - { - return LoggerResolver.Current.Logger; - } - return new HttpTraceLogger(); - } + get { return _logger ?? (_logger = Logging.Logger.CreateWithDefaultLog4NetConfiguration()); } } - private class HttpTraceLogger : ILogger - { - public void Error(Type callingType, string message, Exception exception) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Warn(callingType.ToString(), message + Environment.NewLine + exception); - } - - public void Warn(Type callingType, string message, params Func[] formatItems) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Warn(callingType.ToString(), string.Format(message, formatItems.Select(x => x()))); - } - - public void WarnWithException(Type callingType, string message, Exception e, params Func[] formatItems) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Warn(callingType.ToString(), string.Format(message + Environment.NewLine + e, formatItems.Select(x => x()))); - } - - public void Info(Type callingType, Func generateMessage) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Write(callingType.ToString(), generateMessage()); - } - - public void Info(Type type, string generateMessageFormat, params Func[] formatItems) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Write(type.ToString(), string.Format(generateMessageFormat, formatItems.Select(x => x()))); - } - - public void Debug(Type callingType, Func generateMessage) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Write(callingType.ToString(), generateMessage()); - } - - public void Debug(Type type, string generateMessageFormat, params Func[] formatItems) - { - if (HttpContext.Current == null) return; - HttpContext.Current.Trace.Write(type.ToString(), string.Format(generateMessageFormat, formatItems.Select(x => x()))); - } - } } } diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 55cfcfc3df..c7286c439d 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -413,6 +413,9 @@ namespace Umbraco.Web /// private void SetupMvcAndWebApi() { + //don't output the MVC version header (security) + MvcHandler.DisableMvcResponseHeader = true; + //set master controller factory ControllerBuilder.Current.SetControllerFactory( new MasterControllerFactory(FilteredControllerFactoriesResolver.Current));