| | 1 | | using System.Collections.Concurrent; |
| | 2 | |
|
| | 3 | | namespace Songhay.Diagnostics; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Singleton wrapper for <see cref="TraceSource"/> |
| | 7 | | /// </summary> |
| | 8 | | /// <remarks> |
| | 9 | | /// Based on Fonlow.Diagnostics by Zijian Huang [https://github.com/zijianhuang/Fonlow.Diagnostics] |
| | 10 | | /// Also see “Use TraceSource Efficiently” [http://www.codeproject.com/Tips/1071853/Use-TraceSource-Efficiently] |
| | 11 | | /// </remarks> |
| | 12 | | public class TraceSources |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// The conventional <see cref="TraceSource"/> name. |
| | 16 | | /// </summary> |
| | 17 | | public const string DefaultTraceSourceName = "rx-trace"; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// The configured trace source name |
| | 21 | | /// </summary> |
| | 22 | | public static string? ConfiguredTraceSourceName |
| | 23 | | { |
| 14 | 24 | | get => _configuredTraceSourceName; |
| | 25 | | set |
| 9 | 26 | | { |
| 9 | 27 | | _configuredTraceSourceName = value; |
| 9 | 28 | | IsConfiguredTraceSourceNameLoaded = true; |
| 9 | 29 | | } |
| | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The is configured trace source name loaded? |
| | 34 | | /// </summary> |
| 11 | 35 | | public static bool IsConfiguredTraceSourceNameLoaded { get; private set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Prevents a default instance of the <see cref="TraceSources"/> class from being created. |
| | 39 | | /// </summary> |
| 2 | 40 | | TraceSources() => _traceSources = new ConcurrentDictionary<string, TraceSource>(); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the name of the trace source from configuration. |
| | 44 | | /// </summary> |
| | 45 | | /// <remarks> |
| | 46 | | /// When the trace source name is not configured |
| | 47 | | /// then <see cref="DefaultTraceSourceName"/> is used. |
| | 48 | | /// </remarks> |
| 14 | 49 | | public TraceSource? GetTraceSourceFromConfiguredName() => GetTraceSource(ConfiguredTraceSourceName); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Gets the trace source. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="name">The name.</param> |
| | 55 | | public TraceSource? GetTraceSource(string? name) |
| 14 | 56 | | { |
| 16 | 57 | | if (string.IsNullOrWhiteSpace(name)) return null; |
| 22 | 58 | | if (_traceSources.TryGetValue(name, out TraceSource? r)) return r; |
| | 59 | |
|
| 2 | 60 | | r = new TraceSource(name); |
| 2 | 61 | | _traceSources.TryAdd(name, r); |
| 2 | 62 | | return r; |
| 14 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the <see cref="TraceSource"/> with the specified name. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="name">The name.</param> |
| 0 | 69 | | public TraceSource? this[string? name] => GetTraceSource(name); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets the instance. |
| | 73 | | /// </summary> |
| 14 | 74 | | public static TraceSources Instance => Nested.NestedInstance; |
| | 75 | |
|
| | 76 | | readonly ConcurrentDictionary<string, TraceSource> _traceSources; |
| | 77 | |
|
| | 78 | | static class Nested |
| | 79 | | { |
| 1 | 80 | | internal static readonly TraceSources NestedInstance = new(); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | static string? _configuredTraceSourceName; |
| | 84 | | } |