From 8b7effd6ff1694688e93d03a48c5dcddb4efe4f0 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Tue, 18 Sep 2012 15:33:57 -0400 Subject: Moved discovery of loggers and weather providers to MEF. Also added support for third-party image processors, also discovered through MEF. --- MediaBrowser.Common/Kernel/BaseKernel.cs | 106 ++++++++++--------------------- 1 file changed, 33 insertions(+), 73 deletions(-) (limited to 'MediaBrowser.Common/Kernel/BaseKernel.cs') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 0eebc863c..5a0e1c5e5 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -10,7 +10,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; -using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -72,6 +71,12 @@ namespace MediaBrowser.Common.Kernel [ImportMany(typeof(BaseHandler))] private IEnumerable HttpHandlers { get; set; } + /// + /// Gets the list of currently registered Loggers + /// + [ImportMany(typeof(BaseLogger))] + public IEnumerable Loggers { get; set; } + /// /// Both the Ui and server will have a built-in HttpServer. /// People will inevitably want remote control apps so it's needed in the Ui too. @@ -83,6 +88,8 @@ namespace MediaBrowser.Common.Kernel /// private IDisposable HttpListener { get; set; } + private CompositionContainer CompositionContainer { get; set; } + protected virtual string HttpServerUrlPrefix { get @@ -101,13 +108,13 @@ namespace MediaBrowser.Common.Kernel /// public async Task Init(IProgress progress) { + Logger.Kernel = this; + // Performs initializations that only occur once InitializeInternal(progress); // Performs initializations that can be reloaded at anytime await Reload(progress).ConfigureAwait(false); - - ReportProgress(progress, "Loading Complete"); } /// @@ -117,8 +124,6 @@ namespace MediaBrowser.Common.Kernel { ApplicationPaths = new TApplicationPathsType(); - ReloadLogger(); - ReportProgress(progress, "Loading Configuration"); ReloadConfiguration(); @@ -136,6 +141,8 @@ namespace MediaBrowser.Common.Kernel await ReloadInternal(progress).ConfigureAwait(false); OnReloadCompleted(progress); + + ReportProgress(progress, "Kernel.Reload Complete"); } /// @@ -151,23 +158,6 @@ namespace MediaBrowser.Common.Kernel }).ConfigureAwait(false); } - /// - /// Disposes the current logger and creates a new one - /// - private void ReloadLogger() - { - DisposeLogger(); - - DateTime now = DateTime.Now; - - string logFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, "log-" + now.ToString("dMyyyy") + "-" + now.Ticks + ".log"); - - Trace.Listeners.Add(new TextWriterTraceListener(logFilePath)); - Trace.AutoFlush = true; - - Logger.LoggerInstance = new TraceLogger(); - } - /// /// Uses MEF to locate plugins /// Subclasses can use this to locate types within plugins @@ -176,14 +166,13 @@ namespace MediaBrowser.Common.Kernel { DisposeComposableParts(); - var container = GetCompositionContainer(includeCurrentAssembly: true); + CompositionContainer = GetCompositionContainer(includeCurrentAssembly: true); - container.ComposeParts(this); + CompositionContainer.ComposeParts(this); OnComposablePartsLoaded(); - container.Catalog.Dispose(); - container.Dispose(); + CompositionContainer.Catalog.Dispose(); } /// @@ -198,8 +187,7 @@ namespace MediaBrowser.Common.Kernel var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a))); // Include composable parts in the Common assembly - // Uncomment this if it's ever needed - //catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); + catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); if (includeCurrentAssembly) { @@ -215,8 +203,13 @@ namespace MediaBrowser.Common.Kernel /// protected virtual void OnComposablePartsLoaded() { + foreach (var logger in Loggers) + { + logger.Initialize(this); + } + // Start-up each plugin - foreach (BasePlugin plugin in Plugins) + foreach (var plugin in Plugins) { plugin.Initialize(this); } @@ -230,17 +223,16 @@ namespace MediaBrowser.Common.Kernel //Configuration information for anything other than server-specific configuration will have to come via the API... -ebr // Deserialize config - if (!File.Exists(ApplicationPaths.SystemConfigurationFilePath)) + // Use try/catch to avoid the extra file system lookup using File.Exists + try { - Configuration = new TConfigurationType(); - XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath); + Configuration = XmlSerializer.DeserializeFromFile(ApplicationPaths.SystemConfigurationFilePath); } - else + catch (FileNotFoundException) { - Configuration = XmlSerializer.DeserializeFromFile(ApplicationPaths.SystemConfigurationFilePath); + Configuration = new TConfigurationType(); + XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath); } - - Logger.LoggerInstance.LogSeverity = Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info; } /// @@ -275,11 +267,9 @@ namespace MediaBrowser.Common.Kernel { Logger.LogInfo("Beginning Kernel.Dispose"); - DisposeComposableParts(); - DisposeHttpServer(); - DisposeLogger(); + DisposeComposableParts(); } /// @@ -287,22 +277,9 @@ namespace MediaBrowser.Common.Kernel /// protected virtual void DisposeComposableParts() { - DisposePlugins(); - } - - /// - /// Disposes all plugins - /// - private void DisposePlugins() - { - if (Plugins != null) + if (CompositionContainer != null) { - Logger.LogInfo("Disposing Plugins"); - - foreach (BasePlugin plugin in Plugins) - { - plugin.Dispose(); - } + CompositionContainer.Dispose(); } } @@ -324,21 +301,6 @@ namespace MediaBrowser.Common.Kernel } } - /// - /// Disposes the current Logger instance - /// - private void DisposeLogger() - { - Trace.Listeners.Clear(); - - if (Logger.LoggerInstance != null) - { - Logger.LogInfo("Disposing Logger"); - - Logger.LoggerInstance.Dispose(); - } - } - /// /// Gets the current application version /// @@ -354,10 +316,7 @@ namespace MediaBrowser.Common.Kernel { progress.Report(new TaskProgress { Description = message }); - if (Logger.LoggerInstance != null) - { - Logger.LogInfo(message); - } + Logger.LogInfo(message); } BaseApplicationPaths IKernel.ApplicationPaths @@ -373,6 +332,7 @@ namespace MediaBrowser.Common.Kernel Task Init(IProgress progress); Task Reload(IProgress progress); + IEnumerable Loggers { get; } void Dispose(); } } -- cgit v1.2.3