Skip to content

Instantly share code, notes, and snippets.

@maryamariyan
Created July 30, 2020 23:51
Show Gist options
  • Save maryamariyan/53dfc99a27e9f74fca619bd2ee1e9dbd to your computer and use it in GitHub Desktop.
Save maryamariyan/53dfc99a27e9f74fca619bd2ee1e9dbd to your computer and use it in GitHub Desktop.
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]);
}
}
}
}
@maryamariyan
Copy link
Author

Output before PR:

MEL Event: FormattedMessage LogAlways
    Level = 5
    FactoryID = 1
    LoggerName = Component33
    EventId = 0
    EventName =
    FormattedMessage = LogCritical3

After PR:
Using "Component33:5" as FilterSpec the output is the same.
But when using "UseAppFilters;Component33:5" as the FilterSpec we get:

MEL Event: FormattedMessage LogAlways
    Level = 5
    FactoryID = 1
    LoggerName = Component11
    EventId = 0
    EventName =
    FormattedMessage = LogCritical1
MEL Event: FormattedMessage LogAlways
    Level = 4
    FactoryID = 1
    LoggerName = Component11
    EventId = 0
    EventName =
    FormattedMessage = LogError1
MEL Event: FormattedMessage LogAlways
    Level = 3
    FactoryID = 1
    LoggerName = Component11
    EventId = 0
    EventName =
    FormattedMessage = LogWarning1
MEL Event: FormattedMessage LogAlways
    Level = 5
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogCritical2
MEL Event: FormattedMessage LogAlways
    Level = 4
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogError2
MEL Event: FormattedMessage LogAlways
    Level = 5
    FactoryID = 1
    LoggerName = Component33
    EventId = 0
    EventName =
    FormattedMessage = LogCritical3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment