2011-10-24 08:54:09 +03:00
|
|
|
using System;
|
2011-12-19 22:07:39 +03:00
|
|
|
using System.IO;
|
2011-10-24 08:54:09 +03:00
|
|
|
using NLog;
|
|
|
|
using NLog.Config;
|
|
|
|
using NLog.Targets;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public static class LogConfiguration
|
|
|
|
{
|
|
|
|
static LogConfiguration()
|
|
|
|
{
|
|
|
|
if (EnviromentProvider.IsProduction)
|
|
|
|
{
|
|
|
|
LogManager.ThrowExceptions = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogManager.ThrowExceptions = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LogManager.Configuration == null)
|
|
|
|
{
|
|
|
|
LogManager.Configuration = new LoggingConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RegisterConsoleLogger(LogLevel minLevel, string loggerNamePattern = "*")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var consoleTarget = new ConsoleTarget();
|
|
|
|
consoleTarget.Layout = "${message} ${exception}";
|
|
|
|
LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule(loggerNamePattern, minLevel, consoleTarget));
|
2011-11-14 03:22:18 +03:00
|
|
|
|
|
|
|
LogManager.ConfigurationReloaded += (sender, args) => RegisterConsoleLogger(minLevel, loggerNamePattern);
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
|
|
if (LogManager.ThrowExceptions)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RegisterUdpLogger()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-11-14 05:54:09 +03:00
|
|
|
var udpTarget = new NLogViewerTarget();
|
2011-10-24 08:54:09 +03:00
|
|
|
udpTarget.Address = "udp://127.0.0.1:20480";
|
|
|
|
udpTarget.IncludeCallSite = true;
|
|
|
|
udpTarget.IncludeSourceInfo = true;
|
|
|
|
udpTarget.IncludeNLogData = true;
|
|
|
|
udpTarget.IncludeNdc = true;
|
2011-11-14 05:54:09 +03:00
|
|
|
udpTarget.Parameters.Add(new NLogViewerParameterInfo
|
|
|
|
{
|
|
|
|
Name = "Exception",
|
|
|
|
Layout = "${exception:format=ToString}"
|
|
|
|
});
|
|
|
|
|
2011-10-24 08:54:09 +03:00
|
|
|
LogManager.Configuration.AddTarget(udpTarget.GetType().Name, udpTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, udpTarget));
|
2011-11-14 03:22:18 +03:00
|
|
|
|
|
|
|
LogManager.ConfigurationReloaded += (sender, args) => RegisterUdpLogger();
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
|
|
if (LogManager.ThrowExceptions)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 22:07:39 +03:00
|
|
|
|
|
|
|
public static void RegisterFileLogger(string fileName)
|
|
|
|
{
|
|
|
|
var fileTarget = new FileTarget();
|
|
|
|
fileTarget.AutoFlush = true;
|
|
|
|
fileTarget.ConcurrentWrites = false;
|
|
|
|
fileTarget.FileName = fileName;
|
|
|
|
fileTarget.KeepFileOpen = false;
|
|
|
|
fileTarget.Layout = "${longdate} - ${logger}: ${message} ${exception:format=ToString}";
|
|
|
|
|
|
|
|
LogManager.Configuration.AddTarget(fileTarget.GetType().Name, fileTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
|
|
|
|
}
|
|
|
|
|
2011-10-24 08:54:09 +03:00
|
|
|
public static void RegisterExceptioneer()
|
|
|
|
{
|
2011-11-13 10:27:16 +03:00
|
|
|
if (EnviromentProvider.IsProduction)
|
2011-10-24 08:54:09 +03:00
|
|
|
{
|
2011-11-13 10:27:16 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var exTarget = new ExceptioneerTarget();
|
|
|
|
LogManager.Configuration.AddTarget("Exceptioneer", exTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Error, exTarget));
|
2011-11-14 03:22:18 +03:00
|
|
|
|
|
|
|
LogManager.ConfigurationReloaded += (sender, args) => RegisterExceptioneer();
|
2011-11-13 10:27:16 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
}
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Reload()
|
|
|
|
{
|
|
|
|
LogManager.Configuration.Reload();
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|