fix log window setting

This commit is contained in:
LukePulverenti 2013-02-26 12:21:18 -05:00
parent cbb2f00da5
commit 0f1ec5b586
12 changed files with 254 additions and 127 deletions

View File

@ -26,20 +26,18 @@ namespace MediaBrowser.Api.WebSocket
/// <summary>
/// The _kernel
/// </summary>
private readonly IApplicationHost _appHost;
private readonly IKernel _kernel;
private readonly ILogManager _logManager;
/// <summary>
/// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="kernel">The kernel.</param>
public LogFileWebSocketListener(ILogger logger, IApplicationHost host, IKernel kernel)
/// <param name="logManager">The log manager.</param>
public LogFileWebSocketListener(ILogger logger, ILogManager logManager)
: base(logger)
{
_appHost = host;
_kernel = kernel;
kernel.LoggerLoaded += kernel_LoggerLoaded;
_logManager = logManager;
_logManager.LoggerLoaded += kernel_LoggerLoaded;
}
/// <summary>
@ -49,9 +47,9 @@ namespace MediaBrowser.Api.WebSocket
/// <returns>IEnumerable{System.String}.</returns>
protected override async Task<IEnumerable<string>> GetDataToSend(LogFileWebSocketState state)
{
if (!string.Equals(_appHost.LogFilePath, state.LastLogFilePath))
if (!string.Equals(_logManager.LogFilePath, state.LastLogFilePath))
{
state.LastLogFilePath = _appHost.LogFilePath;
state.LastLogFilePath = _logManager.LogFilePath;
state.StartLine = 0;
}
@ -70,7 +68,7 @@ namespace MediaBrowser.Api.WebSocket
{
if (dispose)
{
_kernel.LoggerLoaded -= kernel_LoggerLoaded;
_logManager.LoggerLoaded -= kernel_LoggerLoaded;
}
base.Dispose(dispose);
}

View File

@ -1,6 +1,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using SimpleInjector;
@ -18,6 +19,18 @@ namespace MediaBrowser.Common.Implementations
/// <value>The logger.</value>
public ILogger Logger { get; protected set; }
/// <summary>
/// Gets or sets the log manager.
/// </summary>
/// <value>The log manager.</value>
public ILogManager LogManager { get; protected set; }
/// <summary>
/// Gets the application paths.
/// </summary>
/// <value>The application paths.</value>
protected IApplicationPaths ApplicationPaths { get; private set; }
/// <summary>
/// The container
/// </summary>
@ -45,6 +58,12 @@ namespace MediaBrowser.Common.Implementations
/// </summary>
protected readonly List<IDisposable> DisposableParts = new List<IDisposable>();
/// <summary>
/// Gets a value indicating whether this instance is first run.
/// </summary>
/// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
public bool IsFirstRun { get; private set; }
/// <summary>
/// The _protobuf serializer initialized
/// </summary>
@ -82,6 +101,16 @@ namespace MediaBrowser.Common.Implementations
protected BaseApplicationHost()
{
FailedAssemblies = new List<string>();
ApplicationPaths = GetApplicationPaths();
LogManager = GetLogManager();
Logger = LogManager.GetLogger("App");
IsFirstRun = !File.Exists(ApplicationPaths.SystemConfigurationFilePath);
DiscoverTypes();
}
/// <summary>
@ -90,6 +119,18 @@ namespace MediaBrowser.Common.Implementations
/// <returns>IEnumerable{Assembly}.</returns>
protected abstract IEnumerable<Assembly> GetComposablePartAssemblies();
/// <summary>
/// Gets the log manager.
/// </summary>
/// <returns>ILogManager.</returns>
protected abstract ILogManager GetLogManager();
/// <summary>
/// Gets the application paths.
/// </summary>
/// <returns>IApplicationPaths.</returns>
protected abstract IApplicationPaths GetApplicationPaths();
/// <summary>
/// Discovers the types.
/// </summary>

View File

@ -14,25 +14,32 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
public class ReloadLoggerFileTask : IScheduledTask
{
/// <summary>
/// Gets or sets the kernel.
/// Gets or sets the log manager.
/// </summary>
/// <value>The kernel.</value>
private IKernel Kernel { get; set; }
/// <value>The log manager.</value>
private ILogManager LogManager { get; set; }
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
private ILogger Logger { get; set; }
/// <summary>
/// Gets or sets the kernel.
/// </summary>
/// <value>The kernel.</value>
private IKernel Kernel { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ReloadLoggerFileTask" /> class.
/// </summary>
/// <param name="kernel">The kernel.</param>
/// <param name="logManager">The logManager.</param>
/// <param name="logger">The logger.</param>
public ReloadLoggerFileTask(IKernel kernel, ILogger logger)
/// <param name="kernel">The kernel.</param>
public ReloadLoggerFileTask(ILogManager logManager, ILogger logger, IKernel kernel)
{
Kernel = kernel;
LogManager = logManager;
Logger = logger;
Kernel = kernel;
}
/// <summary>
@ -57,8 +64,8 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
cancellationToken.ThrowIfCancellationRequested();
progress.Report(0);
return Task.Run(() => Kernel.ReloadLogger());
return Task.Run(() => LogManager.ReloadLogger(Kernel.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info));
}
/// <summary>

View File

@ -42,20 +42,6 @@ namespace MediaBrowser.Common.Kernel
}
#endregion
#region LoggerLoaded Event
/// <summary>
/// Fires whenever the logger is loaded
/// </summary>
public event EventHandler LoggerLoaded;
/// <summary>
/// Called when [logger loaded].
/// </summary>
private void OnLoggerLoaded()
{
EventHelper.QueueEventIfNotNull(LoggerLoaded, this, EventArgs.Empty, Logger);
}
#endregion
#region ReloadBeginning Event
/// <summary>
/// Fires whenever the kernel begins reloading
@ -277,16 +263,6 @@ namespace MediaBrowser.Common.Kernel
return Task.FromResult<object>(null);
}
/// <summary>
/// Disposes and reloads all loggers
/// </summary>
public void ReloadLogger()
{
ApplicationHost.ReloadLogger();
OnLoggerLoaded();
}
/// <summary>
/// Composes the parts with ioc container.
/// </summary>

