| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using NLog; |
| | | 6 | | |
| | | 7 | | namespace ReFlex.Core.Common.Util |
| | | 8 | | { |
| | | 9 | | public static class LogUtilities |
| | | 10 | | { |
| | 1 | 11 | | private static readonly Logger Log = LogManager.GetCurrentClassLogger(); |
| | 1 | 12 | | private static readonly ConcurrentDictionary<string, byte> LoggedErrors = new ConcurrentDictionary<string, byte> |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Logs an exception only once for the same source, method and exception signature. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="exception">Exception to log.</param> |
| | | 18 | | /// <param name="sourceName">Logical source (for example class name) used for scoping deduplication.</param> |
| | | 19 | | /// <param name="methodName">Method name used for scoping deduplication.</param> |
| | | 20 | | /// <param name="message">Optional custom message for the log entry.</param> |
| | | 21 | | /// <exception cref="ArgumentNullException">Thrown when required arguments are null.</exception> |
| | | 22 | | public static void LogErrorOnce( |
| | | 23 | | Exception exception, |
| | | 24 | | string sourceName, |
| | | 25 | | string methodName, |
| | | 26 | | string message = null) |
| | 19 | 27 | | { |
| | 19 | 28 | | if (sourceName == null) |
| | 0 | 29 | | throw new ArgumentNullException(nameof(sourceName)); |
| | 19 | 30 | | if (methodName == null) |
| | 0 | 31 | | throw new ArgumentNullException(nameof(methodName)); |
| | | 32 | | |
| | 19 | 33 | | if (exception == null) |
| | 0 | 34 | | { |
| | 0 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 19 | 38 | | var errorKey = BuildErrorKey(sourceName, methodName, exception); |
| | 19 | 39 | | if (!LoggedErrors.TryAdd(errorKey, 0)) |
| | 7 | 40 | | { |
| | 7 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 12 | 44 | | if (string.IsNullOrWhiteSpace(message)) |
| | 9 | 45 | | { |
| | 9 | 46 | | Log.Error(exception); |
| | 9 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 3 | 50 | | Log.Error(exception, message); |
| | 19 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Clears deduplication entries. |
| | | 55 | | /// If <paramref name="sourceName"/> is provided, only entries for this source are removed. |
| | | 56 | | /// If it is null or whitespace, all entries are removed. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="sourceName">Optional source name filter.</param> |
| | | 59 | | public static void ClearLoggedErrors(string sourceName = null) |
| | 22 | 60 | | { |
| | 22 | 61 | | if (string.IsNullOrWhiteSpace(sourceName)) |
| | 4 | 62 | | { |
| | 4 | 63 | | LoggedErrors.Clear(); |
| | 4 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 18 | 67 | | var sourcePrefix = $"{sourceName}:"; |
| | 18 | 68 | | var keys = LoggedErrors.Keys |
| | 4 | 69 | | .Where(key => key.StartsWith(sourcePrefix, StringComparison.Ordinal)) |
| | 18 | 70 | | .ToList(); |
| | | 71 | | |
| | 62 | 72 | | foreach (var key in keys) |
| | 4 | 73 | | { |
| | 4 | 74 | | LoggedErrors.TryRemove(key, out _); |
| | 4 | 75 | | } |
| | 22 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Creates a key that uniquely identifies an error context for deduplicated logging. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="sourceName">Logical source (for example class name).</param> |
| | | 82 | | /// <param name="methodName">Method name where the exception occurred.</param> |
| | | 83 | | /// <param name="exception">Exception to create the key from.</param> |
| | | 84 | | /// <returns>Deterministic key based on source, method and exception signature.</returns> |
| | | 85 | | private static string BuildErrorKey(string sourceName, string methodName, Exception exception) |
| | 19 | 86 | | { |
| | 19 | 87 | | return $"{sourceName}:{methodName}:{BuildExceptionSignature(exception)}"; |
| | 19 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Builds a deterministic signature from the exception chain. |
| | | 92 | | /// This includes each exception type and message, including inner exceptions. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="exception">Exception to convert to a signature.</param> |
| | | 95 | | /// <returns>Signature string that can be used for deduplication.</returns> |
| | | 96 | | private static string BuildExceptionSignature(Exception exception) |
| | 19 | 97 | | { |
| | 19 | 98 | | var builder = new StringBuilder(); |
| | 19 | 99 | | var current = exception; |
| | | 100 | | |
| | 40 | 101 | | while (current != null) |
| | 21 | 102 | | { |
| | 21 | 103 | | builder.Append(current.GetType().FullName); |
| | 21 | 104 | | builder.Append(':'); |
| | 21 | 105 | | builder.Append(current.Message); |
| | 21 | 106 | | builder.Append('|'); |
| | 21 | 107 | | current = current.InnerException; |
| | 21 | 108 | | } |
| | | 109 | | |
| | 19 | 110 | | return builder.ToString(); |
| | 19 | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |