| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Concurrent; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics.Tracing; |
| | 9 | | using Azure.Core.Diagnostics; |
| | 10 | | using Azure.Core.Shared; |
| | 11 | | using Microsoft.Extensions.Logging; |
| | 12 | |
|
| | 13 | | namespace Microsoft.Extensions.Azure |
| | 14 | | { |
| | 15 | | internal class EventSourceLogForwarder: IDisposable |
| | 16 | | { |
| | 17 | | private readonly ILoggerFactory _loggerFactory; |
| | 18 | |
|
| 56 | 19 | | private readonly ConcurrentDictionary<string, ILogger> _loggers = new ConcurrentDictionary<string, ILogger>(); |
| | 20 | |
|
| 56 | 21 | | private readonly Func<EventSourceEvent, Exception, string> _formatMessage = FormatMessage; |
| | 22 | |
|
| | 23 | | private readonly AzureEventSourceListener _listener; |
| | 24 | |
|
| 56 | 25 | | public EventSourceLogForwarder(ILoggerFactory loggerFactory = null) |
| | 26 | | { |
| 56 | 27 | | _loggerFactory = loggerFactory; |
| 364 | 28 | | _listener = new AzureEventSourceListener((e, s) => LogEvent(e), EventLevel.Verbose); |
| 56 | 29 | | } |
| | 30 | |
|
| | 31 | | private void LogEvent(EventWrittenEventArgs eventData) |
| | 32 | | { |
| 308 | 33 | | if (_loggerFactory == null) |
| | 34 | | { |
| 296 | 35 | | return; |
| | 36 | | } |
| | 37 | |
|
| 24 | 38 | | var logger = _loggers.GetOrAdd(eventData.EventSource.Name, name => _loggerFactory.CreateLogger(ToLoggerName( |
| 12 | 39 | | logger.Log(MapLevel(eventData.Level), new EventId(eventData.EventId, eventData.EventName), new EventSourceEv |
| 12 | 40 | | } |
| | 41 | |
|
| | 42 | | private static string ToLoggerName(string name) |
| | 43 | | { |
| 12 | 44 | | return name.Replace('-', '.'); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public void Dispose() |
| | 48 | | { |
| 14 | 49 | | _listener.Dispose(); |
| 14 | 50 | | } |
| | 51 | |
|
| | 52 | | private static LogLevel MapLevel(EventLevel level) |
| | 53 | | { |
| | 54 | | switch (level) |
| | 55 | | { |
| | 56 | | case EventLevel.Critical: |
| 2 | 57 | | return LogLevel.Critical; |
| | 58 | | case EventLevel.Error: |
| 2 | 59 | | return LogLevel.Error; |
| | 60 | | case EventLevel.Informational: |
| 2 | 61 | | return LogLevel.Information; |
| | 62 | | case EventLevel.Verbose: |
| 2 | 63 | | return LogLevel.Debug; |
| | 64 | | case EventLevel.Warning: |
| 2 | 65 | | return LogLevel.Warning; |
| | 66 | | case EventLevel.LogAlways: |
| 2 | 67 | | return LogLevel.Information; |
| | 68 | | default: |
| 0 | 69 | | throw new ArgumentOutOfRangeException(nameof(level), level, null); |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private static string FormatMessage(EventSourceEvent eventSourceEvent, Exception exception) |
| | 74 | | { |
| 12 | 75 | | return EventSourceEventFormatting.Format(eventSourceEvent.EventData); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private struct EventSourceEvent: IReadOnlyList<KeyValuePair<string, object>> |
| | 79 | | { |
| 12 | 80 | | public EventWrittenEventArgs EventData { get; } |
| | 81 | |
|
| | 82 | | public EventSourceEvent(EventWrittenEventArgs eventData) |
| | 83 | | { |
| 12 | 84 | | EventData = eventData; |
| 12 | 85 | | } |
| | 86 | |
|
| | 87 | | public IEnumerator<KeyValuePair<string, object>> GetEnumerator() |
| | 88 | | { |
| 0 | 89 | | for (int i = 0; i < Count; i++) |
| | 90 | | { |
| 0 | 91 | | yield return new KeyValuePair<string, object>(EventData.PayloadNames[i], EventData.Payload[i]); |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | IEnumerator IEnumerable.GetEnumerator() |
| | 96 | | { |
| 0 | 97 | | return GetEnumerator(); |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | public int Count => EventData.PayloadNames.Count; |
| | 101 | |
|
| 0 | 102 | | public KeyValuePair<string, object> this[int index] => new KeyValuePair<string, object>(EventData.PayloadNam |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |