You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP

SerilogMetrics2

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

SerilogMetrics2

Provides utilities to facilitate metrics (timers, counters, gauges, meters, healthchecks) on top of Serilog. SerilogMetrics2 extends the original SerilogMetrics to provide task timing and unscoped section timing.

2.1.7
Version published
Maintainers
1
Created

SerilogMetrics2 Build status NuGet

Serilog combines the best features of traditional and structured diagnostic logging in an easy-to-use package and Serilog.Metrics extends this logging framework with measure capabilities like counters, timers, meters and gauges.

Get started

To quickly get started, add the SerilogMetrics2 package to your solution using the NuGet Package manager or run the following command in the Package Console Window:

Install-Package SerilogMetrics2

The metrics method extensions are extending the ILogger interface of Serilog. So just reference the Serilog namespace and you can invoke the functionality from the logger. SerilogMetrics2 extends functionality of SerilogMetrics which doesn't seem to be supported any longer. The following extension methods are added:

public static IDisposable BeginUnscopedTimedOperation(string description, string id)

This method is exactly like BeginTimedOperation except it does not require that the scope of the timing be marked with braces. The EndTimedOperation() method may be called from anywhere in the code as long as the logger is in scope.

This method takes the same arguments as BeginTimedOperation but it requires a unique ID as a string, which will be used when EndTimedOperation is called. See example below.

e.g.

logger.BeginUnscopedTimedOperation("Time a thread sleep for 2 seconds.", "myID");
var t = Task.Run(() => MyLongRunningTask("Task") );
// Lots of other code executing while task is running
// ...
// ...
t.Wait();
// EndTimedOperation must be called with the same string ID used above.
Logger.EndTimedOperation("any message", "myID"); // Note: SAME ID as above

// Note: ID must match the call to BeginUnscopedTimedOperation
public static EndTimedOperation(string description, string id); 

public static IDisposable LogTaskExecutionTime(Task t, string description, string uniqueID)

This method takes the same arguments as BeginTimedOperation but also requires a Task task and string ID which is unique to this call. This is a "set and forget" method which creates an awaiter for the task and when the task finishes, it will log the time spent performing the task.

e.g.

static void LongRunningTask(String s)
{
    Thread.Sleep(2500);
}

Task t = Task.Run(() => LongRunningTask("SerilogMetrics2"));    // no await
logger.LogTaskExecutionTime(t, "ExampleMethodAsync", "myid1");  // will log when above task completes

For example;

var logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Trace()
                .CreateLogger();

using (logger.BeginTimedOperation("Time a thread sleep for 2 seconds."))
{
     Thread.Sleep(2000);
}

See the documentation for more details.

Copyright © 2016 Serilog Metrics Contributors - Provided under the Apache License, Version 2.0.

FAQs

Package last updated on 03 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts