Skip to content

Instantly share code, notes, and snippets.

@maryamariyan
Created September 3, 2020 18:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maryamariyan/8dc05a1c7de657d4ed054374c9873220 to your computer and use it in GitHub Desktop.
EventSourceLoggerOutOfProcess
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Text.Json;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Extensions.Logging;
namespace Microsoft.Repro
{
class Program
{
static void Main(string[] args)
{
var processId = 37240; // ME Logging breakpoints go here
var providerList = new List<EventPipeProvider>()
{
new EventPipeProvider(
"Microsoft-Extensions-Logging",
EventLevel.LogAlways,
(long)(Keywords.JsonMessage | Keywords.FormattedMessage | Keywords.Message))
//, filter too
};
var diagnosticsClient = new DiagnosticsClient(processId);
EventPipeSession session = null;
try
{
session = diagnosticsClient.StartEventPipeSession(providerList, true);
var source = new EventPipeEventSource(session.EventStream);
source.Dynamic.All += (eventData) =>
{
Console.WriteLine($"Provider = {eventData.ProviderName} ID = {eventData.ID} Name = {eventData.EventName}");
};
source.Dynamic.AddCallbackForProviderEvent("Microsoft-Extensions-Logging", "FormattedMessage", (traceEvent) =>
{
// Level, FactoryID, LoggerName, EventID, EventName, FormattedMessage
var categoryName = (string)traceEvent.PayloadValue(2);
var logLevel = traceEvent.PayloadValue(0);
var message = (string)traceEvent.PayloadValue(4);
});
source.Dynamic.AddCallbackForProviderEvent("Microsoft-Extensions-Logging", "Message", (traceEvent) =>
{
var stacktrace = (string)traceEvent.PayloadValue(5);
});
source.Dynamic.AddCallbackForProviderEvent("Microsoft-Extensions-Logging", "MessageJson", (traceEvent) =>
{
// Level, FactoryID, LoggerName, EventID, EventName, ExceptionJson, ArgumentsJson
var logLevel = (LogLevel)traceEvent.PayloadByName("Level");
var factoryId = (int)traceEvent.PayloadByName("FactoryID");
var categoryName = (string)traceEvent.PayloadByName("LoggerName");
var eventId = (int)traceEvent.PayloadByName("EventId");
var eventName = (string)traceEvent.PayloadByName("EventName");
var exceptionJson = (string)traceEvent.PayloadByName("ExceptionJson");
var argsJson = (string)traceEvent.PayloadByName("ArgumentsJson");
var formattedJson = (string)traceEvent.PayloadByName("FormattedMessage"); // gives message.ToString (getting formatted message from message event)
Console.WriteLine(formattedJson);
// There's a bug that causes some of the columns to get mixed up
if (eventName.StartsWith("{"))
{
argsJson = exceptionJson;
exceptionJson = eventName;
eventName = null;
}
if (string.IsNullOrEmpty(argsJson))
{
return;
}
});
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
session.Stop();
e.Cancel = true;
};
source.Process();
}
catch (DiagnosticsClientException e)
{
Console.Error.WriteLine($"Unable to start a tracing session: {e.ToString()}");
}
if (session == null)
{
Console.Error.WriteLine("Unable to create session.");
return;
}
}
}
internal static class Keywords
{
public const EventKeywords Meta = (EventKeywords)1;
public const EventKeywords Message = (EventKeywords)2;
public const EventKeywords FormattedMessage = (EventKeywords)4;
public const EventKeywords JsonMessage = (EventKeywords)8;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment