|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 2.0. |
| 3 | +// Copyright (c) 2007-2020 VMware, Inc. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics.Tracing; |
| 7 | + |
| 8 | +namespace RabbitMQ.Stream.Client |
| 9 | +{ |
| 10 | + public static class Keywords |
| 11 | + { |
| 12 | + public const EventKeywords Log = (EventKeywords)1; |
| 13 | + } |
| 14 | + |
| 15 | + [EventSource(Name = "rabbitmq-client-stream")] |
| 16 | + internal sealed class LogEventSource : EventSource, ILogEventSource |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Default <see cref="LogEventSource" /> implementation for logging. |
| 20 | + /// </summary> |
| 21 | + public static readonly |
| 22 | + ILogEventSource Log = new |
| 23 | + LogEventSource |
| 24 | + (); |
| 25 | + |
| 26 | + private LogEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat) |
| 27 | + { } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// </summary> |
| 31 | + [NonEvent] |
| 32 | + private static string ConvertToString(Exception exception) |
| 33 | + { |
| 34 | + return exception == default ? default : $"{Environment.NewLine}{ exception?.ToString() }"; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Writes an informational log message. |
| 39 | + /// </summary> |
| 40 | + /// |
| 41 | + /// <param name="message"> |
| 42 | + /// </param> |
| 43 | + [Event(1, Message = "INFO", Keywords = Keywords.Log, Level = EventLevel.Informational)] |
| 44 | + public ILogEventSource LogInformation(string message) |
| 45 | + { |
| 46 | + if (IsEnabled()) |
| 47 | + { |
| 48 | + WriteEvent(1, message); |
| 49 | + } |
| 50 | + |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Writes an informational log message. |
| 56 | + /// </summary> |
| 57 | + /// |
| 58 | + /// <param name="message"> |
| 59 | + /// </param> |
| 60 | + /// |
| 61 | + /// <param name="args"> |
| 62 | + /// </param> |
| 63 | + [NonEvent] |
| 64 | + public ILogEventSource LogInformation(string message, params object[] args) |
| 65 | + { |
| 66 | + return LogInformation(string.Format(message, args)); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Writes a warning log message. |
| 71 | + /// </summary> |
| 72 | + /// |
| 73 | + /// <param name="message"> |
| 74 | + /// </param> |
| 75 | + [Event(2, Message = "WARN", Keywords = Keywords.Log, Level = EventLevel.Warning)] |
| 76 | + public ILogEventSource LogWarning(string message) |
| 77 | + { |
| 78 | + if (IsEnabled()) |
| 79 | + { |
| 80 | + WriteEvent(2, message); |
| 81 | + } |
| 82 | + |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Writes a warning log message. |
| 88 | + /// </summary> |
| 89 | + /// |
| 90 | + /// <param name="message"> |
| 91 | + /// </param> |
| 92 | + /// |
| 93 | + /// <param name="args"> |
| 94 | + /// </param> |
| 95 | + public ILogEventSource LogWarning(string message, params object[] args) |
| 96 | + { |
| 97 | + return LogWarning(string.Format(message, args)); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Writes an error log message. |
| 102 | + /// </summary> |
| 103 | + /// |
| 104 | + /// <param name="message"> |
| 105 | + /// </param> |
| 106 | + [Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)] |
| 107 | + public ILogEventSource LogError(string message) |
| 108 | + { |
| 109 | + if (IsEnabled()) |
| 110 | + { |
| 111 | + WriteEvent(3, message); |
| 112 | + } |
| 113 | + |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Writes an error log message. |
| 119 | + /// </summary> |
| 120 | + /// |
| 121 | + /// <param name="message"> |
| 122 | + /// </param> |
| 123 | + /// |
| 124 | + /// <param name="exception"> |
| 125 | + /// The exception to log. |
| 126 | + /// </param> |
| 127 | + [NonEvent] |
| 128 | + public ILogEventSource LogError(string message, Exception exception) |
| 129 | + { |
| 130 | + LogError($"{ message }{ ConvertToString(exception) }"); |
| 131 | + |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Writes an error log message. |
| 137 | + /// </summary> |
| 138 | + /// |
| 139 | + /// <param name="message"> |
| 140 | + /// </param> |
| 141 | + /// |
| 142 | + /// <param name="exception"> |
| 143 | + /// The exception to log. |
| 144 | + /// </param> |
| 145 | + /// |
| 146 | + /// <param name="args"> |
| 147 | + /// </param> |
| 148 | + [NonEvent] |
| 149 | + public ILogEventSource LogError(string message, Exception exception, params object[] args) |
| 150 | + { |
| 151 | + return LogError(string.Format(message, args), exception); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments