2020-02-09 18:08:34 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2020-02-09 02:35:16 +00:00
|
|
|
using Jackett.Common.Models.Config;
|
2018-06-22 12:21:10 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-03-26 22:15:28 +00:00
|
|
|
var logFile = new FileTarget
|
|
|
|
{
|
|
|
|
Layout = "${longdate} ${level} ${message} ${exception:format=ToString}",
|
|
|
|
FileName = Path.Combine(settings.DataFolder, logFileName),
|
|
|
|
ArchiveFileName = Path.Combine(settings.DataFolder, logFileName + ".{#####}.txt"),
|
|
|
|
ArchiveAboveSize = 2097152, // 2 MB
|
|
|
|
MaxArchiveFiles = 5,
|
|
|
|
KeepFileOpen = false,
|
|
|
|
ArchiveNumbering = ArchiveNumberingMode.DateAndSequence
|
|
|
|
};
|
2018-11-14 16:52:56 +00:00
|
|
|
logConfig.AddTarget("file", logFile);
|
|
|
|
|
2020-03-26 22:15:28 +00:00
|
|
|
var microsoftRule = new LoggingRule
|
|
|
|
{
|
|
|
|
LoggerNamePattern = "Microsoft.*",
|
|
|
|
Final = true
|
|
|
|
};
|
2018-11-14 16:52:56 +00:00
|
|
|
microsoftRule.SetLoggingLevels(LogLevel.Warn, LogLevel.Fatal);
|
|
|
|
microsoftRule.Targets.Add(logFile);
|
|
|
|
|
2020-03-26 22:15:28 +00:00
|
|
|
var microsoftDebugRule = new LoggingRule
|
|
|
|
{
|
|
|
|
LoggerNamePattern = "Microsoft.*"
|
|
|
|
};
|
2018-11-14 16:52:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-03-26 22:15:28 +00:00
|
|
|
var logConsole = new ColoredConsoleTarget
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2020-02-25 16:08:03 +00:00
|
|
|
protected override void Append(StringBuilder builder, LogEventInfo logEvent) =>
|
2018-07-02 11:03:03 +00:00
|
|
|
builder.Append(DateTime.Now.ToString("MM-dd HH:mm:ss"));
|
|
|
|
}
|
2018-06-22 12:21:10 +00:00
|
|
|
}
|
|
|
|
}
|