Skip to content

Commit 61a6603

Browse files
author
Thibaud DESODT
committed
Add more "end-to-end" tests for ConfigurationSettings (WIP)
1 parent c342d12 commit 61a6603

22 files changed

+621
-2
lines changed

serilog-settings-configuration.sln

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.10
4+
VisualStudioVersion = 15.0.27130.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4E41FD57-5FAB-4E3C-B16E-463DE98338BC}"
77
EndProject
@@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Settings.Configurat
2424
EndProject
2525
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "sample\Sample\Sample.csproj", "{A00E5E32-54F9-401A-BBA1-2F6FCB6366CD}"
2626
EndProject
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestDummies", "test\TestDummies\TestDummies.csproj", "{B7CF5068-DD19-4868-A268-5280BDE90361}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +44,10 @@ Global
4244
{A00E5E32-54F9-401A-BBA1-2F6FCB6366CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{A00E5E32-54F9-401A-BBA1-2F6FCB6366CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{A00E5E32-54F9-401A-BBA1-2F6FCB6366CD}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{B7CF5068-DD19-4868-A268-5280BDE90361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{B7CF5068-DD19-4868-A268-5280BDE90361}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{B7CF5068-DD19-4868-A268-5280BDE90361}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{B7CF5068-DD19-4868-A268-5280BDE90361}.Release|Any CPU.Build.0 = Release|Any CPU
4551
EndGlobalSection
4652
GlobalSection(SolutionProperties) = preSolution
4753
HideSolutionNode = FALSE
@@ -50,5 +56,9 @@ Global
5056
{21FF98ED-E68C-4A67-B241-C8D6122FAD7D} = {4E41FD57-5FAB-4E3C-B16E-463DE98338BC}
5157
{F793C6E8-C40A-4018-8884-C97E2BE38A54} = {D551DCB0-7771-4D01-BEBD-F7B57D1CF0E3}
5258
{A00E5E32-54F9-401A-BBA1-2F6FCB6366CD} = {D24872B9-57F3-42A7-BC8D-F9DA222FCE1B}
59+
{B7CF5068-DD19-4868-A268-5280BDE90361} = {D551DCB0-7771-4D01-BEBD-F7B57D1CF0E3}
60+
EndGlobalSection
61+
GlobalSection(ExtensibilityGlobals) = postSolution
62+
SolutionGuid = {485F8843-42D7-4267-B5FB-20FE9181DEE9}
5363
EndGlobalSection
5464
EndGlobal

src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ItemGroup>
2525
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />
2626
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="1.0.0" />
27-
<PackageReference Include="Serilog" Version="2.0.0" />
27+
<PackageReference Include="Serilog" Version="2.5.0" />
2828
</ItemGroup>
2929

3030
<PropertyGroup Condition=" '$(TargetFramework)' == 'net451' ">
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using Microsoft.Extensions.Configuration;
3+
using Serilog.Events;
4+
using Serilog.Settings.Configuration.Tests.Support;
5+
using TestDummies;
6+
using TestDummies.Console;
7+
using Xunit;
8+
9+
namespace Serilog.Settings.Configuration.Tests
10+
{
11+
public class ConfigurationSettingsTests
12+
{
13+
private static LoggerConfiguration ConfigFromJson(string jsonString)
14+
{
15+
var config = new ConfigurationBuilder().AddJsonString(jsonString).Build();
16+
return new LoggerConfiguration()
17+
.ReadFrom.Configuration(config);
18+
}
19+
20+
[Fact]
21+
public void PropertyEnrichmentIsApplied()
22+
{
23+
LogEvent evt = null;
24+
25+
var json = @"{
26+
""Serilog"": {
27+
""Properties"": {
28+
""App"": ""Test""
29+
}
30+
}
31+
}";
32+
33+
var log = ConfigFromJson(json)
34+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
35+
.CreateLogger();
36+
37+
log.Information("Has a test property");
38+
39+
Assert.NotNull(evt);
40+
Assert.Equal("Test", evt.Properties["App"].LiteralValue());
41+
}
42+
43+
44+
[Fact]
45+
public void SinksAreConfigured()
46+
{
47+
var json = @"{
48+
""Serilog"": {
49+
""Using"": [""TestDummies""],
50+
""WriteTo"": [{
51+
""Name"": ""DummyRollingFile"",
52+
""Args"": {""pathFormat"" : ""C:\\""}
53+
}]
54+
}
55+
}";
56+
57+
var log = ConfigFromJson(json)
58+
.CreateLogger();
59+
60+
DummyRollingFileSink.Emitted.Clear();
61+
DummyRollingFileAuditSink.Emitted.Clear();
62+
63+
log.Write(Some.InformationEvent());
64+
65+
Assert.Equal(1, DummyRollingFileSink.Emitted.Count);
66+
Assert.Equal(0, DummyRollingFileAuditSink.Emitted.Count);
67+
}
68+
69+
//[Fact]
70+
//public void AuditSinksAreConfigured()
71+
//{
72+
// var settings = new Dictionary<string, string>
73+
// {
74+
// ["using:TestDummies"] = typeof(DummyLoggerConfigurationExtensions).GetTypeInfo().Assembly.FullName,
75+
// ["audit-to:DummyRollingFile.pathFormat"] = "C:\\"
76+
// };
77+
78+
// var log = new LoggerConfiguration()
79+
// .ReadFrom.KeyValuePairs(settings)
80+
// .CreateLogger();
81+
82+
// DummyRollingFileSink.Emitted.Clear();
83+
// DummyRollingFileAuditSink.Emitted.Clear();
84+
85+
// log.Write(Some.InformationEvent());
86+
87+
// Assert.Equal(0, DummyRollingFileSink.Emitted.Count);
88+
// Assert.Equal(1, DummyRollingFileAuditSink.Emitted.Count);
89+
//}
90+
91+
[Fact]
92+
public void TestMinimumLevelOverrides()
93+
{
94+
var json = @"{
95+
""Serilog"": {
96+
""MinimumLevel"" : {
97+
""Override"" : {
98+
""System"" : ""Warning""
99+
}
100+
}
101+
}
102+
}";
103+
104+
LogEvent evt = null;
105+
106+
var log = ConfigFromJson(json)
107+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
108+
.CreateLogger();
109+
110+
var systemLogger = log.ForContext<WeakReference>();
111+
systemLogger.Write(Some.InformationEvent());
112+
113+
Assert.Null(evt);
114+
115+
systemLogger.Warning("Bad things");
116+
Assert.NotNull(evt);
117+
118+
evt = null;
119+
log.Write(Some.InformationEvent());
120+
Assert.NotNull(evt);
121+
}
122+
123+
[Fact]
124+
public void SinksWithAbstractParamsAreConfiguredWithTypeName()
125+
{
126+
var json = @"{
127+
""Serilog"": {
128+
""Using"": [""TestDummies""],
129+
""WriteTo"": [{
130+
""Name"": ""DummyConsole"",
131+
""Args"": {""theme"" : ""Serilog.Settings.Configuration.Tests.Support.CustomConsoleTheme, Serilog.Settings.Configuration.Tests""}
132+
}]
133+
}
134+
}";
135+
136+
DummyConsoleSink.Theme = null;
137+
138+
ConfigFromJson(json)
139+
.CreateLogger();
140+
141+
Assert.NotNull(DummyConsoleSink.Theme);
142+
Assert.IsType<CustomConsoleTheme>(DummyConsoleSink.Theme);
143+
}
144+
145+
//[Fact]
146+
//public void SinksAreConfiguredWithStaticMember()
147+
//{
148+
// var json = @"{
149+
// ""Serilog"": {
150+
// ""Using"": [""TestDummies""],
151+
// ""WriteTo"": [{
152+
// ""Name"": ""DummyConsole"",
153+
// ""Args"": {""theme"" : ""TestDummies.Console.Themes.ConsoleThemes::Theme1, TestDummies""}
154+
// }]
155+
// }
156+
// }";
157+
158+
// DummyConsoleSink.Theme = null;
159+
160+
// ConfigFromJson(json)
161+
// .CreateLogger();
162+
163+
// Assert.Equal(ConsoleThemes.Theme1, DummyConsoleSink.Theme);
164+
//}
165+
}
166+
}

