Created
July 30, 2020 02:28
-
-
Save maryamariyan/84f0b6f98d065ec42e1d88e88cd3520e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Logging.Console; | |
using Microsoft.Extensions.Logging.EventSource; | |
using System; | |
using System.Diagnostics.Tracing; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp46 | |
{ | |
class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
MyEventListener listener = new MyEventListener(); | |
using var loggerFactory = LoggerFactory.Create(builder => | |
{ | |
builder | |
.AddEventSourceLogger() | |
.AddFilter<EventSourceLoggerProvider>("Component1", LogLevel.Warning) | |
.AddFilter<EventSourceLoggerProvider>("Component2", LogLevel.Warning) | |
; | |
}); | |
var logger = loggerFactory.CreateLogger<Program>(); | |
var logger1 = loggerFactory.CreateLogger("Component1"); | |
var logger2 = loggerFactory.CreateLogger("Component2"); | |
while (true) | |
{ | |
logger.LogCritical("LogCritical"); | |
logger.LogDebug("LogDebug"); | |
logger.LogError("LogError"); | |
logger.LogInformation("LogInformation"); | |
logger.LogTrace("LogTrace"); | |
logger.LogWarning("LogWarning"); | |
logger1.LogCritical("LogCritical1"); | |
logger1.LogDebug("LogDebug1"); | |
logger1.LogError("LogError1"); | |
logger1.LogInformation("LogInformation1"); | |
logger1.LogTrace("LogTrace1"); | |
logger1.LogWarning("LogWarning1"); | |
logger2.LogCritical("LogCritical2"); | |
logger2.LogDebug("LogDebug2"); | |
logger2.LogError("LogError2"); | |
logger2.LogInformation("LogInformation2"); | |
logger2.LogTrace("LogTrace2"); | |
logger2.LogWarning("LogWarning2"); | |
Thread.Sleep(1000); | |
} | |
} | |
} | |
class Dummy { } | |
class MyEventListener : EventListener | |
{ | |
protected override void OnEventSourceCreated(EventSource eventSource) | |
{ | |
if (eventSource.Name == "Microsoft-Extensions-Logging") | |
{ | |
// MEL keywords | |
// 0x1 = Meta | |
// 0x2 = Message | |
// 0x4 = FormattedMessage | |
// 0x8 = MessageJson | |
EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)0x4, new System.Collections.Generic.Dictionary<string, string?>() | |
{ | |
{ "FilterSpecs", "*:1" } | |
}); | |
} | |
} | |
protected override void OnEventWritten(EventWrittenEventArgs eventData) | |
{ | |
Console.WriteLine("MEL Event: " + eventData.EventName + " " + eventData.Level); | |
for (int i = 0; i < eventData.Payload.Count; i++) | |
{ | |
Console.WriteLine(" " + eventData.PayloadNames[i] + " = " + eventData.Payload[i]); | |
if (eventData.Payload[i] is object[]) | |
{ | |
foreach (var obj in (object[])eventData.Payload[i]) | |
{ | |
Console.WriteLine(" " + obj.ToString()); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment