Skip to content

Metrics library for the .NET structured logging framework Serilog http://serilog.net

License

Notifications You must be signed in to change notification settings

bwedding/serilog-metrics2

 
 

Repository files navigation

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.

About

Metrics library for the .NET structured logging framework Serilog http://serilog.net

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.1%
  • PowerShell 1.9%