/// <summary> /// Convert LoggingEvent to JSON string for LogStash /// </summary> /// <param name="log">Log event</param> /// <returns>JSON string</returns> protected virtual string ConvertToString(LoggingEvent log) { Contract.Requires(log != null); JsonSimpleWriter writer = new JsonSimpleWriter(1024); writer.AppendJsonBeginObject(); { writer.AppendJsonParamConditional("@timestamp", log.Date.ToUniversalTime().ToString("o", System.Globalization.CultureInfo.InvariantCulture), screen: false); writer.AppendJsonParamConditional("@version", "1", screen: false); writer.AppendJsonParamConditional("level", log.Level.Name, screen: false); writer.AppendJsonParamConditional("message", log.Message ?? "", screen: true); writer.AppendJsonParamConditional("context", log.Context, screen: true); writer.AppendJsonKey("location").AppendJsonBeginObject(); { writer.AppendJsonParamConditional("file", log.FilePath, screen: true); writer.AppendJsonParamConditional("line_number", log.LineNumber > 0 ? log.LineNumber.ToString() : null, screen: false); writer.AppendJsonParamConditional("assembly", log.Assembly, screen: true); writer.AppendJsonParamConditional("namespace", log.Namespace, screen: false); writer.AppendJsonParamConditional("class", log.Clazz, screen: false); writer.AppendJsonParamConditional("method", log.Method, screen: false); writer.AppendJsonParamConditional("stack_source", log.StackSources, screen: true); writer.AppendJsonEndObject(); } writer.AppendJsonKey("source").AppendJsonBeginObject(); { writer.AppendJsonParamConditional("process_name", log.ProcessName, screen: true); writer.AppendJsonParamConditional("pid", log.ProcessId > 0 ? log.ProcessId.ToString() : null, screen: false); writer.AppendJsonParamConditional("machine_name", log.MachineName, screen: true); writer.AppendJsonParamConditional("machine_ip", log.MachineIpAddress, screen: true); writer.AppendJsonEndObject(); } if (log.Exception != null) { List <string> messages = new List <string>(); List <string> types = new List <string>(); Error currentError = log.Exception; while (currentError != null) { messages.Add(currentError.Message); types.Add(currentError.Type); currentError = currentError.InnerError; } writer.AppendJsonParamConditional("exception", _exceptionConverter.Convert(log), screen: true); writer.AppendJsonParamConditional("exception.message", messages, screen: true); writer.AppendJsonParamConditional("exception.type", types, screen: true); } writer.AppendJsonEndObject(withComma: false); } return(writer.GetStringBuilder().Append("\n").ToString()); }
public void JsonSimpleWriterTest() { using (var writer = new JsonSimpleWriter()) { writer.CreateStore(StoreName, OutputPath, false); IStreamMetadata metadata1 = new JsonStreamMetadata("Stream1", 1, TypeName, null, StoreName, OutputPath); IStreamMetadata metadata2 = new JsonStreamMetadata("Stream2", 2, TypeName, null, StoreName, OutputPath); List <Message <SimpleObject> > stream1 = new List <Message <SimpleObject> > { new Message <SimpleObject>(Data, FirstTime, FirstTime, 1, 0), new Message <SimpleObject>(Data, LastTime, LastTime, 1, 1), }; List <Message <SimpleObject> > stream2 = new List <Message <SimpleObject> > { new Message <SimpleObject>(Data, FirstTime, FirstTime, 2, 0), new Message <SimpleObject>(Data, LastTime, LastTime, 2, 1), }; writer.CreateStream(metadata1, stream1); writer.CreateStream(metadata2, stream2); writer.WriteAll(ReplayDescriptor.ReplayAll); } var escapedOutputPath = OutputPath.Replace(@"\", @"\\"); string expectedCatalog = "[{\"Name\":\"Stream1\",\"Id\":1,\"TypeName\":\"Test.Psi.Data.SimpleObject\",\"StoreName\":\"JsonStore\",\"StorePath\":\"" + escapedOutputPath + "\",\"FirstMessageCreationTime\":\"2017-11-01T09:15:30.12345Z\",\"LastMessageCreationTime\":\"2017-11-01T09:15:34.12345Z\",\"FirstMessageOriginatingTime\":\"2017-11-01T09:15:30.12345Z\",\"LastMessageOriginatingTime\":\"2017-11-01T09:15:34.12345Z\",\"MessageCount\":2,\"MessageSizeCumulativeSum\":606,\"LatencyCumulativeSum\":0,\"SupplementalMetadataTypeName\":null,\"OpenedTime\":\"0001-01-01T00:00:00\",\"ClosedTime\":\"9999-12-31T23:59:59.9999999\",\"IsClosed\":false},{\"Name\":\"Stream2\",\"Id\":2,\"TypeName\":\"Test.Psi.Data.SimpleObject\",\"StoreName\":\"JsonStore\",\"StorePath\":\"" + escapedOutputPath + "\",\"FirstMessageCreationTime\":\"2017-11-01T09:15:30.12345Z\",\"LastMessageCreationTime\":\"2017-11-01T09:15:34.12345Z\",\"FirstMessageOriginatingTime\":\"2017-11-01T09:15:30.12345Z\",\"LastMessageOriginatingTime\":\"2017-11-01T09:15:34.12345Z\",\"MessageCount\":2,\"MessageSizeCumulativeSum\":606,\"LatencyCumulativeSum\":0,\"SupplementalMetadataTypeName\":null,\"OpenedTime\":\"0001-01-01T00:00:00\",\"ClosedTime\":\"9999-12-31T23:59:59.9999999\",\"IsClosed\":false}]"; string expectedData = "[{\"Envelope\":{\"SourceId\":1,\"SequenceId\":0,\"OriginatingTime\":\"2017-11-01T09:15:30.12345Z\",\"Time\":\"2017-11-01T09:15:30.12345Z\"},\"Data\":{\"ArrayValue\":[0,1,2,3],\"BoolValue\":true,\"DateTimeValue\":\"2017-11-30T12:59:41.896745Z\",\"DoubleValue\":0.123456,\"IntValue\":123456,\"ListValue\":[4,5,6,7],\"StringValue\":\"abc\",\"TimeSpanValue\":\"01:02:03.4567890\"}},{\"Envelope\":{\"SourceId\":2,\"SequenceId\":0,\"OriginatingTime\":\"2017-11-01T09:15:30.12345Z\",\"Time\":\"2017-11-01T09:15:30.12345Z\"},\"Data\":{\"ArrayValue\":[0,1,2,3],\"BoolValue\":true,\"DateTimeValue\":\"2017-11-30T12:59:41.896745Z\",\"DoubleValue\":0.123456,\"IntValue\":123456,\"ListValue\":[4,5,6,7],\"StringValue\":\"abc\",\"TimeSpanValue\":\"01:02:03.4567890\"}},{\"Envelope\":{\"SourceId\":1,\"SequenceId\":1,\"OriginatingTime\":\"2017-11-01T09:15:34.12345Z\",\"Time\":\"2017-11-01T09:15:34.12345Z\"},\"Data\":{\"ArrayValue\":[0,1,2,3],\"BoolValue\":true,\"DateTimeValue\":\"2017-11-30T12:59:41.896745Z\",\"DoubleValue\":0.123456,\"IntValue\":123456,\"ListValue\":[4,5,6,7],\"StringValue\":\"abc\",\"TimeSpanValue\":\"01:02:03.4567890\"}},{\"Envelope\":{\"SourceId\":2,\"SequenceId\":1,\"OriginatingTime\":\"2017-11-01T09:15:34.12345Z\",\"Time\":\"2017-11-01T09:15:34.12345Z\"},\"Data\":{\"ArrayValue\":[0,1,2,3],\"BoolValue\":true,\"DateTimeValue\":\"2017-11-30T12:59:41.896745Z\",\"DoubleValue\":0.123456,\"IntValue\":123456,\"ListValue\":[4,5,6,7],\"StringValue\":\"abc\",\"TimeSpanValue\":\"01:02:03.4567890\"}}]"; string actualCatalog = string.Empty; string actualData = string.Empty; string catalogPath = Path.Combine(OutputPath, StoreName + ".Catalog.json"); using (var file = File.OpenText(catalogPath)) { actualCatalog = file.ReadToEnd(); } string dataPath = Path.Combine(OutputPath, StoreName + ".Data.json"); using (var file = File.OpenText(dataPath)) { actualData = file.ReadToEnd(); } File.Delete(catalogPath); File.Delete(dataPath); Assert.AreEqual(expectedCatalog, actualCatalog); Assert.AreEqual(expectedData, actualData); }