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

265 lines
7.5 KiB
C#
Raw Normal View History

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