Skip to content

Commit 2cb8852

Browse files
authored
EventGridTarget - Added DataFormat-option instead checking JsonLayout (#149)
1 parent 16c33e9 commit 2cb8852

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/NLog.Extensions.AzureEventGrid/EventGridTarget.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,29 @@ public class EventGridTarget : AsyncTaskTarget
4545
/// <summary>
4646
/// Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON.
4747
/// </summary>
48-
public Layout ContentType { get; set; }
48+
public Layout ContentType
49+
{
50+
get => _contentType;
51+
set
52+
{
53+
_contentType = value;
54+
if (!_dataFormatJson.HasValue && value?.ToString()?.IndexOf("json", StringComparison.OrdinalIgnoreCase) >= 0)
55+
{
56+
_dataFormatJson = true;
57+
}
58+
}
59+
}
60+
private Layout _contentType;
61+
62+
/// <summary>
63+
/// The serialize format of the data object. Json / Binary
64+
/// </summary>
65+
public string DataFormat
66+
{
67+
get => _dataFormatJson == true ? "Json" : "Binary";
68+
set => _dataFormatJson = value?.IndexOf("Json", StringComparison.OrdinalIgnoreCase) >= 0;
69+
}
70+
private bool? _dataFormatJson;
4971

5072
/// <summary>
5173
/// The schema version of the data object.
@@ -207,7 +229,7 @@ private CloudEvent CreateCloudEvent(LogEventInfo logEvent)
207229
var eventType = RenderLogEvent(EventType, logEvent) ?? string.Empty;
208230
var eventDataSchema = RenderLogEvent(DataSchema, logEvent) ?? string.Empty;
209231
var eventContentType = RenderLogEvent(ContentType, logEvent) ?? string.Empty;
210-
var cloudEventFormat = Layout is JsonLayout ? CloudEventDataFormat.Json : CloudEventDataFormat.Binary;
232+
var cloudEventFormat = _dataFormatJson == true ? CloudEventDataFormat.Json : CloudEventDataFormat.Binary;
211233
var cloudEvent = new CloudEvent(eventSource, eventType, new BinaryData(EncodeToUTF8(eventDataBody)), eventContentType, cloudEventFormat);
212234
cloudEvent.Time = logEvent.TimeStamp.ToUniversalTime();
213235

src/NLog.Extensions.AzureEventGrid/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ _eventType_ - Type of the event. Ex. "Contoso.Items.ItemReceived". [Layout](http
4444

4545
_contentType_ - Content type of the data-payload. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
4646

47+
_dataFormat_ - Format of the data-payload (Binary / Json). Default Binary. `String`
48+
4749
_dataSchema_ - Schema version of the data-payload. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
4850

4951
_tenantIdentity_ - Input for DefaultAzureCredential. [Layout](https://github.com/NLog/NLog/wiki/Layouts)

0 commit comments

Comments
 (0)