This repository was archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathExceptionAsJsonObjectFormatterTests.cs
More file actions
117 lines (93 loc) · 4.01 KB
/
ExceptionAsJsonObjectFormatterTests.cs
File metadata and controls
117 lines (93 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Xunit;
using Serilog.Events;
using Serilog.Parsing;
using Serilog.Sinks.Elasticsearch.Tests.Domain;
namespace Serilog.Sinks.Elasticsearch.Tests
{
public class ExceptionAsJsonObjectFormatterTests : ElasticsearchSinkTestsBase
{
private static readonly MessageTemplateParser _messageTemplateParser = new MessageTemplateParser();
public ExceptionAsJsonObjectFormatterTests() : base()
{
_options.CustomFormatter = new ExceptionAsObjectJsonFormatter(renderMessage:true);
}
[Fact]
public void WhenLoggingAnEvent_OutputsValidJson()
{
const string expectedMessage = "test";
using (var sink = new ElasticsearchSink(_options))
{
sink.Emit(LogEventWithMessage(expectedMessage));
}
var eventWritten = AssertAndGetJsonEvents().First();
eventWritten.Level.Should().Be(LogEventLevel.Warning);
eventWritten.Message.Should().Be(expectedMessage);
}
[Fact]
public void WhenLogging_WithException_ExceptionShouldBeRenderedInExceptionField()
{
const string expectedExceptionMessage = "test exception";
using (var sink = new ElasticsearchSink(_options))
{
sink.Emit(LogEventWithMessage("test", new Exception(expectedExceptionMessage)));
}
var eventWritten = AssertAndGetJsonEvents().First();
var exceptionInfo = eventWritten.Exception;
exceptionInfo.Should().NotBeNull();
exceptionInfo.Message.Should().Be(expectedExceptionMessage);
#if !DOTNETCORE
exceptionInfo.ClassName.Should().Be("System.Exception");
#endif
}
[Fact]
public void WhenLogging_ExceptionWithInner_ExceptionShouldIncludeInnerExceptions()
{
var inner = new InvalidOperationException();
var exception = new Exception("outer", inner);
using (var sink = new ElasticsearchSink(_options))
{
sink.Emit(LogEventWithMessage("test", exception));
}
var eventWritten = AssertAndGetJsonEvents().First();
var exceptionInfo = eventWritten.Exception;
exceptionInfo.InnerException.Should().NotBeNull();
}
private static LogEvent LogEventWithMessage(string expectedMessage, Exception exception = null)
{
var template = _messageTemplateParser.Parse(expectedMessage);
return new LogEvent(DateTimeOffset.Now, LogEventLevel.Warning, exception, template, Enumerable.Empty<LogEventProperty>());
}
private IEnumerable<KibanaFriendlyJsonEvent> AssertAndGetJsonEvents()
{
_seenHttpPosts.Should().NotBeEmpty();
return _seenHttpPosts.SelectMany(postedData => postedData.Split(new char[] { '\n'}, StringSplitOptions.RemoveEmptyEntries))
.Where((_,i) => i % 2 == 1)
.Select(JsonConvert.DeserializeObject<KibanaFriendlyJsonEvent>);
}
class KibanaFriendlyJsonEvent : IBulkData
{
[JsonProperty("@timestamp")]
public DateTime Timestamp { get; set; }
[JsonProperty("level")]
[JsonConverter(typeof(StringEnumConverter))]
public LogEventLevel Level { get; set; }
[JsonProperty("messageTemplate")]
public string MessageTemplate { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("exception")]
public SerilogElasticsearchExceptionInfoWithInner Exception { get; set; }
}
class SerilogElasticsearchExceptionInfoWithInner : SerilogElasticsearchExceptionInfo
{
[JsonProperty("innerException")]
public SerilogElasticsearchExceptionInfo InnerException { get; set; }
}
}
}