|
| 1 | +using Exceptionless.Models; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace Exceptionless.Extensions.Logging |
| 7 | +{ |
| 8 | + public class ExceptionlessLogger : ILogger |
| 9 | + { |
| 10 | + static readonly Dictionary<LogLevel, string> LOG_LEVELS = MapLogLevelsToExceptionlessNames(); |
| 11 | + |
| 12 | + static Dictionary<LogLevel, string> MapLogLevelsToExceptionlessNames() |
| 13 | + { |
| 14 | + Dictionary<LogLevel, string> mappings = new Dictionary<LogLevel, string>(7); |
| 15 | + mappings.Add(LogLevel.Critical, global::Exceptionless.Logging.LogLevel.Fatal.ToString()); |
| 16 | + mappings.Add(LogLevel.Debug, global::Exceptionless.Logging.LogLevel.Debug.ToString()); |
| 17 | + mappings.Add(LogLevel.Error, global::Exceptionless.Logging.LogLevel.Error.ToString()); |
| 18 | + mappings.Add(LogLevel.Information, global::Exceptionless.Logging.LogLevel.Info.ToString()); |
| 19 | + mappings.Add(LogLevel.None, global::Exceptionless.Logging.LogLevel.Off.ToString()); |
| 20 | + mappings.Add(LogLevel.Trace, global::Exceptionless.Logging.LogLevel.Trace.ToString()); |
| 21 | + mappings.Add(LogLevel.Warning, global::Exceptionless.Logging.LogLevel.Warn.ToString()); |
| 22 | + |
| 23 | + return mappings; |
| 24 | + } |
| 25 | + |
| 26 | + ExceptionlessClient _Client; |
| 27 | + string _Source; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the <see cref="ExceptionlessLogger"/> class. |
| 31 | + /// </summary> |
| 32 | + /// <param name="client">The <see cref="ExceptionlessClient"/> to be used by the logger.</param> |
| 33 | + /// <param name="source">The source to tag events with, typically the category.</param> |
| 34 | + public ExceptionlessLogger(ExceptionlessClient client, string source) |
| 35 | + { |
| 36 | + if (client == null) |
| 37 | + throw new ArgumentNullException(nameof(client)); |
| 38 | + |
| 39 | + _Client = client; |
| 40 | + _Source = source; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Begins a logical operation scope. |
| 45 | + /// </summary>The identifier for the scope. |
| 46 | + /// <typeparam name="TState">The type of the state object.</typeparam> |
| 47 | + /// <param name="state"></param> |
| 48 | + /// <returns>An <see cref="IDisposable"/> that ends the logical operation scope on dispose.</returns> |
| 49 | + public IDisposable BeginScope<TState>(TState state) |
| 50 | + { |
| 51 | + if (state == null) |
| 52 | + throw new ArgumentNullException(nameof(state)); |
| 53 | + |
| 54 | + // Typically a string will be used as the state for a scope, but the BeginScope extension provides |
| 55 | + // a FormattedLogValues and ASP.NET provides multiple scope objects, so just use ToString() |
| 56 | + string description = state.ToString(); |
| 57 | + |
| 58 | + // If there's data other than a simple string, it'll be added to the event |
| 59 | + object stateObj = state is string ? null : (object)state; |
| 60 | + |
| 61 | + // Log scope creation as an event so that there is a parent to tie events together |
| 62 | + ExceptionlessLoggingScope scope = new ExceptionlessLoggingScope(description); |
| 63 | + LogScope(scope, stateObj); |
| 64 | + |
| 65 | + // Add to stack to support nesting within execution context |
| 66 | + ExceptionlessLoggingScope.Push(scope); |
| 67 | + return scope; |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Checks if the given <see cref="LogLevel"/> is enabled. |
| 72 | + /// </summary> |
| 73 | + /// <param name="logLevel">The level to be checked.</param> |
| 74 | + /// <returns>Returns true if enabled.</returns> |
| 75 | + public bool IsEnabled(LogLevel logLevel) |
| 76 | + { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Writes a log entry. |
| 82 | + /// </summary> |
| 83 | + /// <typeparam name="TState">The type of the state object.</typeparam> |
| 84 | + /// <param name="logLevel">Entry will be written on this level.</param> |
| 85 | + /// <param name="eventId">Id of the event.</param> |
| 86 | + /// <param name="state">The entry to be written. Can be also an object.</param> |
| 87 | + /// <param name="exception">The exception related to this entry.</param> |
| 88 | + /// <param name="formatter">Function to create a <see cref="string"/> message of the <paramref name="state"/> and <paramref name="exception"/>.</param> |
| 89 | + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
| 90 | + { |
| 91 | + // Create basic event from client |
| 92 | + EventBuilder eb = exception == null ? _Client.CreateEvent() : _Client.CreateException(exception); |
| 93 | + eb.SetProperty(Event.KnownDataKeys.Level, LOG_LEVELS[logLevel]); |
| 94 | + |
| 95 | + // Get formatted message |
| 96 | + eb.SetMessage(formatter.Invoke(state, exception)); |
| 97 | + |
| 98 | + // Add category as source, if available |
| 99 | + if (_Source != null) |
| 100 | + eb.SetSource(_Source); |
| 101 | + |
| 102 | + // Add event id, if available |
| 103 | + if (eventId.Id != 0) |
| 104 | + eb.SetProperty("Event Id", eventId.Id); |
| 105 | + |
| 106 | + // If within a scope, add scope's reference id |
| 107 | + if (ExceptionlessLoggingScope.Current != null) |
| 108 | + eb.SetEventReference("Parent", ExceptionlessLoggingScope.Current.Id); |
| 109 | + |
| 110 | + // The logging framework passes in FormattedLogValues, which implements IEnumerable<KeyValuePair<string, object>>; |
| 111 | + // add each property and value as individual objects for proper visibility in Exceptionless |
| 112 | + IEnumerable<KeyValuePair<string, object>> stateProps = state as IEnumerable<KeyValuePair<string, object>>; |
| 113 | + if (stateProps != null) |
| 114 | + { |
| 115 | + foreach (KeyValuePair<string, object> prop in stateProps) |
| 116 | + { |
| 117 | + // Logging the message template is superfluous |
| 118 | + if (prop.Key != "{OriginalFormat}") |
| 119 | + eb.AddObject(prop.Value, prop.Key); |
| 120 | + } |
| 121 | + } |
| 122 | + // Otherwise, attach the entire object, using its type as the name |
| 123 | + else |
| 124 | + { |
| 125 | + eb.AddObject(state); |
| 126 | + } |
| 127 | + |
| 128 | + // Add to client's queue |
| 129 | + eb.Submit(); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Writes a scope creation entry. |
| 134 | + /// </summary> |
| 135 | + /// <param name="newScope">The <see cref="ExceptionlessLoggingScope"/> being created.</param> |
| 136 | + private void LogScope(ExceptionlessLoggingScope newScope, object state) |
| 137 | + { |
| 138 | + EventBuilder eb = _Client.CreateLog($"Creating scope: {newScope.Description}.", global::Exceptionless.Logging.LogLevel.Other); |
| 139 | + |
| 140 | + // Set event reference id to that of scope object |
| 141 | + eb.SetReferenceId(newScope.Id); |
| 142 | + |
| 143 | + // If this is a nested scope, add parent's reference id |
| 144 | + if (ExceptionlessLoggingScope.Current != null) |
| 145 | + eb.SetEventReference("Parent", ExceptionlessLoggingScope.Current.Id); |
| 146 | + |
| 147 | + if (state != null) |
| 148 | + { |
| 149 | + IEnumerable<KeyValuePair<string, object>> stateProps = state as IEnumerable<KeyValuePair<string, object>>; |
| 150 | + if (stateProps != null) |
| 151 | + { |
| 152 | + foreach (KeyValuePair<string, object> prop in stateProps) |
| 153 | + { |
| 154 | + // Logging the message template is superfluous |
| 155 | + if (prop.Key != "{OriginalFormat}") |
| 156 | + eb.AddObject(prop.Value, prop.Key); |
| 157 | + } |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + eb.AddObject(state); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + eb.Submit(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments