-
Notifications
You must be signed in to change notification settings - Fork 62
/
GithubIssue167.cs
109 lines (93 loc) · 3.06 KB
/
GithubIssue167.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Elastic.CommonSchema.Serialization;
using FluentAssertions;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.TestCorrelator;
using Xunit;
using Xunit.Abstractions;
namespace Elastic.CommonSchema.Serilog.Tests.Repro
{
public class GithubIssue167
{
[JsonConverter(typeof(EcsDocumentJsonConverterFactory))]
public class ContosoDocument : EcsDocument
{
[JsonPropertyName("contoso"), DataMember(Name = "contoso")]
public Contoso Contoso { get; set; }
protected override bool TryRead(string propertyName, out Type type)
{
type = propertyName switch
{
"contoso" => typeof(Contoso),
_ => null
};
return type != null;
}
protected override bool ReceiveProperty(string propertyName, object value) =>
propertyName switch
{
"contoso" => null != (Contoso = value as Contoso),
_ => false
};
protected override void WriteAdditionalProperties(Action<string, object> write) => write("contoso", Contoso);
}
public class Contoso
{
[JsonPropertyName("company_name"), DataMember(Name = "company_name")]
public string CompanyName { get; set; }
}
public class ContosoEcsTextFormatter : EcsTextFormatter<ContosoDocument>
{
public override void Format(LogEvent logEvent, TextWriter output)
{
var ecsEvent = LogEventConverter.ConvertToEcs(logEvent, Configuration);
ecsEvent.Contoso = new Contoso { CompanyName = "Elastic", };
output.WriteLine(ecsEvent.Serialize());
}
}
private LoggerConfiguration LoggerConfiguration { get; }
private ContosoEcsTextFormatter Formatter { get; }
public GithubIssue167(ITestOutputHelper output)
{
Formatter = new ContosoEcsTextFormatter();
LoggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(Formatter)
.WriteTo.TestOutput(output, formatter: Formatter, LogEventLevel.Verbose)
.WriteTo.TestCorrelator();
}
[Fact]
public void CanFormatBaseImplementationOfEcsDocument()
{
using var context = TestCorrelator.CreateContext();
var logger = LoggerConfiguration.CreateLogger().ForContext(GetType());
logger.Information("My log message!");
var logEvents = TestCorrelator.GetLogEventsFromCurrentContext().ToList();
logEvents.Should().HaveCount(1);
var ecsEvents = ToEcsEvents(logEvents);
var (_, info) = ecsEvents.First();
info.Timestamp.Should().NotBeNull();
info.Contoso.Should().NotBeNull();
info.Contoso.CompanyName.Should().Be("Elastic");
}
private List<string> ToFormattedStrings(List<LogEvent> logEvents) =>
logEvents
.Select(l =>
{
using var stringWriter = new StringWriter();
Formatter.Format(l, stringWriter);
return stringWriter.ToString();
})
.ToList();
protected List<(string Json, ContosoDocument Base)> ToEcsEvents(List<LogEvent> logEvents) =>
ToFormattedStrings(logEvents)
.Select(s => (s, EcsSerializerFactory<ContosoDocument>.Deserialize(s)))
.ToList();
}
}