Created
July 30, 2020 23:51
-
-
Save maryamariyan/53dfc99a27e9f74fca619bd2ee1e9dbd 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("Component1*", LogLevel.Warning) | |
.AddFilter("Component2*", LogLevel.Error) | |
; | |
}); | |
var logger1 = loggerFactory.CreateLogger("Component11"); | |
var logger2 = loggerFactory.CreateLogger("Component22"); | |
var logger3 = loggerFactory.CreateLogger("Component33"); | |
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"); | |
logger3.LogCritical("LogCritical3"); | |
logger3.LogDebug("LogDebug3"); | |
logger3.LogError("LogError3"); | |
logger3.LogInformation("LogInformation3"); | |
logger3.LogTrace("LogTrace3"); | |
logger3.LogWarning("LogWarning3"); | |
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", "Component33:5" } | |
}); | |
} | |
} | |
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
Output before PR:
After PR:
Using "Component33:5" as FilterSpec the output is the same.
But when using "UseAppFilters;Component33:5" as the FilterSpec we get: