MiniProfiler / dotnet

A simple but effective mini-profiler for ASP.NET (and Core) websites
https://miniprofiler.com/dotnet/
MIT License
2.93k stars 603 forks source link

How to profile a class library #676

Open AlbertoVPersonal opened 1 month ago

AlbertoVPersonal commented 1 month ago

Current version MiniProfiler

4.3.8

Current version .NET

.NET Core 8.0.4

Expected behaviour

I would like to profile a class library internally.

My class is similar to:

public class StateManagement() : IStateManagement
{
    /// <summary>Sets the state asynchronous.</summary>
    /// <param name="statusKey">The status key.</param>
    /// <param name="statusValue">The status value.</param>
    /// <returns></returns>
    public async Task<bool> SetStateAsync(string statusKey, object statusValue)
    {
        bool result = false;

        try
        {
            // custom code

            result = true;
        }
        catch (Exception stateEx)
        {
            Console.WriteLine(stateEx);
        }

        return result;
    }

    //...
}

Workaround

I think that the solution is not the better.

public class StateManagement() : IStateManagement
{
    /// <summary>Sets the state asynchronous.</summary>
    /// <param name="statusKey">The status key.</param>
    /// <param name="statusValue">The status value.</param>
    /// <returns></returns>
    public async Task<bool> SetStateAsync(MiniProfiler profiler, string statusKey, object statusValue)
    {
        bool result = false;

        try
        {
            using (profiler.Step("Bulk op"))
            {
                // custom code
            }
            result = true;
        }
        catch (Exception stateEx)
        {
            Console.WriteLine(stateEx);
        }

        return result;
    }

    //...
}