Skip to content

Commit

Permalink
ExceptionDataLayoutRendererTests - Changed to isolated LogFactory (NL…
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Jan 17, 2022
1 parent 93a5bd2 commit 724b521
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,53 @@ namespace NLog.UnitTests.LayoutRenderers
{
using System;
using Xunit;
using NLog.Config;

public class ExceptionDataLayoutRendererTests : NLogTestBase
{
private Logger logger = LogManager.GetLogger("NLog.UnitTests.LayoutRenderer.ExceptionDataLayoutRendererTests");

[Fact]
public void ExceptionWithDataItemIsLoggedTest()
{
const string exceptionMessage = "Test exception";
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";

LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:"+exceptionDataKey+@"}' />
<target name='debug' type='Debug' layout='${exceptiondata:" + exceptionDataKey + @"}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");
</nlog>").LogFactory;
Exception ex = new ArgumentException(exceptionMessage);
ex.Data.Add(exceptionDataKey, exceptionDataValue);

logger.Error(ex, "msg");
AssertDebugLastMessage("debug1", exceptionDataValue);
logFactory.GetCurrentClassLogger().Error(ex, "msg");

logFactory.AssertDebugLastMessage("debug", exceptionDataValue);
}

[Fact]
public void ExceptionWithOutDataIsNotLoggedTest()
{
const string exceptionMessage = "Test exception";
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";

LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:" + exceptionDataKey + @"}' />
<target name='debug' type='Debug' layout='${exceptiondata:" + exceptionDataKey + @"}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");

</nlog>").LogFactory;

Exception ex = new ArgumentException(exceptionMessage);
logger.Error(ex, "msg");
logFactory.GetCurrentClassLogger().Error(ex, "msg");

Assert.NotEqual(GetDebugLastMessage("debug1"), exceptionDataValue);
logFactory.AssertDebugLastMessage("");
}

[Fact]
Expand All @@ -96,21 +91,20 @@ public void ExceptionUsingSpecifiedParamLogsProperlyTest()
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";

LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:item="+exceptionDataKey+@"}' />
<target name='debug' type='Debug' layout='${exceptiondata:item=" + exceptionDataKey + @"}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");

</nlog>").LogFactory;

Exception ex = new ArgumentException(exceptionMessage);
ex.Data.Add(exceptionDataKey, exceptionDataValue);
logger.Error(ex);
AssertDebugLastMessage("debug1", exceptionDataValue);
logFactory.GetCurrentClassLogger().Error(ex);
logFactory.AssertDebugLastMessage(exceptionDataValue);
}

[Fact]
Expand All @@ -119,20 +113,21 @@ public void BadDataForItemResultsInEmptyValueTest()
const string exceptionMessage = "I don't like nullref exception!";
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"

var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:item=@}' />
<target name='debug' type='Debug' layout='${exceptiondata:item=@}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");
</nlog>").LogFactory;

Exception ex = new ArgumentException(exceptionMessage);
ex.Data.Add(exceptionDataKey, exceptionDataValue);
logger.Error(ex);
AssertDebugLastMessage("debug1", "");
logFactory.GetCurrentClassLogger().Error(ex);
logFactory.AssertDebugLastMessage("");
}

[Fact]
Expand All @@ -141,20 +136,20 @@ public void NoDatkeyTest()
const string exceptionMessage = "I don't like nullref exception!";
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:}' />
<target name='debug' type='Debug' layout='${exceptiondata:}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");
</nlog>").LogFactory;

Exception ex = new ArgumentException(exceptionMessage);
ex.Data.Add(exceptionDataKey, exceptionDataValue);
logger.Error(ex);
AssertDebugLastMessage("debug1", "");
logFactory.GetCurrentClassLogger().Error(ex);
logFactory.AssertDebugLastMessage("");
}

[Fact]
Expand All @@ -163,36 +158,36 @@ public void NoDatkeyUingParamTest()
const string exceptionMessage = "I don't like nullref exception!";
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:item=}' />
<target name='debug' type='Debug' layout='${exceptiondata:item=}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");
</nlog>").LogFactory;

Exception ex = new ArgumentException(exceptionMessage);
ex.Data.Add(exceptionDataKey, exceptionDataValue);
logger.Error(ex);
AssertDebugLastMessage("debug1", "");
logFactory.GetCurrentClassLogger().Error(ex);
logFactory.AssertDebugLastMessage("");
}

[Fact]
public void BaseExceptionFlippedTest()
{
const string exceptionDataKey = "testkey";
const string exceptionDataValue = "testvalue";
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
<nlog>
<targets>
<target name='debug1' type='Debug' layout='${exceptiondata:item=" + exceptionDataKey + @":BaseException=true}' />
<target name='debug' type='Debug' layout='${exceptiondata:item=" + exceptionDataKey + @":BaseException=true}' />
</targets>
<rules>
<logger minlevel='Info' writeTo='debug1' />
<logger minlevel='Info' writeTo='debug' />
</rules>
</nlog>");
</nlog>").LogFactory;

Exception exceptionToTest;
try
Expand Down Expand Up @@ -220,9 +215,9 @@ public void BaseExceptionFlippedTest()
exceptionToTest = ex;
}

logger.Fatal(exceptionToTest, "msg");
logFactory.GetCurrentClassLogger().Fatal(exceptionToTest, "msg");

AssertDebugLastMessage("debug1", exceptionDataValue);
logFactory.AssertDebugLastMessage(exceptionDataValue);
}
}
}
2 changes: 1 addition & 1 deletion tests/NLog.UnitTests/LogFactoryTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static DebugTarget GetDebugTarget(string targetName, LoggingConfiguration
var debugTarget = configuration.FindTargetByName<DebugTarget>(targetName);
if (debugTarget is null)
{
throw new Exception($"debugtarget with name {targetName} not found in configuration");
throw new Exception($"DebugTarget(Name={targetName}) not found in configuration");
}
return debugTarget;
}
Expand Down

0 comments on commit 724b521

Please sign in to comment.