Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maryamariyan/f7dde115c092c47403b5b2cbb4e2105c to your computer and use it in GitHub Desktop.
Save maryamariyan/f7dde115c092c47403b5b2cbb4e2105c 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()
.AddFilter<EventSourceLoggerProvider>("*", LogLevel.Warning)
//.AddFilter<EventSourceLoggerProvider>("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", "Component22: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]);
}
}
}
}
@maryamariyan
Copy link
Author

output:

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 = 1
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogDebug2
MEL Event: FormattedMessage LogAlways
    Level = 4
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogError2
MEL Event: FormattedMessage LogAlways
    Level = 2
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogInformation2
MEL Event: FormattedMessage LogAlways
    Level = 3
    FactoryID = 1
    LoggerName = Component22
    EventId = 0
    EventName =
    FormattedMessage = LogWarning2

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