PerformanceLogger is a tool for Microsoft .NET Standard 2.0 to help with logging the performance (execution times) of any .NET application at the method level. The library comes with a simple API and an easy-to-extend architecture allowing writing extensions to log to different targets. Support Microsoft.Extensions.ILogger and Postgres database as logging targets. It is designed to have a low impact on performance, enabling detailed monitoring performance on live production systems. Used in Continuous Integration or local developement, it enables measuring the impacts of design decisions.
Get the packages with NuGet.
To log to an ILogger dotnet add package PerformanceLogger.Extensions.Logging
To log to an Postgres database dotnet add package PerformanceLogger.Extensions.Postgres
To extend and implement your own targets dotnet add package PerformanceLogger
See our Sample Project for an example of usage of the PerformanceLogger with an ILogger and a Postgres database, in a console application.
// Get an instance of your ILogger
var logger = serviceProvider.GetService<ILogger>();
// Build a performance logger that will log into the ILogger
var performanceLogger = new PerformanceLoggerBuilder()
.AddLogger(logger)
.Build();
// Start tracking a long operation
var perfTracker = performanceLogger.Start("my_long_task_01");
// Simulating a long operation
Thread.Sleep(25);
// End the tracking (and log the results)
perfTracker.End();
In the case of a console logger, the results will look like this :
You are free to setup an ILogger with NLog, log4net, Serilog or any other adapter.
All the performance logs go to a dedicated table of which you can select the name, and defaults to perflogs
. The table will be created if it does not already exists.
var connectionString = "Host=localhost;Username=postgres;Password=MYPASSWORD;Database=performancelogs";
var tableName = "perflogs";
// Build a performance logger that will log into a Postgres database
var performanceLogger = new PerformanceLoggerBuilder()
.AddPostgres(connectionString, tableName)
.Build();
The PostresTarget writes the logs to the database by batches, in a parallel Task for a lower performance impact. Note that you must properly Dispose()
the performanceLogger
(blocking call) to ensure not losing the last logs in the queue when exiting an application.
As many targets as needed can be used simultaneously to log the application performance with the builder, as follows
// Build a performance logger that will log into two databases, and one ILogger
var performanceLogger = new PerformanceLoggerBuilder()
.AddPostgres(connectionString1, "logs")
.AddPostgres(connectionString2, "perflogs")
.AddLogger(logger1)
.Build();
TODO: extend Microsoft's dependency injection mechanism
It is possible to extend the base package and write your own target for the performance logs. Write your own implementation of ITarget
as follows
// Implementation of performance logging target which
// writes the logs into the Console with Console.WriteLine()
public class MyConsoleTarget : ITarget
{
public void Log(PerformanceResult report)
{
// Format the entry
var entry = $"{report.StartDate.ToShortDateString()} " +
$"{report.StartDate.ToShortTimeString()};" +
$"{report.EventId};{report.Duration.TotalMilliseconds} ms;";
// Log it
Console.WriteLine(entry);
}
public void Dispose() { }
}
Then use this target with the PerformanceLoggerBuilder
var consoleTarget = new MyConsoleTarget();
var performanceLogger = new PerformanceLoggerBuilder()
.AddTarget(consoleTarget)
.Build();
You may extend the builder to add a method specific to your new target as well
public static class PerformanceLoggerBuilderExtension
{
public static PerformanceLoggerBuilder AddConsoleLogger(this PerformanceLoggerBuilder builder)
{
var consoleTarget = new MyConsoleTarget();
return builder.AddTarget(consoleTarget);
}
}
And then use it as follows
var performanceLogger = new PerformanceLoggerBuilder()
.AddConsoleLogger()
.Build();