|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Threading; |
| 6 | +using Azure; |
| 7 | +using Azure.Messaging; |
| 8 | +using Azure.Messaging.EventGrid; |
| 9 | +using NLog.Common; |
| 10 | +using NLog.Config; |
| 11 | +using NLog.Layouts; |
| 12 | +using NLog.Extensions.AzureStorage; |
| 13 | + |
| 14 | +namespace NLog.Targets |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Azure Event Grid NLog Target |
| 18 | + /// </summary> |
| 19 | + [Target("AzureEventGrid")] |
| 20 | + public class EventGridTarget : AsyncTaskTarget |
| 21 | + { |
| 22 | + private readonly IEventGridService _eventGridService; |
| 23 | + private readonly char[] _reusableEncodingBuffer = new char[32 * 1024]; // Avoid large-object-heap |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The topic endpoint. For example, "https://TOPIC-NAME.REGION-NAME-1.eventgrid.azure.net/api/events" |
| 27 | + /// </summary> |
| 28 | + public Layout Topic { get; set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// A resource path relative to the topic path. |
| 32 | + /// </summary> |
| 33 | + public Layout GridEventSubject { get; set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event. |
| 37 | + /// </summary> |
| 38 | + public Layout CloudEventSource { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The type of the event that occurred. For example, "Contoso.Items.ItemReceived". |
| 42 | + /// </summary> |
| 43 | + public Layout EventType { get; set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON. |
| 47 | + /// </summary> |
| 48 | + public Layout ContentType { get; set; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// The schema version of the data object. |
| 52 | + /// </summary> |
| 53 | + public Layout DataSchema { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Input for AzureKeyCredential for EventGridPublisherClient constructor |
| 57 | + /// </summary> |
| 58 | + public Layout AccessKey { get; set; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Alternative to AccessKey. Input for <see cref="Azure.Identity.DefaultAzureCredential"/> |
| 62 | + /// </summary> |
| 63 | + public Layout TenantIdentity { get; set; } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Alternative to AccessKey. Input for <see cref="Azure.Identity.DefaultAzureCredential"/> |
| 67 | + /// </summary> |
| 68 | + public Layout ResourceIdentity { get; set; } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Alternative to AccessKey. Input for <see cref="Azure.Identity.DefaultAzureCredential"/> |
| 72 | + /// </summary> |
| 73 | + public Layout ClientIdentity { get; set; } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Gets a list of message properties aka. custom CloudEvent Extension Attributes |
| 77 | + /// </summary> |
| 78 | + [ArrayParameter(typeof(TargetPropertyWithContext), "messageproperty")] |
| 79 | + public IList<TargetPropertyWithContext> MessageProperties { get => ContextProperties; } |
| 80 | + |
| 81 | + public EventGridTarget() |
| 82 | + : this(new EventGridService()) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + internal EventGridTarget(IEventGridService eventGridService) |
| 87 | + { |
| 88 | + _eventGridService = eventGridService; |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc /> |
| 92 | + protected override void InitializeTarget() |
| 93 | + { |
| 94 | + base.InitializeTarget(); |
| 95 | + |
| 96 | + for (int i = 0; i < MessageProperties.Count; ++i) |
| 97 | + { |
| 98 | + var messageProperty = MessageProperties[i]; |
| 99 | + if (!IsValidExtensionAttribue(messageProperty.Name)) |
| 100 | + { |
| 101 | + var messagePropertyName = (messageProperty.Name ?? string.Empty).Trim().ToLowerInvariant(); |
| 102 | + if (!IsValidExtensionAttribue(messagePropertyName)) |
| 103 | + { |
| 104 | + messagePropertyName = string.Join(string.Empty, System.Linq.Enumerable.Where(messagePropertyName, chr => !IsInvalidExtensionAttribueChar(chr))); |
| 105 | + } |
| 106 | + |
| 107 | + InternalLogger.Debug("AzureEventGridTarget(Name={0}): Fixing MessageProperty-Name from '{1}' to '{2}'", Name, messageProperty.Name, messagePropertyName); |
| 108 | + messageProperty.Name = messagePropertyName; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + string topic = string.Empty; |
| 113 | + string tenantIdentity = string.Empty; |
| 114 | + string resourceIdentity = string.Empty; |
| 115 | + string clientIdentity = string.Empty; |
| 116 | + string accessKey = string.Empty; |
| 117 | + |
| 118 | + var defaultLogEvent = LogEventInfo.CreateNullEvent(); |
| 119 | + |
| 120 | + try |
| 121 | + { |
| 122 | + topic = Topic?.Render(defaultLogEvent); |
| 123 | + tenantIdentity = TenantIdentity?.Render(defaultLogEvent); |
| 124 | + resourceIdentity = ResourceIdentity?.Render(defaultLogEvent); |
| 125 | + clientIdentity = ClientIdentity?.Render(defaultLogEvent); |
| 126 | + accessKey = AccessKey?.Render(defaultLogEvent); |
| 127 | + |
| 128 | + _eventGridService.Connect(topic, tenantIdentity, resourceIdentity, clientIdentity, accessKey); |
| 129 | + InternalLogger.Debug("AzureEventGridTarget(Name={0}): Initialized", Name); |
| 130 | + } |
| 131 | + catch (Exception ex) |
| 132 | + { |
| 133 | + InternalLogger.Error(ex, "AzureEventGridTarget(Name={0}): Failed to create EventGridPublisherClient with Topic={1}.", Name, topic); |
| 134 | + throw; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken) |
| 139 | + { |
| 140 | + try |
| 141 | + { |
| 142 | + if (CloudEventSource is null) |
| 143 | + { |
| 144 | + var gridEvent = CreateGridEvent(logEvent); |
| 145 | + return _eventGridService.SendEventAsync(gridEvent, cancellationToken); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + var cloudEvent = CreateCloudEvent(logEvent); |
| 150 | + return _eventGridService.SendEventAsync(cloudEvent, cancellationToken); |
| 151 | + } |
| 152 | + } |
| 153 | + catch (Exception ex) |
| 154 | + { |
| 155 | + InternalLogger.Error(ex, "AzureEventGridTarget(Name={0}): Failed sending logevent to Topic={1}", Name, _eventGridService?.Topic); |
| 156 | + throw; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private sealed class EventGridService : IEventGridService |
| 161 | + { |
| 162 | + EventGridPublisherClient _client; |
| 163 | + |
| 164 | + public string Topic { get; private set; } |
| 165 | + |
| 166 | + public void Connect(string topic, string tenantIdentity, string resourceIdentifier, string clientIdentity, string accessKey) |
| 167 | + { |
| 168 | + Topic = topic; |
| 169 | + |
| 170 | + if (!string.IsNullOrWhiteSpace(accessKey)) |
| 171 | + { |
| 172 | + _client = new EventGridPublisherClient(new Uri(topic), new AzureKeyCredential(accessKey)); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + var tokenCredentials = AzureCredentialHelpers.CreateTokenCredentials(clientIdentity, tenantIdentity, resourceIdentifier); |
| 177 | + _client = new EventGridPublisherClient(new Uri(topic), tokenCredentials); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public Task SendEventAsync(EventGridEvent gridEvent, CancellationToken cancellationToken) |
| 182 | + { |
| 183 | + return _client.SendEventAsync(gridEvent, cancellationToken); |
| 184 | + } |
| 185 | + |
| 186 | + public Task SendEventAsync(CloudEvent cloudEvent, CancellationToken cancellationToken) |
| 187 | + { |
| 188 | + return _client.SendEventAsync(cloudEvent, cancellationToken); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private CloudEvent CreateCloudEvent(LogEventInfo logEvent) |
| 193 | + { |
| 194 | + var eventDataBody = RenderLogEvent(Layout, logEvent) ?? string.Empty; |
| 195 | + var eventSource = RenderLogEvent(CloudEventSource, logEvent) ?? string.Empty; |
| 196 | + var eventType = RenderLogEvent(EventType, logEvent) ?? string.Empty; |
| 197 | + var eventDataSchema = RenderLogEvent(DataSchema, logEvent) ?? string.Empty; |
| 198 | + var eventContentType = RenderLogEvent(ContentType, logEvent) ?? string.Empty; |
| 199 | + var cloudEvent = new CloudEvent(eventSource, eventType, new BinaryData(EncodeToUTF8(eventDataBody)), eventContentType, CloudEventDataFormat.Binary); |
| 200 | + cloudEvent.Time = logEvent.TimeStamp.ToUniversalTime(); |
| 201 | + |
| 202 | + if (!string.IsNullOrEmpty(eventDataSchema)) |
| 203 | + { |
| 204 | + cloudEvent.DataSchema = eventDataSchema; |
| 205 | + } |
| 206 | + |
| 207 | + for (int i = 0; i < MessageProperties.Count; ++i) |
| 208 | + { |
| 209 | + var messageProperty = MessageProperties[i]; |
| 210 | + if (string.IsNullOrEmpty(messageProperty.Name)) |
| 211 | + continue; |
| 212 | + |
| 213 | + var propertyValue = RenderLogEvent(messageProperty.Layout, logEvent); |
| 214 | + if (string.IsNullOrEmpty(propertyValue) && !messageProperty.IncludeEmptyValue) |
| 215 | + continue; |
| 216 | + |
| 217 | + cloudEvent.ExtensionAttributes.Add(messageProperty.Name, propertyValue); |
| 218 | + } |
| 219 | + |
| 220 | + return cloudEvent; |
| 221 | + } |
| 222 | + |
| 223 | + private EventGridEvent CreateGridEvent(LogEventInfo logEvent) |
| 224 | + { |
| 225 | + var eventDataBody = RenderLogEvent(Layout, logEvent) ?? string.Empty; |
| 226 | + var eventSubject = RenderLogEvent(GridEventSubject, logEvent) ?? string.Empty; |
| 227 | + var eventType = RenderLogEvent(EventType, logEvent) ?? string.Empty; |
| 228 | + var eventDataSchema = RenderLogEvent(DataSchema, logEvent) ?? string.Empty; |
| 229 | + var gridEvent = new EventGridEvent(eventSubject, eventType, eventDataSchema, new BinaryData(EncodeToUTF8(eventDataBody))); |
| 230 | + gridEvent.EventTime = logEvent.TimeStamp.ToUniversalTime(); |
| 231 | + return gridEvent; |
| 232 | + } |
| 233 | + |
| 234 | + private byte[] EncodeToUTF8(string eventDataBody) |
| 235 | + { |
| 236 | + if (eventDataBody.Length < _reusableEncodingBuffer.Length) |
| 237 | + { |
| 238 | + lock (_reusableEncodingBuffer) |
| 239 | + { |
| 240 | + eventDataBody.CopyTo(0, _reusableEncodingBuffer, 0, eventDataBody.Length); |
| 241 | + return Encoding.UTF8.GetBytes(_reusableEncodingBuffer, 0, eventDataBody.Length); |
| 242 | + } |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + return Encoding.UTF8.GetBytes(eventDataBody); // Calls string.ToCharArray() |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + private static bool IsValidExtensionAttribue(string propertyValue) |
| 251 | + { |
| 252 | + for (int i = 0; i < propertyValue.Length; ++i) |
| 253 | + { |
| 254 | + char chr = propertyValue[i]; |
| 255 | + if (IsInvalidExtensionAttribueChar(chr)) |
| 256 | + { |
| 257 | + return false; |
| 258 | + } |
| 259 | + } |
| 260 | + return true; |
| 261 | + } |
| 262 | + |
| 263 | + private static bool IsInvalidExtensionAttribueChar(char chr) |
| 264 | + { |
| 265 | + return (chr < 'a' || chr > 'z') && (chr < '0' || chr > '9'); |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments