Skip to content

Instantly share code, notes, and snippets.

@CheetahChrome
Created July 9, 2024 19:51
Show Gist options
  • Save CheetahChrome/6ede3d643e156231a2bc195f0aee2a12 to your computer and use it in GitHub Desktop.
Save CheetahChrome/6ede3d643e156231a2bc195f0aee2a12 to your computer and use it in GitHub Desktop.
Logging Helper For BeginScope Log
public static class LoggingExtensions
{
/// <summary>
/// Quick way to return dictionary for logging scope.
/// </summary>
/// <param name="key">Key to use</param>
/// <param name="value">Value to use</param>
/// <returns>Dictionary of one key value pair</returns>
public static Dictionary<string, object> ScopeIt(this string key, object value)
=> new Dictionary<string, object> { [key] = value };
public static IDisposable BeginScopeEx(this ILogger log, params string[] kvps)
{
var dict = new Dictionary<string, object>();
for (int i = 0; i < kvps.Length; i += 2)
dict.Add(kvps[i], kvps[i + 1]);
return log.BeginScope(dict);
}
public static IDisposable BeginScopeEx(this ILogger log, string key, object value)
=> log.BeginScope(key.ScopeIt(value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment