Giter Club home page Giter Club logo

diagnostic.extensions's Introduction

Diagnostic.Extensions

Delegates.Extensions.Diagnostics

What did it do.

public static Action Diagnostic(this Action action, string? category = null, object? data = null)
{
    DiagnosticListener.Write("Delegates.Extensions.{category}.Preparing");
    Action wrapped = () =>
    {
        DiagnosticListener.Write("Delegates.Extensions.{category}.Executing");
        try
        {
            action();
            DiagnosticListener.Write("Delegates.Extensions.{category}.Succeed");
        }
        catch (Exception ex)
        {
            DiagnosticListener.Write("Delegates.Extensions.{category}.Exceptional");
            throw;
        }
        finally
        {
            DiagnosticListener.Write("Delegates.Extensions.{category}.Finished");
        }
    };
    DiagnosticListener.Write("Delegates.Extensions.{category}.Prepared");

    return wrapped;
}

What you can do.

Task.Run(((Action)Call).Diagnostic("Timing"));
// or
Task.Run(new DiagnosticAction("Timing", Call));


void Call() { }

internal class Listener : IObserver<KeyValuePair<string, object>>
{
    public void OnNext(KeyValuePair<string, object> value)
    {
        switch (value.Key)
        {
            case "Delegates.Extensions.Timing.Preparing":
                ((PreparingContext)value.Value).Cabinet["prepare"] = Stopwatch.StartNew();;
                break;
            case "Delegates.Extensions.Timing.Executing":
                var stopwatch = (Stopwatch)((ExecutingContext)value.Value).Cabinet["prepare"];
                stopwatch.Stop();
                Console.WriteLine($"enqueue task costs {prepare.ElapsedMilliseconds}ms");
                break;
        }
    }
}

Wrap Func<Task> or Func<Task<T>>

// define a async Method
public async Task<int> CallAsync(int delay)
{
    await Task.Delay(delay);
    await new HttpClient().GetAsync("https://github.com");
    return delay;
}

// await directly
await CallAsync(100);

// without await
CallAsync(100);

// await with wrap
await new DiagnosticFunc<Task<int>>("my-category", () => CallAsync(100));

// without await but wrap
new DiagnosticFunc<Task<int>>("my-category", () => CallAsync(100)).Invoke();

diagnostic.extensions's People

Contributors

inversionhourglass avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.