public void JsonLayoutRenderingAndEncodingSpecialCharacters() { var jsonLayout = new JsonLayout() { Attributes = { new JsonAttribute("date", "${longdate}"), new JsonAttribute("level", "${level}"), new JsonAttribute("message", "${message}"), } }; var ev = new LogEventInfo(); ev.TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56); ev.Level = LogLevel.Info; ev.Message = "\"hello, world\""; Assert.Equal("{ \"date\": \"2010-01-01 12:34:56.0000\", \"level\": \"Info\", \"message\": \"\\\"hello, world\\\"\" }", jsonLayout.Render(ev)); }
public void JsonLayoutRenderingNoSpaces() { var jsonLayout = new JsonLayout() { Attributes = { new JsonAttribute("date", "${longdate}"), new JsonAttribute("level", "${level}"), new JsonAttribute("message", "${message}"), }, SuppressSpaces = true }; var ev = new LogEventInfo(); ev.TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56); ev.Level = LogLevel.Info; ev.Message = "hello, world"; Assert.Equal("{\"date\":\"2010-01-01 12:34:56.0000\",\"level\":\"Info\",\"message\":\"hello, world\"}", jsonLayout.Render(ev)); }
public void JsonLayoutRendering() { var jsonLayout = new JsonLayout() { Attributes = { new JsonAttribute("date", "${longdate}"), new JsonAttribute("level", "${level}"), new JsonAttribute("message", "${message}"), } }; var logEventInfo = new LogEventInfo { TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56), Level = LogLevel.Info, Message = "hello, world" }; Assert.Equal("{ \"date\": \"2010-01-01 12:34:56.0000\", \"level\": \"Info\", \"message\": \"hello, world\" }", jsonLayout.Render(logEventInfo)); }
public void NestedJsonAttrTest() { var jsonLayout = new JsonLayout { Attributes = { new JsonAttribute("type", "${exception:format=Type}"), new JsonAttribute("message", "${exception:format=Message}"), new JsonAttribute("innerException", new JsonLayout { Attributes = { new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"), new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"), } }, //don't escape layout false) } }; var logEventInfo = new LogEventInfo { Exception = new NLogRuntimeException("test", new NullReferenceException("null is bad!")) }; var json = jsonLayout.Render(logEventInfo); Assert.Equal("{ \"type\": \"NLog.NLogRuntimeException\", \"message\": \"test\", \"innerException\": { \"type\": \"System.NullReferenceException\", \"message\": \"null is bad!\" } }", json); }
public void NestedJsonAttrRendersEmptyLiteralIfRenderEmptyObjectIsTrueTest() { var jsonLayout = new JsonLayout { Attributes = { new JsonAttribute("type", "${exception:format=Type}"), new JsonAttribute("message", "${exception:format=Message}"), new JsonAttribute("innerException", new JsonLayout { Attributes = { new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"), new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"), }, RenderEmptyObject = true }, //don't escape layout false) } }; var logEventInfo = new LogEventInfo { Exception = new NLogRuntimeException("test", (Exception)null) }; var json = jsonLayout.Render(logEventInfo); Assert.Equal("{ \"type\": \"NLog.NLogRuntimeException\", \"message\": \"test\", \"innerException\": { } }", json); }
public void IncludeAllJsonProperties() { var jsonLayout = new JsonLayout() { IncludeAllProperties = true }; jsonLayout.ExcludeProperties.Add("Excluded1"); jsonLayout.ExcludeProperties.Add("Excluded2"); var logEventInfo = CreateLogEventWithExcluded(); Assert.Equal(ExpectedIncludeAllPropertiesWithExcludes, jsonLayout.Render(logEventInfo)); }