Created
July 30, 2020 22:23
-
-
Save maryamariyan/82fd31a8fa65f61a52273ed678b3e667 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.EventSource; | |
using System; | |
using System.Collections.Generic; | |
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() | |
.AddConsole() | |
//.AddFilter<EventSourceLoggerProvider>("*", LogLevel.Warning) | |
.AddFilter("Component1*", LogLevel.Warning) | |
//.AddFilter<EventSourceLoggerProvider>("C*", LogLevel.Warning) | |
; | |
}); | |
var logger1 = loggerFactory.CreateLogger("Component11"); | |
var logger2 = loggerFactory.CreateLogger("Component22"); | |
while (true) | |
{ | |
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 MyEventListener : EventListener | |
{ | |
protected override void OnEventSourceCreated(EventSource eventSource) | |
{ | |
if (eventSource.Name == "Microsoft-Extensions-Logging") | |
{ | |
EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)0x4 | |
, new Dictionary<string, string?>() | |
{ | |
{ "FilterSpecs", "" } | |
}); | |
} | |
} | |
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]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment