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)); }
/// <summary> /// /// </summary> public CustomJsonLayout() { 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 = false }, false) }, RenderEmptyObject = false }; Attributes.Add(new JsonAttribute("ID", Layout.FromString("${guid}"))); Attributes.Add(new JsonAttribute("Time", Layout.FromString("${longdate}"))); Attributes.Add(new JsonAttribute("LogType", Layout.FromString("${level:upperCase=true}"))); Attributes.Add(new JsonAttribute("Logger", Layout.FromString("${logger}"))); Attributes.Add(new JsonAttribute("Message", Layout.FromString("${Message}"))); Attributes.Add(new JsonAttribute("Error", jsonLayout, false)); Attributes.Add(new JsonAttribute("Appdomain", Layout.FromString("${appdomain}"))); Attributes.Add(new JsonAttribute("Callsite", Layout.FromString("${callsite}"))); Attributes.Add(new JsonAttribute("Context", Layout.FromString("${all-event-properties}"))); Attributes.Add(new JsonAttribute("Machinename", Layout.FromString("${machinename}"))); Attributes.Add(new JsonAttribute("Processname", Layout.FromString("${processname}"))); Attributes.Add(new JsonAttribute("Processtime", Layout.FromString("${processtime}"))); }
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)); }