Coder Social home page Coder Social logo

psibr / contextually Goto Github PK

View Code? Open in Web Editor NEW
9.0 6.0 6.0 578 KB

A dead simple contextual block pattern library that works, even with async/await and threads.

C# 91.24% CSS 0.57% JavaScript 0.03% HTML 8.16%
logging dotnet dotnetcore netstandard net46 asynclocal nuget csharp

contextually's Introduction

Contextually

Enables contextual information to flow with your code:

using System;
using Contextually;

namespace Sample
{
  class App
  {
    static void Main(string[] args)
    {
      // Setup top level context of the entire app.
      var systemContext = new NameValueCollection
      {
        ["MachineName"] = System.Environment.MachineName
      };
      
      using (Relevant.Info(systemContext))
      {
        // Shows that the context flows even if you start a new thread.
        Task.Run(AppMainLoop).Wait();
      }
      // This is the end of the context scope.
    }
    
    void AppMainLoop()
    {
      Console.WriteLine("Enter your name:");
      var userName = Console.ReadLine();

      var appContext = new NameValueCollection
      {
        ["UserName"] = userName
      };
      
      // Setup a sub-context of relevant information.
      using (Relevant.Info(systemContext))
      {
        while (...)
          DoWork();
      }
      // End of the sub-scope. The "UserName" is not
      // available anymore, but 'MachineName' still is.
    }

    void DoWork()
    {
      try
      {
        ...
      }
      catch (Exception ex)
      {
        // Now you can collect all contextual information
        // from all open scopes and sub-scopes.
        var aggregatedContext = Relevant.Info();
        // This helps with analysis of 'dynamic' data,
        // which is not known at compile time.
        Logger.LogError(ex, aggregatedContext);
      }
    }
  }
}

contextually's People

Contributors

abalfour avatar dependabot[bot] avatar kind-serge avatar tyrotoxin avatar zoalasaurusrawr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

contextually's Issues

Feature Request: Allow custom Relevant instances

Currently, the static AsyncLocal<InfoBlock> is hidden behind static Relevant class. This works for a simple app. Once you want to build a more complex one, where multiple modules can use Contextually in different ways, this would become a problem due to absence of isolation of the contextual data of different modules.

My proposal is to make the Relevant class non-static, and allow custom instances of it. For example, I'm building a logging module that uses Contextually:

static class Log
{
    private static Relevant _relevant = new Relevant();

    public static IDisposable Info(NameValueCollection info)
        => _relevant .Info(info);
}
using (Log.Info(...))
{
  ...
}

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.