< Summary

Class:Microsoft.Extensions.Azure.EventSourceLogForwarder
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\EventSourceLogForwarder.cs
Covered lines:24
Uncovered lines:7
Coverable lines:31
Total lines:105
Line coverage:77.4% (24 of 31)
Covered branches:8
Total branches:11
Branch coverage:72.7% (8 of 11)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
LogEvent(...)-100%100%
ToLoggerName(...)-100%100%
Dispose()-100%100%
MapLevel(...)-85.71%85.71%
FormatMessage(...)-100%100%
get_EventData()-100%100%
.ctor(...)-100%100%
GetEnumerator()-0%0%
System.Collections.IEnumerable.GetEnumerator()-0%100%
get_Count()-0%100%
get_Item(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\EventSourceLogForwarder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections;
 6using System.Collections.Concurrent;
 7using System.Collections.Generic;
 8using System.Diagnostics.Tracing;
 9using Azure.Core.Diagnostics;
 10using Azure.Core.Shared;
 11using Microsoft.Extensions.Logging;
 12
 13namespace Microsoft.Extensions.Azure
 14{
 15    internal class EventSourceLogForwarder: IDisposable
 16    {
 17        private readonly ILoggerFactory _loggerFactory;
 18
 5619        private readonly ConcurrentDictionary<string, ILogger> _loggers = new ConcurrentDictionary<string, ILogger>();
 20
 5621        private readonly Func<EventSourceEvent, Exception, string> _formatMessage = FormatMessage;
 22
 23        private readonly AzureEventSourceListener _listener;
 24
 5625        public EventSourceLogForwarder(ILoggerFactory loggerFactory = null)
 26        {
 5627            _loggerFactory = loggerFactory;
 36428            _listener = new AzureEventSourceListener((e, s) => LogEvent(e), EventLevel.Verbose);
 5629        }
 30
 31        private void LogEvent(EventWrittenEventArgs eventData)
 32        {
 30833            if (_loggerFactory == null)
 34            {
 29635                return;
 36            }
 37
 2438            var logger = _loggers.GetOrAdd(eventData.EventSource.Name, name => _loggerFactory.CreateLogger(ToLoggerName(
 1239            logger.Log(MapLevel(eventData.Level), new EventId(eventData.EventId, eventData.EventName), new EventSourceEv
 1240        }
 41
 42        private static string ToLoggerName(string name)
 43        {
 1244            return name.Replace('-', '.');
 45        }
 46
 47        public void Dispose()
 48        {
 1449            _listener.Dispose();
 1450        }
 51
 52        private static LogLevel MapLevel(EventLevel level)
 53        {
 54            switch (level)
 55            {
 56                case EventLevel.Critical:
 257                    return LogLevel.Critical;
 58                case EventLevel.Error:
 259                    return LogLevel.Error;
 60                case EventLevel.Informational:
 261                    return LogLevel.Information;
 62                case EventLevel.Verbose:
 263                    return LogLevel.Debug;
 64                case EventLevel.Warning:
 265                    return LogLevel.Warning;
 66                case EventLevel.LogAlways:
 267                    return LogLevel.Information;
 68                default:
 069                    throw new ArgumentOutOfRangeException(nameof(level), level, null);
 70            }
 71        }
 72
 73        private static string FormatMessage(EventSourceEvent eventSourceEvent, Exception exception)
 74        {
 1275            return EventSourceEventFormatting.Format(eventSourceEvent.EventData);
 76        }
 77
 78        private struct EventSourceEvent: IReadOnlyList<KeyValuePair<string, object>>
 79        {
 1280            public EventWrittenEventArgs EventData { get; }
 81
 82            public EventSourceEvent(EventWrittenEventArgs eventData)
 83            {
 1284                EventData = eventData;
 1285            }
 86
 87            public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
 88            {
 089                for (int i = 0; i < Count; i++)
 90                {
 091                    yield return new KeyValuePair<string, object>(EventData.PayloadNames[i], EventData.Payload[i]);
 92                }
 093            }
 94
 95            IEnumerator IEnumerable.GetEnumerator()
 96            {
 097                return GetEnumerator();
 98            }
 99
 0100            public int Count => EventData.PayloadNames.Count;
 101
 0102            public KeyValuePair<string, object> this[int index] => new KeyValuePair<string, object>(EventData.PayloadNam
 103        }
 104    }
 105}