| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="TraceSource"/> |
| | 5 | | /// </summary> |
| | 6 | | /// <remarks> |
| | 7 | | /// Based on Fonlow.Diagnostics by Zijian Huang [https://github.com/zijianhuang/Fonlow.Diagnostics] |
| | 8 | | /// Also see “Use TraceSource Efficiently” [http://www.codeproject.com/Tips/1071853/Use-TraceSource-Efficiently] |
| | 9 | | /// </remarks> |
| | 10 | | public static class TraceSourceExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Traces the error. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="traceSource">The trace source.</param> |
| | 16 | | /// <param name="format">The format.</param> |
| | 17 | | /// <param name="args">The arguments.</param> |
| | 18 | | public static void TraceError(this TraceSource? traceSource, string format, params object[] args) => |
| 0 | 19 | | traceSource?.TraceEvent(TraceEventType.Error, ++_eventId, format, args); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Traces the error. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="traceSource">The trace source.</param> |
| | 25 | | /// <param name="message">The message.</param> |
| | 26 | | public static void TraceError(this TraceSource? traceSource, string message) => |
| 6 | 27 | | traceSource?.TraceEvent(TraceEventType.Error, ++_eventId, message); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Traces the error. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="traceSource">The trace source.</param> |
| | 33 | | /// <param name="ex">The exception.</param> |
| | 34 | | public static void TraceError(this TraceSource? traceSource, Exception ex) => |
| 0 | 35 | | traceSource.TraceError(ex, includeStackTrace: false); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Traces the error. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="traceSource">The trace source.</param> |
| | 41 | | /// <param name="ex">The exception.</param> |
| | 42 | | /// <param name="includeStackTrace">When <c>true</c>, include <see cref="Exception.StackTrace"/> info.</param> |
| | 43 | | public static void TraceError(this TraceSource? traceSource, Exception? ex, bool includeStackTrace) |
| 0 | 44 | | { |
| 0 | 45 | | if (traceSource == null) return; |
| 0 | 46 | | if (ex == null) return; |
| | 47 | |
|
| 0 | 48 | | var sb = new StringBuilder(ex.GetType().Name); |
| | 49 | |
|
| 0 | 50 | | sb.AppendLine($"{nameof(ex.Message)}: {ex.Message}"); |
| | 51 | |
|
| 0 | 52 | | if (includeStackTrace) |
| 0 | 53 | | { |
| 0 | 54 | | sb.AppendLine($"{nameof(ex.StackTrace)}:"); |
| 0 | 55 | | sb.AppendLine($"{ex.StackTrace}"); |
| 0 | 56 | | } |
| | 57 | |
|
| 0 | 58 | | traceSource.TraceError(sb.ToString()); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Trace event type <see cref="TraceEventType.Warning"/> |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="traceSource">The trace source.</param> |
| | 65 | | /// <param name="message">The message.</param> |
| | 66 | | public static void TraceWarning(this TraceSource? traceSource, string message) => |
| 1 | 67 | | traceSource?.TraceEvent(TraceEventType.Warning, ++_eventId, message); |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Trace event type <see cref="TraceEventType.Warning"/> |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="traceSource">The trace source.</param> |
| | 73 | | /// <param name="format">The format.</param> |
| | 74 | | /// <param name="args">The arguments.</param> |
| | 75 | | public static void TraceWarning(this TraceSource? traceSource, string format, params object[] args) => |
| 0 | 76 | | traceSource?.TraceEvent(TraceEventType.Warning, ++_eventId, format, args); |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Trace event type <see cref="TraceEventType.Verbose"/> |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="traceSource">The trace source.</param> |
| | 82 | | /// <param name="message">The message.</param> |
| | 83 | | public static void TraceVerbose(this TraceSource? traceSource, string message) => |
| 4 | 84 | | traceSource?.TraceEvent(TraceEventType.Verbose, ++_eventId, message); |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Trace event type <see cref="TraceEventType.Verbose"/> |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="traceSource">The trace source.</param> |
| | 90 | | /// <param name="format">The format.</param> |
| | 91 | | /// <param name="args">The arguments.</param> |
| | 92 | | public static void TraceVerbose(this TraceSource? traceSource, string format, params object[] args) => |
| 0 | 93 | | traceSource?.TraceEvent(TraceEventType.Verbose, ++_eventId, format, args); |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Returns the <see cref="TraceSource"/> |
| | 97 | | /// with Switch Level <see cref="SourceLevels.All"/>. |
| | 98 | | /// </summary> |
| | 99 | | /// <param name="traceSource">The trace source.</param> |
| | 100 | | public static TraceSource? WithSourceLevels(this TraceSource? traceSource) => |
| 11 | 101 | | traceSource?.WithSourceLevels(SourceLevels.All); |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Returns the <see cref="TraceSource"/> |
| | 105 | | /// with the specified <see cref="SourceLevels"/>. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="traceSource">The trace source.</param> |
| | 108 | | /// <param name="levels">The levels.</param> |
| | 109 | | public static TraceSource? WithSourceLevels(this TraceSource? traceSource, SourceLevels levels) |
| 10 | 110 | | { |
| 10 | 111 | | if (traceSource == null) return null; |
| | 112 | |
|
| 10 | 113 | | traceSource.Switch.Level = levels; |
| | 114 | |
|
| 10 | 115 | | return traceSource; |
| 10 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Trace event type <see cref="TraceEventType.Information"/> |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="traceSource">The trace source.</param> |
| | 122 | | /// <param name="message">The message.</param> |
| | 123 | | /// <remarks> |
| | 124 | | /// This member was previously marked with <see cref="ObsoleteAttribute"/>. |
| | 125 | | /// For detail, see https://github.com/BryanWilhite/SonghayCore/issues/82#issuecomment-566214635 |
| | 126 | | /// </remarks> |
| | 127 | | public static void WriteLine(this TraceSource? traceSource, string message) => |
| 14 | 128 | | traceSource?.TraceEvent(TraceEventType.Information, ++_eventId, message); |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Trace event type <see cref="TraceEventType.Information"/> |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="traceSource">The trace source.</param> |
| | 134 | | /// <param name="format">The format.</param> |
| | 135 | | /// <param name="args">The arguments.</param> |
| | 136 | | /// <remarks> |
| | 137 | | /// This member was previously marked with <see cref="ObsoleteAttribute"/>. |
| | 138 | | /// For detail, see https://github.com/BryanWilhite/SonghayCore/issues/82#issuecomment-566214635 |
| | 139 | | /// </remarks> |
| | 140 | | public static void WriteLine(this TraceSource? traceSource, string format, params object[] args) => |
| 0 | 141 | | traceSource?.TraceEvent(TraceEventType.Information, ++_eventId, format, args); |
| | 142 | |
|
| | 143 | | [ThreadStatic] static int _eventId; |
| | 144 | | } |