Skip to content

Simplest way to listen and collect events for .Net/.Net Core applications.

License

Notifications You must be signed in to change notification settings

btungut/SimpleTracer

Repository files navigation

SimpleTracer

SimpleTracer

Simplest way to listen and collect events for .Net/.Net Core applications.!

Build Test Coverage Nuget
Build Status Azure DevOps tests Azure DevOps coverage Nuget

If you have an application that you need to listen some events, you should implement the followings which is fairly painful;

  • Create a class that inherits from EventListener
  • Call EnableEvents(...) method for every source and try to eliminate the concurrency problem
  • To listen multiple different sources, be familiar with bitwise operations

Here we go! OnEventWritten(...) method will be triggered for every written events and you should concern;

  • Concurrency because events might be written from different threads
  • Buffering because events count per specific time might be more than you can handle this.
  • Separation because you wouldn't like to handle every event in same way

SimpleTracer addresses many of the common concern to provide fault tolerant, thread safe and TPL friendly solution.

Simplest way to collect Garbage Collection events, ever!

You can configure how often and which method will be triggered by calling WithInterval and WithDelegate methods. Also WithMaxTake could be handy to set event limit for each trigger.

var builder = SubscriptionContainerBuilder
    .New()
    .WithSubscription(s => s
        .WithEvents(e => e.DotNetRuntime.GC.AllInformationals())
        .WithOptions()
        .WithExecution(e => e
            .WithInterval(TimeSpan.FromSeconds(30))
            .WithDelegate(OnEventsCollected)
            .WithMaxTake(300)));

var container = builder.Build();
container.Start();

You could store it wherever you want

SimpleTracer supports awaitable methods. Every execution occurs in a Task which was dedicated per Subscription for your workload!

private static Task OnEventsCollected(IEventNotification arg)
{
    foreach (IEventEntry item in arg.Events)
        Console.WriteLine($"{item.CreatedOn}\t{item.Id} : {item.Name}");

    return Task.CompletedTask;
}

Multiple EventSource in one Subscription

You don't have to create different subscriptions for every EventSource. One subscription can listen multiple EventSource with various EventLevel and EventKeywords.

 var builder = SubscriptionContainerBuilder
     .New()
     .WithSubscription(s => s
         .WithEvents(e => e.DotNetRuntime.GC.AllInformationals())
         .WithEvents(e => e.SystemNetHttp.AllWarnings())
         .WithEvents(new SourceDefinition("Custom-Event-Source", EventLevel.Error, EventKeywords.All))
         .WithExecution(e => e
             .WithInterval(TimeSpan.FromSeconds(3))
             .WithDelegate(OnEventsCollected)
             .WithMaxTake(300))
         .WithOptions());

var container = builder.Build();
container.Start();

Don't want to involve too much details?

You can create a Subscription to listen all of EventSource objects with EventLevel filter. It gives you to a massive insight about your application but also more than a thousand events to be queued.

Don't worry! By calling WithQueueCapacity, it is possible to limit to be enqueued event count.

var builder = SubscriptionContainerBuilder
    .New()
    .WithSubscription(EventLevel.Error, c => c
         .WithExecution(e => e
            .WithInterval(TimeSpan.FromSeconds(3))
            .WithDelegate(OnEventsCollected)
            .WithMaxTake(300))
        .WithOptions(o => o
            .WithQueueCapacity(3000)));

var container = builder.Build();
container.Start();

About

Simplest way to listen and collect events for .Net/.Net Core applications.

Topics

Resources

License

Stars

Watchers

Forks

Languages