Created
July 9, 2024 19:51
-
-
Save CheetahChrome/6ede3d643e156231a2bc195f0aee2a12 to your computer and use it in GitHub Desktop.
Logging Helper For BeginScope Log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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