Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit b39b7e8

Browse files
committed
Merge branch 'dev'
2 parents dc39b86 + 5fe45bd commit b39b7e8

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ This example shows the options that are currently available when using the appSe
6363
<add key="serilog:write-to:Elasticsearch.batchPostingLimit" value="50"/>
6464
<add key="serilog:write-to:Elasticsearch.period" value="2"/>
6565
<add key="serilog:write-to:Elasticsearch.inlineFields" value="true"/>
66-
<add key="serilog:write-to:Elasticsearch.minimumLogEventLevel" value="Warning"/>
66+
<add key="serilog:write-to:Elasticsearch.restrictedToMinimumLevel" value="Warning"/>
6767
<add key="serilog:write-to:Elasticsearch.bufferBaseFilename" value="C:\Temp\SerilogElasticBuffer"/>
6868
<add key="serilog:write-to:Elasticsearch.bufferFileSizeLimitBytes" value="5242880"/>
6969
<add key="serilog:write-to:Elasticsearch.bufferLogShippingInterval" value="5000"/>
@@ -182,7 +182,7 @@ In your `appsettings.json` file, under the `Serilog` node, :
182182
"batchPostingLimit": 50,
183183
"period": 2000,
184184
"inlineFields": true,
185-
"minimumLogEventLevel": "Warning",
185+
"restrictedToMinimumLevel": "Warning",
186186
"bufferBaseFilename": "C:/Temp/LogDigipolis/docker-elk-serilog-web-buffer",
187187
"bufferFileSizeLimitBytes": 5242880,
188188
"bufferLogShippingInterval": 5000,

src/Serilog.Formatting.Elasticsearch/ElasticsearchJsonFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ protected void WriteSingleException(Exception exception, ref string delim, TextW
247247

248248
private void WriteMultilineString(string name, string value, ref string delimeter, TextWriter output)
249249
{
250-
string[] lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
250+
var lines = value?.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) ?? new string[] { };
251251
WriteJsonArrayProperty(name, lines, ref delimeter, output);
252252
}
253253

src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/Durable/Elasticsearch/ElasticsearchLogClient.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<SentPayloadResult> SendPayloadAsync(List<string> payload)
3535
return await SendPayloadAsync(payload, true);
3636
}
3737

