Member-only story
Featured
Stop writing stopwatch code to everywhere, here is a story about automatic method profiling in .NET
Let me tell you about a problem I kept running into. You know that moment when your API starts slowing down, and you’re sitting there wondering which method is the slowdown.
The usual approach goes something like this: you wrap everything in Stopwatch, and log the results, and hope you remember to remove all that code before committing. Usually, we forget to remove some :)
Sound familiar?
I got tired of that pattern. So I built something better, and I want to share it with you.
The Problem We All Face
Performance issues are sneaky. They don’t announce themselves with big red error messages. Instead, your users start complaining that things feel slow, and you’re left debugging in production with limited visibility.
The traditional way of profiling methods looks like this
public void ProcessData()
{
var stopwatch = Stopwatch.StartNew();
try
{
// Your actual business logic
DoActualWork();
}
finally
{
stopwatch.Stop();
_logger.LogInformation($"ProcessData took {stopwatch.ElapsedMilliseconds}ms");
}
}





