| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Diagnostics.Tracing; |
| | 8 | | using System.Globalization; |
| | 9 | | using Azure.Core.Shared; |
| | 10 | |
|
| | 11 | | namespace Azure.Core.Diagnostics |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Implementation of <see cref="EventListener"/> that listens to events produces by Azure SDK Client libraries. |
| | 15 | | /// </summary> |
| | 16 | | public class AzureEventSourceListener: EventListener |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The trait name that has to be present on all event sources collected by this listener. |
| | 20 | | /// </summary> |
| | 21 | | public const string TraitName = "AzureEventSource"; |
| | 22 | | /// <summary> |
| | 23 | | /// The trait value that has to be present on all event sources collected by this listener. |
| | 24 | | /// </summary> |
| | 25 | | public const string TraitValue = "true"; |
| | 26 | |
|
| 18 | 27 | | private readonly List<EventSource> _eventSources = new List<EventSource>(); |
| | 28 | |
|
| | 29 | | private readonly Action<EventWrittenEventArgs, string> _log; |
| | 30 | | private readonly EventLevel _level; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Creates an instance of <see cref="AzureEventSourceListener"/> that executes a <paramref name="log"/> callbac |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="log">The <see cref="System.Action{EventWrittenEventArgs, String}"/> to call when event is writt |
| | 36 | | /// <param name="level">The level of events to enable.</param> |
| 18 | 37 | | public AzureEventSourceListener(Action<EventWrittenEventArgs, string> log, EventLevel level) |
| | 38 | | { |
| 18 | 39 | | _log = log ?? throw new ArgumentNullException(nameof(log)); |
| | 40 | |
|
| 18 | 41 | | _level = level; |
| | 42 | |
|
| 352 | 43 | | foreach (EventSource eventSource in _eventSources) |
| | 44 | | { |
| 158 | 45 | | OnEventSourceCreated(eventSource); |
| | 46 | | } |
| | 47 | |
|
| 18 | 48 | | _eventSources.Clear(); |
| 18 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc /> |
| | 52 | | protected sealed override void OnEventSourceCreated(EventSource eventSource) |
| | 53 | | { |
| 320 | 54 | | base.OnEventSourceCreated(eventSource); |
| | 55 | |
|
| 320 | 56 | | if (_log == null) |
| | 57 | | { |
| 158 | 58 | | _eventSources.Add(eventSource); |
| | 59 | | } |
| | 60 | |
|
| 320 | 61 | | if (eventSource.GetTrait(TraitName) == TraitValue) |
| | 62 | | { |
| 32 | 63 | | EnableEvents(eventSource, _level); |
| | 64 | | } |
| 320 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <inheritdoc /> |
| | 68 | | protected sealed override void OnEventWritten(EventWrittenEventArgs eventData) |
| | 69 | | { |
| | 70 | | // Workaround https://github.com/dotnet/corefx/issues/42600 |
| 10 | 71 | | if (eventData.EventId == -1) |
| | 72 | | { |
| 2 | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| 8 | 76 | | _log(eventData, EventSourceEventFormatting.Format(eventData)); |
| 8 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Creates a new instance of <see cref="AzureEventSourceListener"/> that forwards events to <see cref="Console. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="level">The level of events to enable.</param> |
| | 83 | | public static AzureEventSourceListener CreateConsoleLogger(EventLevel level = EventLevel.Informational) |
| | 84 | | { |
| 0 | 85 | | return new AzureEventSourceListener((eventData, text) => Console.WriteLine("[{1}] {0}: {2}", eventData.Event |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Creates a new instance of <see cref="AzureEventSourceListener"/> that forwards events to <see cref="Trace.Wr |
| | 90 | | /// </summary> |
| | 91 | | /// <param name="level">The level of events to enable.</param> |
| | 92 | | public static AzureEventSourceListener CreateTraceLogger(EventLevel level = EventLevel.Informational) |
| | 93 | | { |
| 2 | 94 | | return new AzureEventSourceListener( |
| 0 | 95 | | (eventData, text) => Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "[{0}] {1}", eventData. |
| | 96 | | } |
| | 97 | | } |
| | 98 | | } |