2018-06-22 12:21:10 +00:00
|
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
|
using Jackett.Common.Services;
|
2018-07-02 11:03:03 +00:00
|
|
|
|
using NLog;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
using NLog.Config;
|
2018-07-02 11:03:03 +00:00
|
|
|
|
using NLog.LayoutRenderers;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
using NLog.Targets;
|
2018-07-02 11:03:03 +00:00
|
|
|
|
using System;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
using System.IO;
|
2018-07-02 11:03:03 +00:00
|
|
|
|
using System.Text;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
|
|
namespace Jackett.Common.Utils
|
|
|
|
|
{
|
|
|
|
|
public static class LoggingSetup
|
|
|
|
|
{
|
|
|
|
|
public static LoggingConfiguration GetLoggingConfiguration(RuntimeSettings settings, bool fileOnly = false)
|
|
|
|
|
{
|
|
|
|
|
var logFileName = settings.CustomLogFileName ?? "log.txt";
|
|
|
|
|
var logLevel = settings.TracingEnabled ? NLog.LogLevel.Debug : NLog.LogLevel.Info;
|
|
|
|
|
// Add custom date time format renderer as the default is too long
|
|
|
|
|
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("simpledatetime", typeof(SimpleDateTimeRenderer));
|
|
|
|
|
|
|
|
|
|
var logConfig = new LoggingConfiguration();
|
2018-11-14 16:52:56 +00:00
|
|
|
|
|
2018-06-22 12:21:10 +00:00
|
|
|
|
var logFile = new FileTarget();
|
|
|
|
|
logFile.Layout = "${longdate} ${level} ${message} ${exception:format=ToString}";
|
|
|
|
|
logFile.FileName = Path.Combine(settings.DataFolder, logFileName);
|
|
|
|
|
logFile.ArchiveFileName = Path.Combine(settings.DataFolder, logFileName + ".{#####}.txt");
|
|
|
|
|
logFile.ArchiveAboveSize = 500000;
|
|
|
|
|
logFile.MaxArchiveFiles = 5;
|
|
|
|
|
logFile.KeepFileOpen = false;
|
|
|
|
|
logFile.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
|
2018-11-14 16:52:56 +00:00
|
|
|
|
logConfig.AddTarget("file", logFile);
|
|
|
|
|
|
|
|
|
|
var microsoftRule = new LoggingRule();
|
|
|
|
|
microsoftRule.LoggerNamePattern = "Microsoft.*";
|
|
|
|
|
microsoftRule.SetLoggingLevels(LogLevel.Warn, LogLevel.Fatal);
|
|
|
|
|
microsoftRule.Final = true;
|
|
|
|
|
microsoftRule.Targets.Add(logFile);
|
|
|
|
|
|
|
|
|
|
var microsoftDebugRule = new LoggingRule();
|
|
|
|
|
microsoftDebugRule.LoggerNamePattern = "Microsoft.*";
|
|
|
|
|
microsoftDebugRule.SetLoggingLevels(LogLevel.Debug, LogLevel.Info);
|
|
|
|
|
microsoftDebugRule.Final = true;
|
|
|
|
|
if (settings.TracingEnabled)
|
|
|
|
|
{
|
|
|
|
|
microsoftDebugRule.Targets.Add(logFile);
|
|
|
|
|
}
|
|
|
|
|
logConfig.LoggingRules.Add(microsoftDebugRule);
|
|
|
|
|
|
2018-06-22 12:21:10 +00:00
|
|
|
|
var logFileRule = new LoggingRule("*", logLevel, logFile);
|
|
|
|
|
logConfig.LoggingRules.Add(logFileRule);
|
|
|
|
|
|
|
|
|
|
if (!fileOnly)
|
|
|
|
|
{
|
|
|
|
|
var logConsole = new ColoredConsoleTarget();
|
2018-11-14 16:52:56 +00:00
|
|
|
|
logConsole.Layout = "${simpledatetime} ${level} ${message} ${exception:format=ToString}";
|
2018-06-22 12:21:10 +00:00
|
|
|
|
logConfig.AddTarget("console", logConsole);
|
|
|
|
|
|
|
|
|
|
var logConsoleRule = new LoggingRule("*", logLevel, logConsole);
|
|
|
|
|
logConfig.LoggingRules.Add(logConsoleRule);
|
|
|
|
|
|
|
|
|
|
var logService = new LogCacheService();
|
|
|
|
|
logConfig.AddTarget("service", logService);
|
2018-11-14 16:52:56 +00:00
|
|
|
|
|
2018-06-22 12:21:10 +00:00
|
|
|
|
var serviceRule = new LoggingRule("*", logLevel, logService);
|
|
|
|
|
logConfig.LoggingRules.Add(serviceRule);
|
2018-11-14 16:52:56 +00:00
|
|
|
|
|
|
|
|
|
microsoftRule.Targets.Add(logConsole);
|
|
|
|
|
microsoftRule.Targets.Add(logService);
|
|
|
|
|
|
|
|
|
|
if (settings.TracingEnabled)
|
|
|
|
|
{
|
|
|
|
|
microsoftDebugRule.Targets.Add(logConsole);
|
|
|
|
|
microsoftDebugRule.Targets.Add(logService);
|
|
|
|
|
}
|
2018-06-22 12:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 16:52:56 +00:00
|
|
|
|
logConfig.LoggingRules.Add(microsoftRule);
|
|
|
|
|
|
2018-06-22 12:21:10 +00:00
|
|
|
|
return logConfig;
|
|
|
|
|
}
|
2018-07-02 11:03:03 +00:00
|
|
|
|
|
|
|
|
|
[LayoutRenderer("simpledatetime")]
|
|
|
|
|
public class SimpleDateTimeRenderer : LayoutRenderer
|
|
|
|
|
{
|
|
|
|
|
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
|
|
|
|
|
{
|
|
|
|
|
builder.Append(DateTime.Now.ToString("MM-dd HH:mm:ss"));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 12:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|