From 84af2be14a7c10026a255d542f2b3174ad17bc1b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 24 Jul 2013 08:08:31 -0700 Subject: [PATCH] Log level halfway there --- NzbDrone.Common/PathExtensions.cs | 7 ++- .../Configuration/ConfigFileProvider.cs | 14 ++++- NzbDrone.Core/Configuration/ConfigService.cs | 1 + .../Events/ConfigFileSavedEvent.cs | 12 +++++ NzbDrone.Core/Configuration/IConfigService.cs | 1 + .../Instrumentation/SetLoggingLevel.cs | 53 +++++++++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 2 + 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 NzbDrone.Core/Configuration/Events/ConfigFileSavedEvent.cs create mode 100644 NzbDrone.Core/Instrumentation/SetLoggingLevel.cs diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index bd5ae4805..dfe2149dc 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Common private const string NZBDRONE_DB = "nzbdrone.db"; private const string NZBDRONE_LOG_DB = "logs.db"; private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; + private const string NLOG_CONFIG_FILE = "nlog.config"; private static readonly string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update" + Path.DirectorySeparatorChar; private static readonly string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone" + Path.DirectorySeparatorChar; @@ -70,7 +71,6 @@ namespace NzbDrone.Common return Path.Combine(GetProperCapitalization(dirInfo), dirInfo.GetFiles(fileInfo.Name)[0].Name); } - public static string GetAppDataPath(this IAppFolderInfo appFolderInfo) { return appFolderInfo.AppDataFolder; @@ -135,5 +135,10 @@ namespace NzbDrone.Common { return Path.Combine(GetAppDataPath(appFolderInfo), NZBDRONE_LOG_DB); } + + public static string GetNlogConfigPath(this IAppFolderInfo appFolderInfo) + { + return Path.Combine(appFolderInfo.StartUpFolder, NLOG_CONFIG_FILE); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 89c427fb2..7e37a2ea4 100644 --- a/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -6,6 +6,8 @@ using System.Xml.Linq; using NzbDrone.Common; using NzbDrone.Common.Cache; using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Configuration.Events; namespace NzbDrone.Core.Configuration { @@ -19,19 +21,22 @@ namespace NzbDrone.Core.Configuration bool AuthenticationEnabled { get; } string Username { get; } string Password { get; } + string LogLevel { get; } } public class ConfigFileProvider : IConfigFileProvider { private readonly IAppFolderInfo _appFolderInfo; + private readonly IMessageAggregator _messageAggregator; private readonly ICached _cache; private readonly string _configFile; - public ConfigFileProvider(IAppFolderInfo appFolderInfo, ICacheManger cacheManger) + public ConfigFileProvider(IAppFolderInfo appFolderInfo, ICacheManger cacheManger, IMessageAggregator messageAggregator) { _appFolderInfo = appFolderInfo; _cache = cacheManger.GetCache(GetType()); + _messageAggregator = messageAggregator; _configFile = _appFolderInfo.GetConfigPath(); } @@ -72,7 +77,7 @@ namespace NzbDrone.Core.Configuration } } - + _messageAggregator.PublishEvent(new ConfigFileSavedEvent()); } public int Port @@ -100,6 +105,11 @@ namespace NzbDrone.Core.Configuration get { return GetValue("Password", ""); } } + public string LogLevel + { + get { return GetValue("LogLevel", "Info"); } + } + public int GetValueInt(string key, int defaultValue) { return Convert.ToInt32(GetValue(key, defaultValue)); diff --git a/NzbDrone.Core/Configuration/ConfigService.cs b/NzbDrone.Core/Configuration/ConfigService.cs index 456954676..dade25f74 100644 --- a/NzbDrone.Core/Configuration/ConfigService.cs +++ b/NzbDrone.Core/Configuration/ConfigService.cs @@ -5,6 +5,7 @@ using NLog; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients.Nzbget; using NzbDrone.Core.Download.Clients.Sabnzbd; +using NzbDrone.Core.Instrumentation; namespace NzbDrone.Core.Configuration { diff --git a/NzbDrone.Core/Configuration/Events/ConfigFileSavedEvent.cs b/NzbDrone.Core/Configuration/Events/ConfigFileSavedEvent.cs new file mode 100644 index 000000000..97db618ee --- /dev/null +++ b/NzbDrone.Core/Configuration/Events/ConfigFileSavedEvent.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.Configuration.Events +{ + public class ConfigFileSavedEvent : IEvent + { + } +} diff --git a/NzbDrone.Core/Configuration/IConfigService.cs b/NzbDrone.Core/Configuration/IConfigService.cs index 430d15fde..a16470791 100644 --- a/NzbDrone.Core/Configuration/IConfigService.cs +++ b/NzbDrone.Core/Configuration/IConfigService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients.Nzbget; using NzbDrone.Core.Download.Clients.Sabnzbd; +using NzbDrone.Core.Instrumentation; namespace NzbDrone.Core.Configuration { diff --git a/NzbDrone.Core/Instrumentation/SetLoggingLevel.cs b/NzbDrone.Core/Instrumentation/SetLoggingLevel.cs new file mode 100644 index 000000000..6067146b6 --- /dev/null +++ b/NzbDrone.Core/Instrumentation/SetLoggingLevel.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml.Linq; +using NLog; +using NLog.Config; +using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Configuration.Events; +using NzbDrone.Core.Lifecycle; + +namespace NzbDrone.Core.Instrumentation +{ + public interface ISetLoggingLevel + { + void Reconfigure(); + } + + public class SetLoggingLevel : ISetLoggingLevel, IHandleAsync, IHandleAsync + { + private readonly IConfigFileProvider _configFileProvider; + + public SetLoggingLevel(IConfigFileProvider configFileProvider) + { + _configFileProvider = configFileProvider; + } + + public void Reconfigure() + { + var logLevel = _configFileProvider.LogLevel; + + var rules = LogManager.Configuration.LoggingRules; + var rollingFileLogger = rules.Single(s => s.Targets.Any(t => t.Name == "rollingFileLogger")); + rollingFileLogger.EnableLoggingForLevel(LogLevel.Trace); + + var test = 1; + } + + public void HandleAsync(ConfigFileSavedEvent message) + { + Reconfigure(); + } + + public void HandleAsync(ApplicationStartedEvent message) + { + Reconfigure(); + } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 9deaeec44..1c5d48159 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -180,6 +180,7 @@ + @@ -268,6 +269,7 @@ +