View File

@ -16,23 +16,12 @@ namespace MediaBrowser.Common.Kernel
/// </summary>
void Restart();
/// <summary>
/// Reloads the logger.
/// </summary>
void ReloadLogger();
/// <summary>
/// Gets the application version.
/// </summary>
/// <value>The application version.</value>
Version ApplicationVersion { get; }
/// <summary>
/// Gets the log file path.
/// </summary>
/// <value>The log file path.</value>
string LogFilePath { get; }
/// <summary>
/// Gets or sets a value indicating whether this instance can self update.
/// </summary>

View File

@ -53,11 +53,6 @@ namespace MediaBrowser.Common.Kernel
/// <returns>SystemInfo.</returns>
SystemInfo GetSystemInfo();
/// <summary>
/// Reloads the logger.
/// </summary>
void ReloadLogger();
/// <summary>
/// Called when [application updated].
/// </summary>
@ -105,11 +100,6 @@ namespace MediaBrowser.Common.Kernel
/// <value>The web socket listeners.</value>
IEnumerable<IWebSocketListener> WebSocketListeners { get; }
/// <summary>
/// Occurs when [logger loaded].
/// </summary>
event EventHandler LoggerLoaded;
/// <summary>
/// Occurs when [reload completed].
/// </summary>

View File

