jellyfin/MediaBrowser.Common.Implementations/Logging/NlogManager.cs

265 lines
7.5 KiB
C#
Raw Normal View History

2013-09-21 01:04:14 +00:00
using MediaBrowser.Model.Logging;
2013-02-26 17:21:18 +00:00
using NLog;
using NLog.Config;
using NLog.Targets;
2014-11-27 03:10:44 +00:00
using NLog.Targets.Wrappers;
2013-02-26 17:21:18 +00:00
using System;
using System.IO;
2013-09-21 01:04:14 +00:00
using System.Linq;
2013-02-26 20:07:06 +00:00
namespace MediaBrowser.Common.Implementations.Logging
{
/// <summary>
/// Class NlogManager
/// </summary>
2013-02-26 17:21:18 +00:00
public class NlogManager : ILogManager
{
2013-02-26 17:21:18 +00:00
/// <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; }
2014-12-17 22:39:17 +00:00
/// <summary>
/// Gets or sets the exception message prefix.
/// </summary>
/// <value>The exception message prefix.</value>
public string ExceptionMessagePrefix { get; set; }
2013-02-26 17:21:18 +00:00
/// <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;
2014-11-27 03:10:44 +00:00
LogManager.Configuration = new LoggingConfiguration ();
2013-02-26 17:21:18 +00:00
}
2013-09-21 01:04:14 +00:00
private LogSeverity _severity = LogSeverity.Debug;
public LogSeverity LogSeverity
{
get
{
return _severity;
}
set
{
var changed = _severity != value;
_severity = value;
if (changed)
{
UpdateLogLevel(value);
}
}
}
private void UpdateLogLevel(LogSeverity newLevel)
{
var level = GetLogLevel(newLevel);
var rules = LogManager.Configuration.LoggingRules;
foreach (var rule in rules)
{
if (!rule.IsLoggingEnabledForLevel(level))
{
rule.EnableLoggingForLevel(level);
}
2015-07-08 16:10:34 +00:00
foreach (var lev in rule.Levels.ToArray())
{
if (lev < level)
{
rule.DisableLoggingForLevel(lev);
}
}
2013-09-21 01:04:14 +00:00
}
}
/// <summary>
/// Adds the file target.
/// </summary>
/// <param name="path">The path.</param>
2013-02-26 17:21:18 +00:00
/// <param name="level">The level.</param>
private void AddFileTarget(string path, LogSeverity level)
{
2014-11-27 03:10:44 +00:00
RemoveTarget("ApplicationLogFileWrapper");
var wrapper = new AsyncTargetWrapper ();
wrapper.Name = "ApplicationLogFileWrapper";
var logFile = new FileTarget
{
FileName = path,
2015-10-28 19:40:38 +00:00
Layout = "${longdate} ${level} ${logger}: ${message}"
};
2013-03-04 16:31:33 +00:00
logFile.Name = "ApplicationLogFile";
2014-11-27 03:10:44 +00:00
wrapper.WrappedTarget = logFile;
AddLogTarget(wrapper, level);
}
/// <summary>
/// Adds the log target.
/// </summary>
/// <param name="target">The target.</param>
2013-02-26 17:21:18 +00:00
/// <param name="level">The level.</param>
2013-03-05 18:11:25 +00:00
public void AddLogTarget(Target target, LogSeverity level)
{
var config = LogManager.Configuration;
2013-03-04 16:31:33 +00:00
config.AddTarget(target.Name, target);
2013-02-26 17:21:18 +00:00
var rule = new LoggingRule("*", GetLogLevel(level), target);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
}
2013-02-26 17:21:18 +00:00
2013-03-04 16:31:33 +00:00
/// <summary>
/// Removes the target.
/// </summary>
/// <param name="name">The name.</param>
public void RemoveTarget(string name)
{
var config = LogManager.Configuration;
var target = config.FindTargetByName(name);
if (target != null)
{
foreach (var rule in config.LoggingRules.ToList())
{
var contains = rule.Targets.Contains(target);
rule.Targets.Remove(target);
if (contains)
{
config.LoggingRules.Remove(rule);
}
}
config.RemoveTarget(name);
LogManager.Configuration = config;
}
}
2013-02-26 17:21:18 +00:00
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>ILogger.</returns>
2015-09-12 02:18:09 +00:00
public Model.Logging.ILogger GetLogger(string name)
2013-02-26 17:21:18 +00:00
{
2014-12-17 22:39:17 +00:00
return new NLogger(name, this);
2013-02-26 17:21:18 +00:00
}
/// <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)
{
2014-04-09 02:12:17 +00:00
LogFilePath = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Round(DateTime.Now.Ticks / 10000000) + ".txt");
2013-02-26 17:21:18 +00:00
2015-09-13 23:07:54 +00:00
Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath));
2014-01-09 04:44:51 +00:00
2013-02-26 17:21:18 +00:00
AddFileTarget(LogFilePath, level);
2013-09-21 01:04:14 +00:00
LogSeverity = level;
2013-02-26 17:21:18 +00:00
if (LoggerLoaded != null)
{
try
2013-02-26 17:21:18 +00:00
{
LoggerLoaded(this, EventArgs.Empty);
}
catch (Exception ex)
{
GetLogger("Logger").ErrorException("Error in LoggerLoaded event", ex);
}
2013-02-26 17:21:18 +00:00
}
}
/// <summary>
/// Flushes this instance.
/// </summary>
public void Flush()
{
LogManager.Flush();
}
2014-01-09 04:44:51 +00:00
public void AddConsoleOutput()
{
2014-11-27 03:10:44 +00:00
RemoveTarget("ConsoleTargetWrapper");
var wrapper = new AsyncTargetWrapper ();
wrapper.Name = "ConsoleTargetWrapper";
2014-01-09 04:44:51 +00:00
var target = new ConsoleTarget()
{
Layout = "${level}, ${logger}, ${message}",
Error = false
};
target.Name = "ConsoleTarget";
2014-11-27 03:10:44 +00:00
wrapper.WrappedTarget = target;
AddLogTarget(wrapper, LogSeverity);
2014-01-09 04:44:51 +00:00
}
public void RemoveConsoleOutput()
{
2014-11-27 03:10:44 +00:00
RemoveTarget("ConsoleTargetWrapper");
2014-01-09 04:44:51 +00:00
}
}
}