|
| 1 | +using log4net.Core; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using System.Collections; |
| 4 | +using System.Globalization; |
| 5 | + |
| 6 | +namespace XKeys_dmxc3_Plugin.Plugin.Logging |
| 7 | +{ |
| 8 | + public class LumosLogWrapperEventFactory |
| 9 | + { |
| 10 | + private static readonly LumosLogWrapperEventFactory instance = new(); |
| 11 | + |
| 12 | + public static LumosLogWrapperEventFactory getInstance() |
| 13 | + { |
| 14 | + return instance; |
| 15 | + } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The default property name for scopes that don't provide their own property name by implementing |
| 19 | + /// an <see cref="IEnumerable{T}"/> where T is <see cref="KeyValuePair{TKey,TValue}"/> and where TKey |
| 20 | + /// is <see cref="string"/>. |
| 21 | + /// </summary> |
| 22 | + protected const string DefaultScopeProperty = "scope"; |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + public LoggingEvent CreateLoggingEvent<TState>( |
| 26 | + in MessageCandidate<TState> messageCandidate, |
| 27 | + log4net.Core.ILogger logger, |
| 28 | + IExternalScopeProvider scopeProvider) |
| 29 | + { |
| 30 | + Type callerStackBoundaryDeclaringType = typeof(LoggerExtensions); |
| 31 | + string message = messageCandidate.Formatter(messageCandidate.State, messageCandidate.Exception); |
| 32 | + Level logLevel = LumosLogWrapperLogLevelTranslator.TranslateLogLevel(messageCandidate.LogLevel); |
| 33 | + |
| 34 | + if (logLevel == null || (string.IsNullOrEmpty(message) && messageCandidate.Exception == null)) |
| 35 | + return null; |
| 36 | + |
| 37 | + var loggingEvent = new LoggingEvent( |
| 38 | + callerStackBoundaryDeclaringType: callerStackBoundaryDeclaringType, |
| 39 | + repository: logger.Repository, |
| 40 | + loggerName: logger.Name, |
| 41 | + level: logLevel, |
| 42 | + message: message, |
| 43 | + exception: messageCandidate.Exception); |
| 44 | + |
| 45 | + EnrichWithScopes(loggingEvent, scopeProvider); |
| 46 | + |
| 47 | + return loggingEvent; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the scopes from the external scope provider and converts them to the properties on the logging event. |
| 53 | + /// This function will honor the convention that logging scopes can provide their own property name, by implementing |
| 54 | + /// an <see cref="IEnumerable{T}"/> where T is <see cref="KeyValuePair{TKey,TValue}"/> and where TKey is |
| 55 | + /// <see cref="string"/>. |
| 56 | + /// </summary> |
| 57 | + /// <remarks> |
| 58 | + /// The default implementation will call Convert.ToString(scope, CultureInfo.InvariantCulture) on all scope objects. |
| 59 | + /// If you want to do this conversion inside the Log4Net Pipeline, e. g. with a custom layout, you can override this |
| 60 | + /// method and change the behaviour. |
| 61 | + /// </remarks> |
| 62 | + /// <param name="loggingEvent">The <see cref="LoggingEvent"/> the scope information will be added to.</param> |
| 63 | + /// <param name="scopeProvider">The external provider for the current logging scope.</param> |
| 64 | + protected virtual void EnrichWithScopes(LoggingEvent loggingEvent, IExternalScopeProvider scopeProvider) |
| 65 | + { |
| 66 | + scopeProvider.ForEachScope((scope, @event) => |
| 67 | + { |
| 68 | + // This function will add the scopes in the legacy way they were added before the IExternalScopeProvider was introduced, |
| 69 | + // to maintain backwards compatibility. |
| 70 | + // This pretty much means that we are emulating a LogicalThreadContextStack, which is a stack, that allows pushing |
| 71 | + // strings on to it, which will be concatenated with space as a separator. |
| 72 | + // See: https://github.com/apache/logging-log4net/blob/47aaf46d5f031ea29d781bac4617bd1bb9446215/src/log4net/Util/LogicalThreadContextStack.cs#L343 |
| 73 | + |
| 74 | + // Because string implements IEnumerable we first need to check for string. |
| 75 | + if (scope is string) |
| 76 | + { |
| 77 | + string previousValue = @event.Properties[DefaultScopeProperty] as string; |
| 78 | + |
| 79 | + @event.Properties[DefaultScopeProperty] = JoinOldAndNewValue(previousValue, scope.ToString()); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (scope is IEnumerable col) |
| 84 | + { |
| 85 | + foreach (var item in col) |
| 86 | + { |
| 87 | + if (item is KeyValuePair<string, string>) |
| 88 | + { |
| 89 | + var keyValuePair = (KeyValuePair<string, string>)item; |
| 90 | + string previousValue = @event.Properties[keyValuePair.Key] as string; |
| 91 | + @event.Properties[keyValuePair.Key] = JoinOldAndNewValue(previousValue, keyValuePair.Value); |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + if (item is KeyValuePair<string, object>) |
| 96 | + { |
| 97 | + var keyValuePair = (KeyValuePair<string, object>)item; |
| 98 | + string previousValue = @event.Properties[keyValuePair.Key] as string; |
| 99 | + |
| 100 | + // The current culture should not influence how integers/floats/... are displayed in logging, |
| 101 | + // so we are using Convert.ToString which will convert IConvertible and IFormattable with |
| 102 | + // the specified IFormatProvider. |
| 103 | + string additionalValue = Convert.ToString(keyValuePair.Value, CultureInfo.InvariantCulture); |
| 104 | + @event.Properties[keyValuePair.Key] = JoinOldAndNewValue(previousValue, additionalValue); |
| 105 | + continue; |
| 106 | + } |
| 107 | + } |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (scope is not null) |
| 112 | + { |
| 113 | + string previousValue = @event.Properties[DefaultScopeProperty] as string; |
| 114 | + string additionalValue = Convert.ToString(scope, CultureInfo.InvariantCulture); |
| 115 | + @event.Properties[DefaultScopeProperty] = JoinOldAndNewValue(previousValue, additionalValue); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + }, loggingEvent); |
| 120 | + } |
| 121 | + |
| 122 | + private static string JoinOldAndNewValue(string previousValue, string newValue) |
| 123 | + { |
| 124 | + if (string.IsNullOrEmpty(previousValue)) |
| 125 | + { |
| 126 | + return newValue; |
| 127 | + } |
| 128 | + |
| 129 | + return previousValue + " " + newValue; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments