< Summary

Class:Azure.Core.Diagnostics.AzureEventSourceListener
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Diagnostics\AzureEventSourceListener.cs
Covered lines:19
Uncovered lines:2
Coverable lines:21
Total lines:98
Line coverage:90.4% (19 of 21)
Covered branches:13
Total branches:14
Branch coverage:92.8% (13 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%75%
OnEventSourceCreated(...)-100%100%
OnEventWritten(...)-100%100%
CreateConsoleLogger(...)-0%100%
CreateTraceLogger(...)-50%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Diagnostics\AzureEventSourceListener.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Diagnostics.Tracing;
 8using System.Globalization;
 9using Azure.Core.Shared;
 10
 11namespace 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
 1827        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>
 1837        public AzureEventSourceListener(Action<EventWrittenEventArgs, string> log, EventLevel level)
 38        {
 1839            _log = log ?? throw new ArgumentNullException(nameof(log));
 40
 1841            _level = level;
 42
 35243            foreach (EventSource eventSource in _eventSources)
 44            {
 15845                OnEventSourceCreated(eventSource);
 46            }
 47
 1848            _eventSources.Clear();
 1849        }
 50
 51        /// <inheritdoc />
 52        protected sealed override void OnEventSourceCreated(EventSource eventSource)
 53        {
 32054            base.OnEventSourceCreated(eventSource);
 55
 32056            if (_log == null)
 57            {
 15858                _eventSources.Add(eventSource);
 59            }
 60
 32061            if (eventSource.GetTrait(TraitName) == TraitValue)
 62            {
 3263                EnableEvents(eventSource, _level);
 64            }
 32065        }
 66
 67        /// <inheritdoc />
 68        protected sealed override void OnEventWritten(EventWrittenEventArgs eventData)
 69        {
 70            // Workaround https://github.com/dotnet/corefx/issues/42600
 1071            if (eventData.EventId == -1)
 72            {
 273                return;
 74            }
 75
 876            _log(eventData, EventSourceEventFormatting.Format(eventData));
 877        }
 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        {
 085            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        {
 294            return new AzureEventSourceListener(
 095                (eventData, text) => Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "[{0}] {1}", eventData.
 96        }
 97    }
 98}