From 77669562743d725273ecd52677a6521aa0db7949 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 30 Jul 2012 09:44:28 -0400 Subject: Extracted Logging into a separate, portable class library --- MediaBrowser.Common/Kernel/BaseKernel.cs | 69 ++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 13 deletions(-) (limited to 'MediaBrowser.Common/Kernel/BaseKernel.cs') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 09bb67218..f997aea5d 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -6,12 +6,12 @@ using System.Configuration; using System.IO; using System.Linq; using System.Reflection; -using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Configuration; using MediaBrowser.Common.Json; -using MediaBrowser.Common.Logging; using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; -using MediaBrowser.Common.Progress; +using MediaBrowser.Model.Progress; +using MediaBrowser.Logging; namespace MediaBrowser.Common.Kernel { @@ -48,6 +48,22 @@ namespace MediaBrowser.Common.Kernel } } + /// + /// Gets the path to the log directory + /// + private string LogDirectoryPath + { + get + { + return Path.Combine(ProgramDataPath, "logs"); + } + } + + /// + /// Gets or sets the path to the current log file + /// + private string LogFilePath { get; set; } + /// /// Gets the current configuration /// @@ -73,12 +89,12 @@ namespace MediaBrowser.Common.Kernel public BaseKernel() { ProgramDataPath = GetProgramDataPath(); - - Logger.LoggerInstance = new FileLogger(Path.Combine(ProgramDataPath, "Logs")); } public virtual void Init(IProgress progress) { + ReloadLogger(); + ReloadConfiguration(); ReloadHttpServer(); @@ -86,6 +102,24 @@ namespace MediaBrowser.Common.Kernel ReloadComposableParts(); } + private void ReloadLogger() + { + DisposeLogger(); + + if (!Directory.Exists(LogDirectoryPath)) + { + Directory.CreateDirectory(LogDirectoryPath); + } + + DateTime now = DateTime.Now; + + LogFilePath = Path.Combine(LogDirectoryPath, now.ToString("dMyyyy") + "-" + now.Ticks + ".log"); + + FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read); + + Logger.LoggerInstance = new StreamLogger(fs); + } + /// /// Uses MEF to locate plugins /// Subclasses can use this to locate types within plugins @@ -213,14 +247,6 @@ namespace MediaBrowser.Common.Kernel HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/"); } - private void DisposeHttpServer() - { - if (HttpServer != null) - { - HttpServer.Dispose(); - } - } - /// /// This snippet will allow any plugin to reference another /// @@ -244,6 +270,23 @@ namespace MediaBrowser.Common.Kernel public void Dispose() { DisposeHttpServer(); + DisposeLogger(); + } + + private void DisposeHttpServer() + { + if (HttpServer != null) + { + HttpServer.Dispose(); + } + } + + private void DisposeLogger() + { + if (Logger.LoggerInstance != null) + { + Logger.LoggerInstance.Dispose(); + } } } } -- cgit v1.2.3