NetEvolve.Logging.Measurement 2.0.42

Prefix Reserved
dotnet add package NetEvolve.Logging.Measurement --version 2.0.42
                    
NuGet\Install-Package NetEvolve.Logging.Measurement -Version 2.0.42
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetEvolve.Logging.Measurement" Version="2.0.42" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetEvolve.Logging.Measurement" Version="2.0.42" />
                    
Directory.Packages.props
<PackageReference Include="NetEvolve.Logging.Measurement" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetEvolve.Logging.Measurement --version 2.0.42
                    
#r "nuget: NetEvolve.Logging.Measurement, 2.0.42"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetEvolve.Logging.Measurement@2.0.42
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetEvolve.Logging.Measurement&version=2.0.42
                    
Install as a Cake Addin
#tool nuget:?package=NetEvolve.Logging.Measurement&version=2.0.42
                    
Install as a Cake Tool

NetEvolve.Logging.Measurement

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.

Installation

dotnet add package NetEvolve.Logging.Measurement

Basic Usage

The 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
            // ...
        }
    }
}

Advanced Usage

Custom Log Levels

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
}

Debug Information

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
}

Nested Measurements

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
    }
}

API Reference

StartMeasurement Method

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 - The ILogger instance to use for logging
  • identifier - A descriptive name for the measured operation
  • completionLevel - Optional log level for completion message (default: Information, use None to disable logging)
  • printDebugInformation - Optional flag to print caller information (default: null = print only on exceptions)
  • callerMemberName - Automatically captured calling method name
  • callerFilePath - Automatically captured source file path
  • callerLineNumber - Automatically captured line number

Returns: An IDisposable that completes the measurement when disposed.

Best Practices

  1. Use Descriptive Identifiers: Choose clear, meaningful names for your measurements

    // Good
    using (_logger.StartMeasurement("Customer Order Processing"))
    
    // Avoid
    using (_logger.StartMeasurement("Operation1"))
    
  2. Appropriate Log Levels: Match log levels to operation importance

    • Debug: Detailed diagnostic measurements
    • Information: Standard operation measurements (default)
    • Warning: Critical performance sections
    • None: Measurement without logging
  3. 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);
        }
    }
    
  4. 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();
    }
    

Supported Frameworks

  • .NET 8.0
  • .NET 9.0
  • .NET 10.0

Dependencies

  • Microsoft.Extensions.Logging.Abstractions >= 10.0.0
  • NetEvolve.Arguments >= 2.0.0

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.42 97 12/31/2025
1.1.21 212 1/1/2025
1.1.0 173 12/3/2024
1.0.64 210 9/11/2024
1.0.51 184 8/26/2024
1.0.0 182 5/23/2024