MethodWatch 1.0.1

Additional Details

Deleted

There is a newer version of this package available.
See the version list below for details.
dotnet add package MethodWatch --version 1.0.1
                    
NuGet\Install-Package MethodWatch -Version 1.0.1
                    
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="MethodWatch" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MethodWatch" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MethodWatch" />
                    
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 MethodWatch --version 1.0.1
                    
#r "nuget: MethodWatch, 1.0.1"
                    
#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 MethodWatch@1.0.1
                    
#: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=MethodWatch&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MethodWatch&version=1.0.1
                    
Install as a Cake Tool

MethodWatch

A lightweight .NET library for method execution time monitoring and logging. MethodWatch provides both automatic and manual method timing capabilities with customizable thresholds and real-time statistics.

Features

  • Automatic Method Timing: Use the [MethodWatch] attribute to automatically time method execution
  • Manual Method Timing: Use the MethodWatch.Measure() method for manual timing of code blocks
  • Customizable Thresholds: Set performance thresholds for each method
  • Real-time Statistics: Track execution times, min/max values, and failure rates
  • Web UI: Visualize method performance with a built-in web interface
  • Exception Handling: Automatic timing of methods that throw exceptions
  • Configurable Statistics: Enable/disable statistics collection for better performance

Installation

dotnet add package MethodWatch

Usage

Basic Setup

Add MethodWatch to your application:

// In Program.cs
MethodWatch.MethodWatch.Initialize(loggerFactory, enableStatistics: true);

Automatic Method Timing

Add the [MethodWatch] attribute to any method you want to monitor:

// Basic usage - all times will be logged
[MethodWatch(0)]
public void FastOperation()
{
    // Method implementation
}

// Set threshold to 100ms - times >= 100ms will be marked as slow
[MethodWatch(100)]
public void SlowOperation()
{
    Thread.Sleep(200);
}

// Set threshold to 50ms - times >= 50ms will be marked as slow
[MethodWatch(50)]
public void RandomOperation()
{
    Thread.Sleep(Random.Shared.Next(50, 300));
}

Manual Method Timing

Use the MethodWatch.Measure() method to time specific code blocks:

// Simple usage with custom name
using (MethodWatch.Measure("CustomOperation"))
{
    // Your code here
}

// With threshold - times >= 100ms will be marked as slow
using (MethodWatch.Measure("CustomOperation", 100))
{
    // Your code here
}

// Nested measurements with different thresholds
using (MethodWatch.Measure("OuterOperation", 200))
{
    // Outer operation code
    using (MethodWatch.Measure("InnerOperation", 50))
    {
        // Inner operation code
    }
}

Web UI

MethodWatch includes a web interface to visualize method performance:

  1. Add the web UI to your project:
app.UseStaticFiles();
app.MapFallbackToFile("methodwatch.html");
  1. Access the UI at /methodwatch.html

Features:

  • Real-time statistics updates
  • Search and filter methods
  • Sort by various metrics
  • Color-coded performance indicators
  • Pagination for large datasets

Configuration

Enable/Disable Statistics

Control statistics collection through configuration:

{
  "MethodWatch": {
    "EnableStatistics": false
  }
}

Or in code:

MethodWatch.MethodWatch.Initialize(loggerFactory, enableStatistics: false);
Logging Configuration

Configure logging to be more concise:

builder.Logging.AddConsole(options =>
{
    options.IncludeScopes = false;
    options.TimestampFormat = "[HH:mm:ss] ";
});

Examples

Basic Usage

[MethodWatch(0)]
public void SimpleMethod()
{
    // Method implementation
}

With Threshold

[MethodWatch(100)]
public void MethodWithThreshold()
{
    // Method implementation
}

Manual Timing

public void ComplexOperation()
{
    using (MethodWatch.Measure("ComplexOperation", 50))
    {
        // Complex operation implementation
    }
}

Nested Measurements

public void NestedOperations()
{
    using (MethodWatch.Measure("OuterOperation", 200))
    {
        // Some work
        using (MethodWatch.Measure("InnerOperation", 50))
        {
            // Inner work
        }
        // More work
    }
}

Parallel Operations

[MethodWatch(0)]
public async Task ParallelOperations()
{
    using (MethodWatch.Measure("MainOperation", 200))
    {
        var tasks = new List<Task>();
        for (int i = 0; i < 10; i++)
        {
            var taskId = i;
            tasks.Add(Task.Run(async () =>
            {
                using (MethodWatch.Measure($"ParallelTask_{taskId}", 100))
                {
                    await Task.Delay(Random.Shared.Next(50, 300));
                }
            }));
        }
        await Task.WhenAll(tasks);
    }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on MethodWatch:

Package Downloads
MethodWatch.Web

Web interface for MethodWatch statistics

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 421 6/8/2025 1.1.0 is deprecated.
1.0.2 403 6/8/2025 1.0.2 is deprecated.
1.0.1 394 6/8/2025 1.0.1 is deprecated.
1.0.0 395 6/8/2025 1.0.0 is deprecated.