| | | 1 | | /******************************************************************************** |
| | | 2 | | * RouterEventSource.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Diagnostics.Tracing; |
| | | 9 | | |
| | | 10 | | namespace NanoRoute.Internals |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Exposes events from this library. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks>This logger is not meant to log user errors.</remarks> |
| | | 16 | | [EventSource(Name = EVENT_SOURCE_NAME)] |
| | | 17 | | internal sealed class RouterEventSource : EventSource |
| | | 18 | | { |
| | 2 | 19 | | private RouterEventSource() |
| | 2 | 20 | | { |
| | 2 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The name associated with the event source declaration. |
| | | 25 | | /// </summary> |
| | | 26 | | public const string EVENT_SOURCE_NAME = "NanoRoute"; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The singleton instance. |
| | | 30 | | /// </summary> |
| | 2 | 31 | | public static RouterEventSource Instance { get; } = new(); |
| | | 32 | | |
| | 2 | 33 | | public static EventSourceWriter Debug { get; } = new EventSourceWriter(Instance, EventLevel.Verbose); |
| | | 34 | | |
| | 2 | 35 | | public static EventSourceWriter Info { get; } = new EventSourceWriter(Instance, EventLevel.Informational); |
| | | 36 | | |
| | 2 | 37 | | public static EventSourceWriter Warning { get; } = new EventSourceWriter(Instance, EventLevel.Warning); |
| | | 38 | | |
| | 2 | 39 | | public static EventSourceWriter Error { get; } = new EventSourceWriter(Instance, EventLevel.Error); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Defines some extensions methods over the <see cref="EventSource"/> class. |
| | | 44 | | /// </summary> |
| | | 45 | | internal sealed class EventSourceWriter(EventSource target, EventLevel level) |
| | | 46 | | { |
| | | 47 | | private readonly EventSourceOptions _options = new() { Level = level }; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Logs a message with the given <see cref="Level"/>. The <paramref name="attributesFactory"/> is called only w |
| | | 51 | | /// </summary> |
| | | 52 | | [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The 'attributesFactory' won't return compos |
| | | 53 | | public void Write<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T, TParam>(strin |
| | | 54 | | { |
| | | 55 | | if (target.IsEnabled(Level, EventKeywords.None)) |
| | | 56 | | target.Write(eventName, _options, attributesFactory(p)); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Logs a message with the given <see cref="Level"/>. The <paramref name="attributesFactory"/> is called only w |
| | | 61 | | /// </summary> |
| | | 62 | | [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The 'attributesFactory' won't return compos |
| | | 63 | | public void Write<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T, TParam_1, TPa |
| | | 64 | | { |
| | | 65 | | if (target.IsEnabled(Level, EventKeywords.None)) |
| | | 66 | | target.Write(eventName, _options, attributesFactory(p1, p2)); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public EventLevel Level { get; } = level; |
| | | 70 | | |
| | | 71 | | public override string ToString() => Level.ToString(); |
| | | 72 | | } |
| | | 73 | | } |