test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<ProjectReference Include="..\..\src\Serilog.Settings.Configuration\Serilog.Settings.Configuration.csproj" />
13+
<ProjectReference Include="..\TestDummies\TestDummies.csproj" />
1314
</ItemGroup>
1415

1516
<ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
namespace Serilog.Settings.Configuration.Tests.Support
4+
{
5+
public static class ConfigurationBuilderExtensions
6+
{
7+
public static IConfigurationBuilder AddJsonString(this IConfigurationBuilder builder, string json)
8+
{
9+
return builder.Add(new JsonStringConfigSource(json));
10+
}
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using TestDummies.Console.Themes;
2+
3+
namespace Serilog.Settings.Configuration.Tests.Support
4+
{
5+
class CustomConsoleTheme : ConsoleTheme
6+
{
7+
}
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Serilog.Settings.Configuration.Tests.Support
6+
{
7+
public class DelegatingSink : ILogEventSink
8+
{
9+
readonly Action<LogEvent> _write;
10+
11+
public DelegatingSink(Action<LogEvent> write)
12+
{
13+
if (write == null) throw new ArgumentNullException(nameof(write));
14+
_write = write;
15+
}
16+
17+
public void Emit(LogEvent logEvent)
18+
{
19+
_write(logEvent);
20+
}
21+
22+
public static LogEvent GetLogEvent(Action<ILogger> writeAction)
23+
{
24+
LogEvent result = null;
25+
var l = new LoggerConfiguration()
26+
.MinimumLevel.Verbose()
27+
.WriteTo.Sink(new DelegatingSink(le => result = le))
28+
.CreateLogger();
29+
30+
writeAction(l);
31+
return result;
32+
}
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Serilog.Events;
2+
3+
namespace Serilog.Settings.Configuration.Tests.Support
4+
{
5+
public static class Extensions
6+
{
7+
public static object LiteralValue(this LogEventPropertyValue @this)
8+
{
9+
return ((ScalarValue)@this).Value;
10+
}
11+
}
12+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading;
6+
using Serilog.Core;
7+
using Serilog.Events;
8+
using Serilog.Parsing;
9+
10+
namespace Serilog.Settings.Configuration.Tests.Support
11+
{
12+
static class Some
13+
{
14+
static int Counter;
15+
16+
public static int Int()
17+
{
18+
return Interlocked.Increment(ref Counter);
19+
}
20+
21+
public static decimal Decimal()
22+
{
23+
return Int() + 0.123m;
24+
}
25+
26+
public static string String(string tag = null)
27+
{
28+
return (tag ?? "") + "__" + Int();
29+
}
30+
31+
public static TimeSpan TimeSpan()
32+
{
33+
return System.TimeSpan.FromMinutes(Int());
34+
}
35+
36+
public static DateTime Instant()
37+
{
38+
return new DateTime(2012, 10, 28) + TimeSpan();
39+
}
40+
41+
public static DateTimeOffset OffsetInstant()
42+
{
43+
return new DateTimeOffset(Instant());
44+
}
45+
46+
public static LogEvent LogEvent(string sourceContext, DateTimeOffset? timestamp = null, LogEventLevel level = LogEventLevel.Information)
47+
{
48+
return new LogEvent(timestamp ?? OffsetInstant(), level,
49+
null, MessageTemplate(),
50+
new List<LogEventProperty> { new LogEventProperty(Constants.SourceContextPropertyName, new ScalarValue(sourceContext)) });
51+
}
52+
53+
public static LogEvent LogEvent(DateTimeOffset? timestamp = null, LogEventLevel level = LogEventLevel.Information)
54+
{
55+
return new LogEvent(timestamp ?? OffsetInstant(), level,
56+
null, MessageTemplate(), Enumerable.Empty<LogEventProperty>());
57+
}
58+
59+
public static LogEvent InformationEvent(DateTimeOffset? timestamp = null)
60+
{
61+
return LogEvent(timestamp, LogEventLevel.Information);
62+
}
63+
64+
public static LogEvent DebugEvent(DateTimeOffset? timestamp = null)
65+
{
66+
return LogEvent(timestamp, LogEventLevel.Debug);
67+
}
68+
69+
public static LogEvent WarningEvent(DateTimeOffset? timestamp = null)
70+
{
71+
return LogEvent(timestamp, LogEventLevel.Warning);
72+
}
73+
74+
public static LogEventProperty LogEventProperty()
75+
{
76+
return new LogEventProperty(String(), new ScalarValue(Int()));
77+
}
78+
79+
public static string NonexistentTempFilePath()
80+
{
81+
return Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".txt");
82+
}
83+
84+
public static string TempFilePath()
85+
{
86+
return Path.GetTempFileName();
87+
}
88+
89+
public static string TempFolderPath()
90+
{
91+
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
92+
Directory.CreateDirectory(dir);
93+
return dir;
94+
}
95+
96+
public static MessageTemplate MessageTemplate()
97+
{
98+
return new MessageTemplateParser().Parse(String());
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)