38-
public async Task<SentPayloadResult> SendPayloadAsync(List<string> payload,bool first)
38+
public async Task<SentPayloadResult> SendPayloadAsync(List<string> payload, bool first)
3939
{
4040
try
4141
{
@@ -45,10 +45,10 @@ public async Task<SentPayloadResult> SendPayloadAsync(List<string> payload,bool
4545
if (response.Success)
4646
{
4747
var cleanPayload = new List<string>();
48-
var invalidPayload = GetInvalidPayloadAsync(response, payload,out cleanPayload);
49-
if ((cleanPayload?.Any() ?? false) && first)
48+
var invalidPayload = GetInvalidPayloadAsync(response, payload, out cleanPayload);
49+
if ((cleanPayload?.Any() == false) && first)
5050
{
51-
await SendPayloadAsync(cleanPayload,false);
51+
await SendPayloadAsync(cleanPayload, false);
5252
}
5353

5454
return new SentPayloadResult(response, true, invalidPayload);
@@ -85,16 +85,17 @@ private InvalidResult GetInvalidPayloadAsync(DynamicResponse baseResult, List<st
8585
bool hasErrors = false;
8686
foreach (dynamic item in items)
8787
{
88-
long? status = item.index?.status;
88+
var itemIndex = item?["index"];
89+
long? status = itemIndex?["status"];
8990
i++;
9091
if (!status.HasValue || status < 300)
9192
{
9293
continue;
9394
}
9495

9596
hasErrors = true;
96-
var id = item.index?._id;
97-
var error = item.index?.error;
97+
var id = itemIndex?["_id"];
98+
var error = itemIndex?["error"];
9899
if (int.TryParse(id.Split('_')[0], out int index))
99100
{
100101
SelfLog.WriteLine("Received failed ElasticSearch shipping result {0}: {1}. Failed payload : {2}.", status, error?.ToString(), payload.ElementAt(index * 2 + 1));

src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected override void EmitBatch(IEnumerable<LogEvent> events)
7272
var items = result.Body["items"];
7373
foreach (var item in items)
7474
{
75-
if (item["index"] != null && item["index"]["error"] != null)
75+
if (item["index"] != null && HasProperty(item["index"], "error") && item["index"]["error"] != null)
7676
{
7777
var e = events.ElementAt(indexer);
7878
if (_state.Options.EmitEventFailure.HasFlag(EmitEventFailureHandling.WriteToSelfLog))
@@ -219,5 +219,17 @@ protected virtual void HandleException(Exception ex, IEnumerable<LogEvent> event
219219
if (_state.Options.EmitEventFailure.HasFlag(EmitEventFailureHandling.ThrowException))
220220
throw ex;
221221
}
222+
223+
// Helper function: checks if a given dynamic member / dictionary key exists at runtime
224+
private static bool HasProperty(dynamic settings, string name)
225+
{
226+
if (settings is System.Dynamic.ExpandoObject)
227+
return ((IDictionary<string, object>)settings).ContainsKey(name);
228+
229+
if (settings is System.Dynamic.DynamicObject)
230+
return ((System.Dynamic.DynamicObject)settings).GetDynamicMemberNames().Contains(name);
231+
232+
return settings.GetType().GetProperty(name) != null;
233+
}
222234
}
223235
}

test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchJsonFormatterTests.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static LogEvent CreateLogEvent() =>
1919
DateTimeOffset.Now,
2020
LogEventLevel.Debug,
2121
//exception: CreateThrownException(),
22-
exception: null,
22+
exception: null,
2323
messageTemplate: new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()),
2424
properties: Enumerable.Empty<LogEventProperty>()
2525
);
@@ -55,12 +55,26 @@ static void ThrowInnerException()
5555
throw new Exception("Test inner exception message");
5656
}
5757

58-
static LogEvent CreateLogEventWithException() =>
58+
static Exception CreateThrownExceptionWithNotThrownInner()
59+
{
60+
Exception retEx = null;
61+
try
62+
{
63+
throw new Exception("Test exception message", new Exception("Test inner exception message"));
64+
}
65+
catch (Exception ex)
66+
{
67+
retEx = ex;
68+
}
69+
return retEx;
70+
}
71+
72+
static LogEvent CreateLogEventWithException(Exception ex) =>
5973
new LogEvent
6074
(
6175
DateTimeOffset.Now,
6276
LogEventLevel.Debug,
63-
exception: CreateThrownException(),
77+
exception: ex,
6478
messageTemplate: new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()),
6579
properties: Enumerable.Empty<LogEventProperty>()
6680
);
@@ -181,7 +195,7 @@ public void When_provide_closing_delimiter_should_use_it()
181195
public void DefaultJsonFormater_Should_Render_Exceptions()
182196
{
183197
CheckProperties(
184-
CreateLogEventWithException,
198+
() => CreateLogEventWithException(CreateThrownException()),
185199
new ElasticsearchJsonFormatter(),
186200
result =>
187201
{
@@ -205,11 +219,12 @@ public void DefaultJsonFormater_Should_Render_Exceptions()
205219
});
206220
}
207221

208-
[Fact]
209-
public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Array()
222+
[Theory]
223+
[MemberData(nameof(TestExceptions))]
224+
public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Array(Exception exception)
210225
{
211226
CheckProperties(
212-
CreateLogEventWithException,
227+
() => CreateLogEventWithException(exception),
213228
new ElasticsearchJsonFormatter(formatStackTraceAsArray: true),
214229
result =>
215230
{
@@ -232,5 +247,13 @@ public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Arra
232247
Assert.StartsWith("[", stackTraceValue);
233248
});
234249
}
250+
251+
public static IEnumerable<object[]> TestExceptions =>
252+
new List<object[]>
253+
{
254+
new object[] { CreateThrownException() },
255+
new object[] { CreateThrownExceptionWithNotThrownInner() },
256+
new object[] { new Exception("Not thrown exception message") }
257+
};
235258
}
236259
}

0 commit comments

Comments
 (0)