A lightweight extension library for Microsoft.Extensions.Logging that provides simple and efficient time measurement capabilities for code blocks.
Note: This library is designed for quick performance insights and identifying potential bottlenecks. For comprehensive performance analysis, consider using professional profiling tools.
dotnet add package NetEvolve.Logging.MeasurementThe library provides the StartMeasurement extension method for ILogger instances. Measurements are automatically logged when the returned scope is disposed.
using Microsoft.Extensions.Logging;
using NetEvolve.Logging.Measurement;
public sealed class DataProcessor
{
private readonly ILogger<DataProcessor> _logger;
public DataProcessor(ILogger<DataProcessor> logger)
{
_logger = logger;
}
public void ProcessData()
{
using (_logger.StartMeasurement("Data Loading"))
{
// Load data from database or file
// ...
}
using (_logger.StartMeasurement("Data Transformation"))
{
// Transform and validate data
// ...
}
using (_logger.StartMeasurement("Data Persistence"))
{
// Save processed data
// ...
}
}
}By default, measurements are logged at Information level. You can customize the completion log level:
// Log at Debug level
using (_logger.StartMeasurement("Background Task", LogLevel.Debug))
{
// Background processing
}
// Log at Warning level for critical sections
using (_logger.StartMeasurement("Critical Operation", LogLevel.Warning))
{
// Critical code
}
// Disable logging completely (measurement still happens)
using (_logger.StartMeasurement("Silent Operation", LogLevel.None))
{
// No log output
}Enable detailed caller information for debugging purposes:
// Print caller information (method name, file path, line number)
using (_logger.StartMeasurement(
"Detailed Operation",
printDebugInformation: true))
{
// Your code here
}
// By default (null), debug information is only printed on exceptions
using (_logger.StartMeasurement("Standard Operation"))
{
// Debug info appears only if an exception occurs
}Measurements can be nested to analyze hierarchical operations:
using (_logger.StartMeasurement("Complete Workflow"))
{
using (_logger.StartMeasurement("Step 1: Initialization"))
{
// Initialize resources
}
using (_logger.StartMeasurement("Step 2: Processing"))
{
// Process data
}
using (_logger.StartMeasurement("Step 3: Finalization"))
{
// Clean up
}
}public static IDisposable StartMeasurement(
this ILogger logger,
string identifier,
LogLevel? completionLevel = null,
bool? printDebugInformation = null,
[CallerMemberName] string callerMemberName = "",
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int callerLineNumber = 0)Parameters:
logger- TheILoggerinstance to use for loggingidentifier- A descriptive name for the measured operationcompletionLevel- Optional log level for completion message (default:Information, useNoneto disable logging)printDebugInformation- Optional flag to print caller information (default:null= print only on exceptions)callerMemberName- Automatically captured calling method namecallerFilePath- Automatically captured source file pathcallerLineNumber- Automatically captured line number
Returns: An IDisposable that completes the measurement when disposed.
-
Use Descriptive Identifiers: Choose clear, meaningful names for your measurements
// Good using (_logger.StartMeasurement("Customer Order Processing")) // Avoid using (_logger.StartMeasurement("Operation1"))
-
Appropriate Log Levels: Match log levels to operation importance
Debug: Detailed diagnostic measurementsInformation: Standard operation measurements (default)Warning: Critical performance sectionsNone: Measurement without logging
-
Minimize Measurement Overhead: Don't measure trivial operations in tight loops
// Good: Measure the entire batch using (_logger.StartMeasurement("Process 1000 Items")) { foreach (var item in items) { ProcessItem(item); } } // Avoid: Measuring each iteration foreach (var item in items) { using (_logger.StartMeasurement($"Process Item {item.Id}")) { ProcessItem(item); } }
-
Exception Safety: Measurements are automatically completed even if exceptions occur
using (_logger.StartMeasurement("Risky Operation")) { // Measurement logs elapsed time even if an exception is thrown RiskyMethod(); }
- .NET 8.0
- .NET 9.0
- .NET 10.0
- Microsoft.Extensions.Logging.Abstractions >= 10.0.0
- NetEvolve.Arguments >= 2.0.0
This project is licensed under the MIT License.