-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathLogEventPropFilterTests.cs
164 lines (139 loc) · 5.21 KB
/
LogEventPropFilterTests.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using System.Collections.Generic;
using System.Linq;
using Elastic.Apm.SerilogEnricher;
using FluentAssertions;
using Serilog;
using Serilog.Events;
using Serilog.Parsing;
using Xunit;
using Xunit.Abstractions;
namespace Elastic.CommonSchema.Serilog.Tests
{
public class LogEventPropFilterTests : LogTestsBase
{
public LogEventPropFilterTests(ITestOutputHelper output) : base(output)
{
LoggerConfiguration = LoggerConfiguration
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.Enrich.WithMachineName()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithEnvironmentUserName()
.Enrich.WithElasticApmCorrelationInfo();
var config = new EcsTextFormatterConfiguration { LogEventPropertiesToFilter = new HashSet<string> { "foo" } };
Formatter = new EcsTextFormatter(config);
}
private LogEvent BuildLogEvent()
{
var parser = new MessageTemplateParser();
return new LogEvent(
DateTimeOffset.Now,
LogEventLevel.Information,
null,
parser.Parse("My Log message!"),
new[]
{
new LogEventProperty("foo", new ScalarValue("aaa")),
new LogEventProperty("bar", new ScalarValue("bbb")),
});
}
/// <summary>
/// Test the default <see cref="EcsTextFormatterConfiguration.LogEventPropertiesToFilter"/> via a hashset
/// </summary>
[Fact]
public void FilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var evnt = BuildLogEvent();
logger.Write(evnt);
var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Labels.Should().Contain("bar", "bbb");
info.Labels.Should().NotContainKey("foo", "Should have been filtered");
});
/// <summary>
/// Test that null <see cref="EcsTextFormatterConfiguration.LogEventPropertiesToFilter"/> does not cause any critical errors
/// </summary>
[Fact]
public void NullFilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var config = new EcsTextFormatterConfiguration { LogEventPropertiesToFilter = null };
Formatter = new EcsTextFormatter(config);
var evnt = BuildLogEvent();
logger.Write(evnt);
var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Labels.Should().Contain("bar", "bbb");
info.Labels.Should().Contain("foo", "aaa");
});
/// <summary>
/// Test that <see cref="EcsTextFormatterConfiguration.LogEventPropertiesToFilter"/> can be empty and does not cause any critical errors
/// </summary>
[Fact]
public void EmptyFilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var config = new EcsTextFormatterConfiguration { LogEventPropertiesToFilter = new HashSet<string>() };
Formatter = new EcsTextFormatter(config);
var evnt = BuildLogEvent();
logger.Write(evnt);
var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Labels.Should().Contain("bar", "bbb");
info.Labels.Should().Contain("foo", "aaa");
});
/// <summary>
/// Test that <see cref="EcsTextFormatterConfiguration.LogEventPropertiesToFilter"/> can be case insensitive
/// </summary>
[Fact]
public void CaseInsensitiveFilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var config = new EcsTextFormatterConfiguration { LogEventPropertiesToFilter = new HashSet<string>(StringComparer.OrdinalIgnoreCase){{ "FOO" }} };
Formatter = new EcsTextFormatter(config);
var evnt = BuildLogEvent();
logger.Write(evnt);
var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Labels.Should().Contain("bar", "bbb");
info.Labels.Should().NotContainKey("foo", "Should have been filtered");
});
/// <summary>
/// Test that <see cref="EcsTextFormatterConfiguration.LogEventPropertiesToFilter"/> can be case sensitive
/// </summary>
[Fact]
public void CaseSensitiveFilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var config = new EcsTextFormatterConfiguration { LogEventPropertiesToFilter = new HashSet<string>(StringComparer.Ordinal){{ "FOO" }} };
Formatter = new EcsTextFormatter(config);
var evnt = BuildLogEvent();
logger.Write(evnt);
var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Labels.Should().Contain("bar", "bbb");
info.Labels.Should().Contain("foo", "aaa");
});
}
}