Skip to content

Instantly share code, notes, and snippets.

@abombss
Created June 8, 2016 19:50
Show Gist options
  • Save abombss/81bfdb86e026c0501d39cdb6c8528144 to your computer and use it in GitHub Desktop.
Save abombss/81bfdb86e026c0501d39cdb6c8528144 to your computer and use it in GitHub Desktop.
SumoLogic SLAB Formatter
/*
========================================================================================
Microsoft patterns & practices (http://microsoft.com/practices)
SEMANTIC LOGGING APPLICATION BLOCK
========================================================================================
Copyright (c) Microsoft. All rights reserved.
Microsoft would like to thank its contributors, a list
of whom are at http://aka.ms/entlib-contributors
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions
and limitations under the License.
Forked from: https://github.com/mspnp/semantic-logging/blob/a58177196d26d678146561711996c13d5e683394/source/Src/SemanticLogging/Formatters/JsonEventTextFormatter.cs
Modified by Adam Tybor 2016-08-06
*/
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Utility;
using Newtonsoft.Json;
using System;
using System.IO;
namespace SemanticLogging
{
/// <summary>
/// A <see cref="IEventTextFormatter"/> implementation that writes out text formatted as JSON.
/// </summary>
/// <remarks>This class is not thread-safe.</remarks>
public class SumoLogicJsonEventTextFormatter : IEventTextFormatter
{
/// <summary>
/// The default event text formatting.
/// </summary>
public const EventTextFormatting DefaultEventTextFormatting = EventTextFormatting.None;
private DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// Initializes a new instance of the <see cref="JsonEventTextFormatter" /> class.
/// </summary>
/// <param name="formatting">The <see cref="EventTextFormatting" /> formatting.</param>
/// <param name="dateTimeFormat">The date time format used for timestamp value.</param>
public SumoLogicJsonEventTextFormatter()
{
}
/// <summary>
/// Write the formatted event output.
/// </summary>
/// <param name="eventEntry">The event data to be formatted.</param>
/// <param name="writer">The writer to receive the formatted output.</param>
public void WriteEvent(EventEntry eventEntry, TextWriter writer)
{
Guard.ArgumentNotNull(eventEntry, "eventEntry");
using (var jsonWriter = new JsonTextWriter(writer) { CloseOutput = false, Formatting = Newtonsoft.Json.Formatting.None })
{
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("timestamp");
jsonWriter.WriteValue(Convert.ToInt64( (eventEntry.Timestamp - _epoch).TotalMilliseconds);
jsonWriter.WritePropertyName(PropertyNames.ProviderId);
jsonWriter.WriteValue(eventEntry.ProviderId);
jsonWriter.WritePropertyName(PropertyNames.EventId);
jsonWriter.WriteValue(eventEntry.EventId);
jsonWriter.WritePropertyName(PropertyNames.Keywords);
jsonWriter.WriteValue((long)eventEntry.Schema.Keywords);
jsonWriter.WritePropertyName(PropertyNames.Level);
jsonWriter.WriteValue((int)eventEntry.Schema.Level);
jsonWriter.WritePropertyName(PropertyNames.Message);
jsonWriter.WriteValue(eventEntry.FormattedMessage);
jsonWriter.WritePropertyName(PropertyNames.Opcode);
jsonWriter.WriteValue((int)eventEntry.Schema.Opcode);
jsonWriter.WritePropertyName(PropertyNames.Task);
jsonWriter.WriteValue((int)eventEntry.Schema.Task);
jsonWriter.WritePropertyName(PropertyNames.Version);
jsonWriter.WriteValue(eventEntry.Schema.Version);
jsonWriter.WritePropertyName(PropertyNames.Payload);
EventEntryUtil.JsonWritePayload(jsonWriter, eventEntry);
jsonWriter.WritePropertyName(PropertyNames.EventName);
jsonWriter.WriteValue(eventEntry.Schema.EventName);
jsonWriter.WritePropertyName(PropertyNames.ProcessId);
jsonWriter.WriteValue(eventEntry.ProcessId);
jsonWriter.WritePropertyName(PropertyNames.ThreadId);
jsonWriter.WriteValue(eventEntry.ThreadId);
if (eventEntry.ActivityId != Guid.Empty)
{
jsonWriter.WritePropertyName(PropertyNames.ActivityId);
jsonWriter.WriteValue(eventEntry.ActivityId);
}
if (eventEntry.RelatedActivityId != Guid.Empty)
{
jsonWriter.WritePropertyName(PropertyNames.RelatedActivityId);
jsonWriter.WriteValue(eventEntry.RelatedActivityId);
}
jsonWriter.WriteEndObject();
jsonWriter.WriteRaw("\r\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment