Radarr/src/NzbDrone.Test.Common/LoggingTest.cs

59 lines
2.0 KiB
C#
Raw Normal View History

2014-12-17 07:26:19 +00:00
using System.Linq;
2011-10-24 05:54:09 +00:00
using NLog;
using NLog.Config;
using NLog.Targets;
2011-11-13 07:27:16 +00:00
using NUnit.Framework;
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
{
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
protected static void InitLogging()
2011-10-24 05:54:09 +00:00
{
2013-11-26 06:53:36 +00:00
new StartupContext();
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();
2013-05-21 05:12:11 +00:00
var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" };
LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget));
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
}
private static void RegisterExceptionVerification()
{
var exceptionVerification = new ExceptionVerification();
LogManager.Configuration.AddTarget("ExceptionVerification", exceptionVerification);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Warn, exceptionVerification));
2011-11-13 07:27:16 +00:00
}
[SetUp]
public void LoggingTestSetup()
{
InitLogging();
ExceptionVerification.Reset();
}
[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
2013-08-18 02:00:29 +00:00
if (BuildInfo.IsDebug && TestContext.CurrentContext.Result.State == TestState.Success)
2013-04-30 00:04:14 +00:00
{
ExceptionVerification.AssertNoUnexpectedLogs();
2013-04-30 00:04:14 +00:00
}
2011-10-24 05:54:09 +00:00
}
}
}