https://github.com/psibr/contextually
A dead simple contextual block pattern library that works, even with async/await and threads.
https://github.com/psibr/contextually
asynclocal csharp dotnet dotnetcore logging net46 netstandard nuget
Last synced: about 1 year ago
JSON representation
A dead simple contextual block pattern library that works, even with async/await and threads.
- Host: GitHub
- URL: https://github.com/psibr/contextually
- Owner: psibr
- Created: 2017-02-06T08:42:02.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-25T07:57:25.000Z (over 2 years ago)
- Last Synced: 2025-04-10T01:12:32.676Z (about 1 year ago)
- Topics: asynclocal, csharp, dotnet, dotnetcore, logging, net46, netstandard, nuget
- Language: C#
- Homepage:
- Size: 564 KB
- Stars: 9
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Contextually
Enables contextual information to _flow_ with your code:
```csharp
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);
}
}
}
}
```