@ -531,18 +531,9 @@ namespace MediaBrowser.Controller
/// <param name="config">The config.</param>
public void UpdateConfiguration(ServerConfiguration config)
{
var oldConfiguration = Configuration;
var reloadLogger = config.ShowLogWindow != oldConfiguration.ShowLogWindow;
Configuration = config;
SaveConfiguration();
if (reloadLogger)
{
ReloadLogger();
}
// Validate currently executing providers, in the background
Task.Run(() =>
{

View File

@ -1,27 +1,62 @@
using NLog;
using MediaBrowser.Model.Logging;
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Logging.Nlog
{
/// <summary>
/// Class NlogManager
/// </summary>
public static class NlogManager
public class NlogManager : ILogManager
{
/// <summary>
/// Occurs when [logger loaded].
/// </summary>
public event EventHandler LoggerLoaded;
/// <summary>
/// Gets or sets the log directory.
/// </summary>
/// <value>The log directory.</value>
private string LogDirectory { get; set; }
/// <summary>
/// Gets or sets the log file prefix.
/// </summary>
/// <value>The log file prefix.</value>
private string LogFilePrefix { get; set; }
/// <summary>
/// Gets the log file path.
/// </summary>
/// <value>The log file path.</value>
public string LogFilePath { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="NlogManager" /> class.
/// </summary>
/// <param name="logDirectory">The log directory.</param>
/// <param name="logFileNamePrefix">The log file name prefix.</param>
public NlogManager(string logDirectory, string logFileNamePrefix)
{
LogDirectory = logDirectory;
LogFilePrefix = logFileNamePrefix;
}
/// <summary>
/// Adds the file target.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="enableDebugLogging">if set to <c>true</c> [enable debug logging].</param>
public static void AddFileTarget(string path, bool enableDebugLogging)
/// <param name="level">The level.</param>
private void AddFileTarget(string path, LogSeverity level)
{
var logFile = new FileTarget();
logFile.FileName = path;
logFile.Layout = "${longdate}, ${level}, ${logger}, ${message}";
AddLogTarget(logFile, "ApplicationLogFile", enableDebugLogging);
AddLogTarget(logFile, "ApplicationLogFile", level);
}
/// <summary>
@ -29,8 +64,8 @@ namespace MediaBrowser.Logging.Nlog
/// </summary>
/// <param name="target">The target.</param>
/// <param name="name">The name.</param>
/// <param name="enableDebugLogging">if set to <c>true</c> [enable debug logging].</param>
private static void AddLogTarget(Target target, string name, bool enableDebugLogging)
/// <param name="level">The level.</param>
private void AddLogTarget(Target target, string name, LogSeverity level)
{
var config = LogManager.Configuration;
@ -39,12 +74,71 @@ namespace MediaBrowser.Logging.Nlog
target.Name = name;
config.AddTarget(name, target);
var level = enableDebugLogging ? LogLevel.Debug : LogLevel.Info;
var rule = new LoggingRule("*", level, target);
var rule = new LoggingRule("*", GetLogLevel(level), target);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
}
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>ILogger.</returns>
public ILogger GetLogger(string name)
{
return new NLogger(name);
}
/// <summary>
/// Gets the log level.
/// </summary>
/// <param name="severity">The severity.</param>
/// <returns>LogLevel.</returns>
/// <exception cref="System.ArgumentException">Unrecognized LogSeverity</exception>
private LogLevel GetLogLevel(LogSeverity severity)
{
switch (severity)
{
case LogSeverity.Debug:
return LogLevel.Debug;
case LogSeverity.Error:
return LogLevel.Error;
case LogSeverity.Fatal:
return LogLevel.Fatal;
case LogSeverity.Info:
return LogLevel.Info;
case LogSeverity.Warn:
return LogLevel.Warn;
default:
throw new ArgumentException("Unrecognized LogSeverity");
}
}
/// <summary>
/// Reloads the logger.
/// </summary>
/// <param name="level">The level.</param>
public void ReloadLogger(LogSeverity level)
{
LogFilePath = Path.Combine(LogDirectory, LogFilePrefix + "-" + DateTime.Now.Ticks + ".log");
AddFileTarget(LogFilePath, level);
if (LoggerLoaded != null)
{
Task.Run(() =>
{
try
{
LoggerLoaded(this, EventArgs.Empty);
}
catch (Exception ex)
{
GetLogger("Logger").ErrorException("Error in LoggerLoaded event", ex);
}
});
}
}
}
}

View File

@ -0,0 +1,33 @@
using System;
namespace MediaBrowser.Model.Logging
{
/// <summary>
/// Interface ILogManager
/// </summary>
public interface ILogManager
{
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>ILogger.</returns>
ILogger GetLogger(string name);
/// <summary>
/// Reloads the logger.
/// </summary>
void ReloadLogger(LogSeverity severity);
/// <summary>
/// Gets the log file path.
/// </summary>
/// <value>The log file path.</value>
string LogFilePath { get; }
/// <summary>
/// Occurs when [logger loaded].
/// </summary>
event EventHandler LoggerLoaded;
}
}

View File

@ -48,6 +48,7 @@
<Compile Include="Entities\BaseItemInfo.cs" />
<Compile Include="Connectivity\ClientConnectionInfo.cs" />
<Compile Include="Connectivity\ClientType.cs" />
<Compile Include="Logging\ILogManager.cs" />
<Compile Include="MediaInfo\BlurayDiscInfo.cs" />
<Compile Include="Entities\ChapterInfo.cs" />
<Compile Include="Entities\LocationType.cs" />

View File

@ -12,6 +12,7 @@ using MediaBrowser.Controller;
using MediaBrowser.IsoMounter;
using MediaBrowser.Logging.Nlog;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
@ -39,12 +40,6 @@ namespace MediaBrowser.ServerApplication
/// </summary>
public class ApplicationHost : BaseApplicationHost, IApplicationHost
{
/// <summary>
/// Gets or sets the log file path.
/// </summary>
/// <value>The log file path.</value>
public string LogFilePath { get; private set; }
/// <summary>
/// Gets or sets the kernel.
/// </summary>
@ -62,15 +57,13 @@ namespace MediaBrowser.ServerApplication
private readonly IXmlSerializer _xmlSerializer = new XmlSerializer();
/// <summary>
/// The _application paths
/// Gets the server application paths.
/// </summary>
private readonly IServerApplicationPaths _applicationPaths = new ServerApplicationPaths();
/// <summary>
/// Gets a value indicating whether this instance is first run.
/// </summary>
/// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
public bool IsFirstRun { get; private set; }
/// <value>The server application paths.</value>
protected IServerApplicationPaths ServerApplicationPaths
{
get { return (IServerApplicationPaths) ApplicationPaths; }
}
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationHost" /> class.
@ -79,21 +72,15 @@ namespace MediaBrowser.ServerApplication
public ApplicationHost()
: base()
{
IsFirstRun = !File.Exists(_applicationPaths.SystemConfigurationFilePath);
Logger = new NLogger("App");
DiscoverTypes();
Kernel = new Kernel(this, _applicationPaths, _xmlSerializer, Logger);
Kernel = new Kernel(this, ServerApplicationPaths, _xmlSerializer, Logger);
var networkManager = new NetworkManager();
var serverManager = new ServerManager(this, Kernel, networkManager, _jsonSerializer, Logger);
var taskManager = new TaskManager(_applicationPaths, _jsonSerializer, Logger, serverManager);
var taskManager = new TaskManager(ApplicationPaths, _jsonSerializer, Logger, serverManager);
ReloadLogger();
LogManager.ReloadLogger(Kernel.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
Logger.Info("Version {0} initializing", ApplicationVersion);
@ -104,6 +91,24 @@ namespace MediaBrowser.ServerApplication
FindParts(taskManager, httpServer);
}
/// <summary>
/// Gets the application paths.
/// </summary>
/// <returns>IApplicationPaths.</returns>
protected override IApplicationPaths GetApplicationPaths()
{
return new ServerApplicationPaths();
}
/// <summary>
/// Gets the log manager.
/// </summary>
/// <returns>ILogManager.</returns>
protected override ILogManager GetLogManager()
{
return new NlogManager(ApplicationPaths.LogDirectoryPath, "Server");
}
/// <summary>
/// Registers resources that classes will depend on
/// </summary>
@ -113,14 +118,15 @@ namespace MediaBrowser.ServerApplication
RegisterSingleInstance(Kernel);
RegisterSingleInstance<IApplicationHost>(this);
RegisterSingleInstance(LogManager);
RegisterSingleInstance(Logger);
RegisterSingleInstance(_applicationPaths);
RegisterSingleInstance<IApplicationPaths>(_applicationPaths);
RegisterSingleInstance(ApplicationPaths);
RegisterSingleInstance(ServerApplicationPaths);
RegisterSingleInstance(taskManager);
RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
RegisterSingleInstance<IHttpClient>(new HttpManager(_applicationPaths, Logger));
RegisterSingleInstance<IHttpClient>(new HttpManager(ApplicationPaths, Logger));
RegisterSingleInstance<IZipClient>(new DotNetZipClient());
RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
RegisterSingleInstance(_jsonSerializer);
@ -147,23 +153,11 @@ namespace MediaBrowser.ServerApplication
/// <summary>
/// Restarts this instance.
/// </summary>
/// <exception cref="System.NotImplementedException"></exception>
public void Restart()
{
App.Instance.Restart();
}
/// <summary>
/// Reloads the logger.
/// </summary>
/// <exception cref="System.NotImplementedException"></exception>
public void ReloadLogger()
{
LogFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, "Server-" + DateTime.Now.Ticks + ".log");
NlogManager.AddFileTarget(LogFilePath, Kernel.Configuration.EnableDebugLevelLogging);
}
/// <summary>
/// Gets or sets a value indicating whether this instance can self update.
/// </summary>
@ -204,7 +198,7 @@ namespace MediaBrowser.ServerApplication
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
foreach (var pluginAssembly in Directory
.EnumerateFiles(_applicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
.Select(LoadAssembly).Where(a => a != null))
{
yield return pluginAssembly;

View File

@ -55,6 +55,11 @@ namespace MediaBrowser.ServerApplication
/// </summary>
private readonly IApplicationHost _appHost;
/// <summary>
/// The _log manager
/// </summary>
private readonly ILogManager _logManager;
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow" /> class.
/// </summary>
@ -62,20 +67,21 @@ namespace MediaBrowser.ServerApplication
/// <param name="logger">The logger.</param>
/// <param name="appHost">The app host.</param>
/// <exception cref="System.ArgumentNullException">logger</exception>
public MainWindow(IJsonSerializer jsonSerializer, ILogger logger, IApplicationHost appHost)
public MainWindow(IJsonSerializer jsonSerializer, ILogManager logManager, IApplicationHost appHost)
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (logger == null)
if (logManager == null)
{
throw new ArgumentNullException("logger");
throw new ArgumentNullException("logManager");
}
_jsonSerializer = jsonSerializer;
_logger = logger;
_logger = logManager.GetLogger("MainWindow");
_appHost = appHost;
_logManager = logManager;
InitializeComponent();
@ -94,7 +100,7 @@ namespace MediaBrowser.ServerApplication
Instance_ConfigurationUpdated(null, EventArgs.Empty);
Kernel.Instance.ReloadCompleted += KernelReloadCompleted;
Kernel.Instance.LoggerLoaded += LoadLogWindow;
_logManager.LoggerLoaded += LoadLogWindow;
Kernel.Instance.HasPendingRestartChanged += Instance_HasPendingRestartChanged;
Kernel.Instance.ConfigurationUpdated += Instance_ConfigurationUpdated;
}
@ -115,6 +121,13 @@ namespace MediaBrowser.ServerApplication
separatorDeveloperTools.Visibility = developerToolsVisibility;
cmdReloadServer.Visibility = developerToolsVisibility;
cmOpenExplorer.Visibility = developerToolsVisibility;
var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
if ((logWindow == null && Kernel.Instance.Configuration.ShowLogWindow) || (logWindow != null && !Kernel.Instance.Configuration.ShowLogWindow))
{
_logManager.ReloadLogger(Kernel.Instance.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
}
});
}