2013-08-04 17:27:06 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-07-24 15:08:31 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
2013-08-31 02:32:30 +00:00
|
|
|
|
using NLog.Targets;
|
2013-07-24 15:08:31 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NzbDrone.Core.Configuration.Events;
|
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-07-24 15:08:31 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Instrumentation
|
|
|
|
|
{
|
|
|
|
|
public interface ISetLoggingLevel
|
|
|
|
|
{
|
|
|
|
|
void Reconfigure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SetLoggingLevel : ISetLoggingLevel, IHandleAsync<ConfigFileSavedEvent>, IHandleAsync<ApplicationStartedEvent>
|
|
|
|
|
{
|
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
|
|
|
|
|
|
public SetLoggingLevel(IConfigFileProvider configFileProvider)
|
|
|
|
|
{
|
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reconfigure()
|
|
|
|
|
{
|
2013-07-25 01:03:20 +00:00
|
|
|
|
var minimumLogLevel = LogLevel.FromString(_configFileProvider.LogLevel);
|
2013-07-24 15:08:31 +00:00
|
|
|
|
|
|
|
|
|
var rules = LogManager.Configuration.LoggingRules;
|
2013-08-31 02:32:30 +00:00
|
|
|
|
var rollingFileLogger = rules.Single(s => s.Targets.Any(t => t is FileTarget));
|
2013-07-24 15:08:31 +00:00
|
|
|
|
rollingFileLogger.EnableLoggingForLevel(LogLevel.Trace);
|
|
|
|
|
|
2013-07-25 01:03:20 +00:00
|
|
|
|
SetMinimumLogLevel(rollingFileLogger, minimumLogLevel);
|
2013-08-31 02:32:30 +00:00
|
|
|
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
2013-07-25 01:03:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetMinimumLogLevel(LoggingRule rule, LogLevel minimumLogLevel)
|
|
|
|
|
{
|
|
|
|
|
foreach (var logLevel in GetLogLevels())
|
|
|
|
|
{
|
|
|
|
|
if (logLevel < minimumLogLevel)
|
|
|
|
|
{
|
|
|
|
|
rule.DisableLoggingForLevel(logLevel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rule.EnableLoggingForLevel(logLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<LogLevel> GetLogLevels()
|
|
|
|
|
{
|
|
|
|
|
return new List<LogLevel>
|
|
|
|
|
{
|
|
|
|
|
LogLevel.Trace,
|
|
|
|
|
LogLevel.Debug,
|
|
|
|
|
LogLevel.Info,
|
|
|
|
|
LogLevel.Warn,
|
|
|
|
|
LogLevel.Error,
|
|
|
|
|
LogLevel.Fatal
|
|
|
|
|
};
|
2013-07-24 15:08:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ConfigFileSavedEvent message)
|
|
|
|
|
{
|
|
|
|
|
Reconfigure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
|
|
|
|
{
|
|
|
|
|
Reconfigure();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|