Skip to content

Commit

Permalink
Added unit test as showcase
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed Mar 17, 2016
1 parent 38aef00 commit 50b756d
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion tests/NLog.UnitTests/Layouts/JsonLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using NLog.Targets;

namespace NLog.UnitTests.Layouts
{
using System;
Expand Down Expand Up @@ -192,7 +194,7 @@ public void NestedJsonAttrTest()
new JsonAttribute("message", "${exception:format=Message}"),
new JsonAttribute("innerException", new JsonLayout
{

Attributes =
{
new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
Expand All @@ -213,5 +215,59 @@ public void NestedJsonAttrTest()
Assert.Equal("{ \"type\": \"NLog.NLogRuntimeException\", \"message\": \"test\", \"innerException\": { \"type\": \"System.NullReferenceException\", \"message\": \"null is bad!\" } }", json);

}

[Fact]
public void NestedJsonAttrTestFromXML()
{
var configXml = @"
<nlog>
<targets>
<target name='jsonFile' type='File' fileName='log.json'>
<layout type='JsonLayout'>
<attribute name='time' layout='${longdate}' />
<attribute name='level' layout='${level:upperCase=true}'/>
<attribute name='nested' encode='false' >
<layout type='JsonLayout'>
<attribute name='message' layout='${message}' />
<attribute name='exception' layout='${exception}' />
</layout>
</attribute>
</layout>
</target>
</targets>
<rules>
</rules>
</nlog>
";

var config = CreateConfigurationFromString(configXml);

Assert.NotNull(config);
var target = config.FindTargetByName<FileTarget>("jsonFile");
Assert.NotNull(target);
var jsonLayout = target.Layout as JsonLayout;
Assert.NotNull(jsonLayout);
var attrs = jsonLayout.Attributes;
Assert.NotNull(attrs);
Assert.Equal(3, attrs.Count);
Assert.Equal(typeof(SimpleLayout), attrs[0].Layout.GetType());
Assert.Equal(typeof(SimpleLayout), attrs[1].Layout.GetType());
Assert.Equal(typeof(JsonLayout), attrs[2].Layout.GetType());
var nestedJsonLayout = (JsonLayout)attrs[2].Layout;
Assert.Equal(2, nestedJsonLayout.Attributes.Count);
Assert.Equal("'${message}'", nestedJsonLayout.Attributes[0].Layout.ToString());
Assert.Equal("'${exception}'", nestedJsonLayout.Attributes[1].Layout.ToString());

var logEventInfo = new LogEventInfo
{
TimeStamp = new DateTime(2016, 10, 30, 13, 30, 55),
Message = "this is message",
Level = LogLevel.Info,
Exception = new NLogRuntimeException("test", new NullReferenceException("null is bad!"))
};

var json = jsonLayout.Render(logEventInfo);
Assert.Equal("{ \"time\": \"2016-10-30 13:30:55.0000\", \"level\": \"INFO\", \"nested\": { \"message\": \"this is message\", \"exception\": \"test\" } }", json);
}
}
}

0 comments on commit 50b756d

Please sign in to comment.