2017-09-08 19:28:42 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2011-10-24 05:54:09 +00:00
|
|
|
using NLog;
|
|
|
|
using NLog.Config;
|
2013-02-28 02:43:01 +00:00
|
|
|
using NLog.Targets;
|
2011-11-13 07:27:16 +00:00
|
|
|
using NUnit.Framework;
|
2016-04-02 02:19:32 +00:00
|
|
|
using NUnit.Framework.Interfaces;
|
2013-08-13 05:08:37 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-12-17 07:26:19 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
2011-10-24 05:54:09 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Test.Common
|
|
|
|
{
|
2011-11-03 05:04:14 +00:00
|
|
|
public abstract class LoggingTest
|
2011-10-24 05:54:09 +00:00
|
|
|
{
|
2014-12-17 07:26:19 +00:00
|
|
|
protected static readonly Logger TestLogger = NzbDroneLogger.GetLogger("TestLogger");
|
2013-02-23 20:09:44 +00:00
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
protected static void InitLogging()
|
2011-10-24 05:54:09 +00:00
|
|
|
{
|
2013-11-26 06:53:36 +00:00
|
|
|
new StartupContext();
|
2013-08-13 05:08:37 +00:00
|
|
|
|
2014-12-17 07:26:19 +00:00
|
|
|
if (LogManager.Configuration == null || LogManager.Configuration.AllTargets.None(c => c is ExceptionVerification))
|
2011-11-08 07:01:52 +00:00
|
|
|
{
|
|
|
|
LogManager.Configuration = new LoggingConfiguration();
|
2017-09-08 19:28:42 +00:00
|
|
|
|
|
|
|
var logOutput = TestLogOutput.Console;
|
|
|
|
Enum.TryParse<TestLogOutput>(Environment.GetEnvironmentVariable("SONARR_TESTS_LOG_OUTPUT"), out logOutput);
|
|
|
|
|
2019-04-27 23:54:33 +00:00
|
|
|
RegisterSentryLogger();
|
|
|
|
|
2017-09-08 19:28:42 +00:00
|
|
|
switch (logOutput)
|
|
|
|
{
|
|
|
|
case TestLogOutput.Console:
|
|
|
|
RegisterConsoleLogger();
|
|
|
|
break;
|
|
|
|
case TestLogOutput.File:
|
|
|
|
RegisterFileLogger();
|
|
|
|
break;
|
|
|
|
}
|
2011-10-24 05:54:09 +00:00
|
|
|
|
2011-11-08 07:01:52 +00:00
|
|
|
RegisterExceptionVerification();
|
2014-12-17 07:26:19 +00:00
|
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
2011-11-08 07:01:52 +00:00
|
|
|
}
|
2011-10-24 05:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 19:28:42 +00:00
|
|
|
private static void RegisterConsoleLogger()
|
|
|
|
{
|
|
|
|
var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" };
|
|
|
|
LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void RegisterFileLogger()
|
|
|
|
{
|
|
|
|
const string layout = @"${level}|${message}${onexception:inner=${newline}${newline}${exception:format=ToString}${newline}}";
|
|
|
|
|
|
|
|
var fileTarget = new FileTarget();
|
|
|
|
|
|
|
|
fileTarget.Name = "Test File Logger";
|
|
|
|
fileTarget.FileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestLog.txt");
|
|
|
|
fileTarget.AutoFlush = false;
|
|
|
|
fileTarget.KeepFileOpen = true;
|
|
|
|
fileTarget.ConcurrentWrites = true;
|
|
|
|
fileTarget.ConcurrentWriteAttemptDelay = 50;
|
|
|
|
fileTarget.ConcurrentWriteAttempts = 10;
|
|
|
|
fileTarget.Layout = layout;
|
|
|
|
|
|
|
|
LogManager.Configuration.AddTarget(fileTarget.GetType().Name, fileTarget);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
|
|
|
|
}
|
|
|
|
|
2019-04-27 23:54:33 +00:00
|
|
|
private static void RegisterSentryLogger()
|
|
|
|
{
|
|
|
|
// Register a null target for sentry logs, so they aren't caught by other loggers.
|
|
|
|
var loggingRuleSentry = new LoggingRule("Sentry", LogLevel.Debug, new NullTarget()) { Final = true };
|
|
|
|
LogManager.Configuration.LoggingRules.Insert(0, loggingRuleSentry);
|
|
|
|
}
|
|
|
|
|
2011-10-24 05:54:09 +00:00
|
|
|
private static void RegisterExceptionVerification()
|
|
|
|
{
|
|
|
|
var exceptionVerification = new ExceptionVerification();
|
|
|
|
LogManager.Configuration.AddTarget("ExceptionVerification", exceptionVerification);
|
2013-03-04 05:53:02 +00:00
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Warn, exceptionVerification));
|
2011-11-13 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void LoggingTestSetup()
|
|
|
|
{
|
|
|
|
InitLogging();
|
|
|
|
ExceptionVerification.Reset();
|
2017-09-08 19:28:42 +00:00
|
|
|
TestLogger.Info("--- Start: {0} ---", TestContext.CurrentContext.Test.FullName);
|
2011-11-13 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void LoggingDownBase()
|
|
|
|
{
|
2013-08-06 05:29:53 +00:00
|
|
|
//can't use because of a bug in mono with 2.6.2,
|
|
|
|
//https://bugs.launchpad.net/nunitv2/+bug/1076932
|
2016-04-02 02:19:32 +00:00
|
|
|
if (BuildInfo.IsDebug && TestContext.CurrentContext.Result.Outcome == ResultState.Success)
|
2013-04-30 00:04:14 +00:00
|
|
|
{
|
2014-04-10 22:19:40 +00:00
|
|
|
ExceptionVerification.AssertNoUnexpectedLogs();
|
2013-04-30 00:04:14 +00:00
|
|
|
}
|
2017-09-08 19:28:42 +00:00
|
|
|
|
|
|
|
TestLogger.Info("--- End: {0} ---", TestContext.CurrentContext.Test.FullName);
|
2011-10-24 05:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|