1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-26 01:37:07 +00:00
Sonarr/NzbDrone.Common/LogConfiguration.cs

153 lines
5.3 KiB
C#
Raw Normal View History

2011-10-24 05:54:09 +00:00
using System;
2011-12-19 19:07:39 +00:00
using System.IO;
2011-10-24 05:54:09 +00:00
using NLog;
using NLog.Config;
using NLog.Targets;
using NzbDrone.Common.NlogTargets;
2011-10-24 05:54:09 +00:00
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));
LogManager.ConfigurationReloaded += (sender, args) => RegisterConsoleLogger(minLevel, loggerNamePattern);
2011-10-24 05:54:09 +00:00
}
catch (Exception e)
{
Console.WriteLine(e);
if (LogManager.ThrowExceptions)
throw;
}
}
public static void RegisterUdpLogger()
{
try
{
2011-11-14 02:54:09 +00:00
var udpTarget = new NLogViewerTarget();
2011-10-24 05:54:09 +00:00
udpTarget.Address = "udp://127.0.0.1:20480";
udpTarget.IncludeCallSite = true;
udpTarget.IncludeSourceInfo = true;
udpTarget.IncludeNLogData = true;
udpTarget.IncludeNdc = true;
2011-11-14 02:54:09 +00:00
udpTarget.Parameters.Add(new NLogViewerParameterInfo
{
Name = "Exception",
Layout = "${exception:format=ToString}"
});
2011-10-24 05:54:09 +00:00
LogManager.Configuration.AddTarget(udpTarget.GetType().Name, udpTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, udpTarget));
LogManager.ConfigurationReloaded += (sender, args) => RegisterUdpLogger();
2011-10-24 05:54:09 +00:00
}
catch (Exception e)
{
Console.WriteLine(e);
if (LogManager.ThrowExceptions)
throw;
}
}
2011-12-19 19:07:39 +00:00
2012-01-23 02:24:16 +00:00
private static FileTarget GetBaseTarget()
2011-12-19 19:07:39 +00:00
{
var fileTarget = new FileTarget();
fileTarget.AutoFlush = true;
2012-01-23 02:24:16 +00:00
fileTarget.ConcurrentWrites = true;
2011-12-19 19:07:39 +00:00
fileTarget.KeepFileOpen = false;
2012-01-23 02:24:16 +00:00
fileTarget.ConcurrentWriteAttemptDelay = 50;
fileTarget.ConcurrentWriteAttempts = 200;
2012-02-05 16:53:08 +00:00
fileTarget.Layout = @"${date:format=yy-M-d HH\:mm\:ss.f}|${replace:searchFor=NzbDrone.:replaceWith=:inner=${logger}}|${level}|${message}|${exception:format=ToString}";
2012-01-23 02:24:16 +00:00
return fileTarget;
}
public static void RegisterFileLogger(string fileName, LogLevel level)
{
var fileTarget = GetBaseTarget();
fileTarget.FileName = fileName;
2012-01-23 02:24:16 +00:00
LogManager.Configuration.AddTarget(Guid.NewGuid().ToString(), fileTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", level, fileTarget));
}
public static void RegisterRollingFileLogger(string fileName, LogLevel level)
{
var fileTarget = GetBaseTarget();
fileTarget.FileName = fileName;
fileTarget.ArchiveAboveSize = 200000; //500K
fileTarget.MaxArchiveFiles = 1;
fileTarget.EnableFileDelete = true;
fileTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling;
2011-12-19 19:07:39 +00:00
2012-01-23 02:24:16 +00:00
LogManager.Configuration.AddTarget(Guid.NewGuid().ToString(), fileTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", level, fileTarget));
2011-12-19 19:07:39 +00:00
}
public static void RegisterRemote()
2011-10-24 05:54:09 +00:00
{
2011-11-13 07:27:16 +00:00
if (EnviromentProvider.IsProduction)
2011-10-24 05:54:09 +00:00
{
2011-11-13 07:27:16 +00:00
try
{
var exceptioneerTarget = new ExceptioneerTarget();
LogManager.Configuration.AddTarget("Exceptioneer", exceptioneerTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, exceptioneerTarget));
2011-11-13 07:27:16 +00:00
}
catch (Exception e)
{
Console.WriteLine(e);
}
2011-10-24 05:54:09 +00:00
}
try
{
var remoteTarget = new RemoteTarget();
LogManager.Configuration.AddTarget("RemoteTarget", remoteTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, remoteTarget));
}
catch (Exception e)
{
Console.WriteLine(e);
}
LogManager.ConfigurationReloaded += (sender, args) => RegisterRemote();
2011-10-24 05:54:09 +00:00
}
public static void Reload()
{
LogManager.Configuration.Reload();
LogManager.ReconfigExistingLoggers();
}
}
}