Skip to content

Commit f46622a

Browse files
Serilog configuration options fix (#22)
* Add testing. * And this is why we write Unit Tests.
1 parent 77178f8 commit f46622a

File tree

4 files changed

+163
-35
lines changed

4 files changed

+163
-35
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
11+
<PackageReference Include="Core.Arango" Version="3.12.0"/>
12+
<PackageReference Include="Serilog" Version="4.0.0"/>
13+
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="5.0.0"/>
14+
<PackageReference Include="Testcontainers.ArangoDb" Version="4.3.0"/>
15+
<PackageReference Include="xunit" Version="2.9.3"/>
16+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\Core.Arango.Serilog\Core.Arango.Serilog.csproj"/>
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Serilog;
2+
using Serilog.Sinks.PeriodicBatching;
3+
using Testcontainers.ArangoDb;
4+
5+
namespace Core.Arango.Serilog.Tests;
6+
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
public class SerilogUnitTests : IAsyncLifetime
11+
{
12+
private const string DatabaseName = "test";
13+
private const string CollectionName = "logs";
14+
15+
private ArangoDbContainer? _arangoDbContainer;
16+
private Func<IArangoConfiguration>? _createArangoConfiguration;
17+
18+
public async Task InitializeAsync()
19+
{
20+
_arangoDbContainer = new ArangoDbBuilder()
21+
.WithPassword("password")
22+
.Build();
23+
await _arangoDbContainer.StartAsync();
24+
25+
_createArangoConfiguration = () => new ArangoConfiguration
26+
{
27+
ConnectionString =
28+
$"Server={_arangoDbContainer.GetTransportAddress()};User=root;Realm=CI-{Guid.NewGuid():D};Password=password;",
29+
};
30+
}
31+
32+
public async Task DisposeAsync()
33+
{
34+
await _arangoDbContainer!.DisposeAsync();
35+
}
36+
37+
[Fact]
38+
public async Task DefaultDatabaseIsCreated()
39+
{
40+
var config = _createArangoConfiguration!();
41+
var arango = new ArangoContext(config);
42+
43+
Log.Logger = new LoggerConfiguration()
44+
.WriteTo.Sink(new PeriodicBatchingSink(
45+
new ArangoSerilogSink(arango,
46+
"logs",
47+
"logs"),
48+
new()
49+
{
50+
BatchSizeLimit = 1000,
51+
QueueLimit = 100000,
52+
Period = TimeSpan.FromSeconds(2),
53+
EagerlyEmitFirstEvent = true,
54+
}
55+
))
56+
.MinimumLevel.Debug()
57+
.CreateLogger();
58+
59+
Assert.True(await arango.Database.ExistAsync("logs"));
60+
Assert.True(await arango.Collection.ExistAsync("logs","logs"));
61+
}
62+
63+
[Fact]
64+
public async Task DatabaseAndIndexesAreCreated()
65+
{
66+
var config = _createArangoConfiguration!();
67+
var arango = new ArangoContext(config);
68+
69+
Log.Logger = new LoggerConfiguration()
70+
.WriteTo.Sink(new PeriodicBatchingSink(
71+
new ArangoSerilogSink(arango,
72+
DatabaseName,
73+
CollectionName,
74+
ArangoSerilogSink.LoggingRenderStrategy.StoreTemplate,
75+
true,
76+
true,
77+
true),
78+
new()
79+
{
80+
BatchSizeLimit = 1000,
81+
QueueLimit = 100000,
82+
Period = TimeSpan.FromSeconds(2),
83+
EagerlyEmitFirstEvent = true,
84+
}
85+
))
86+
.MinimumLevel.Debug()
87+
.CreateLogger();
88+
89+
var indexes = await arango.Index.ListAsync(DatabaseName, CollectionName);
90+
91+
Assert.True(await arango.Database.ExistAsync(DatabaseName));
92+
Assert.True(await arango.Collection.ExistAsync(DatabaseName, CollectionName));
93+
94+
Assert.Contains(indexes!, x => x.Name == nameof(ArangoSerilogSink.LogEventEntity.Level));
95+
Assert.Contains(indexes!, x => x.Name == nameof(ArangoSerilogSink.LogEventEntity.Timestamp));
96+
Assert.Contains(indexes!, x => x.Name == nameof(ArangoSerilogSink.LogEventEntity.MessageTemplate));
97+
}
98+
}

Core.Arango.Serilog/ArangoSerilogSink.cs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,72 +34,70 @@ public ArangoSerilogSink(
3434
LoggingRenderStrategy renderMessage = LoggingRenderStrategy.RenderMessage,
3535
bool indexLevel = false,
3636
bool indexTimestamp = false,
37-
bool indexTemplate = false) : this(arango, database, collection)
37+
bool indexTemplate = false)
3838
{
39+
_arango = arango;
40+
_database = database;
41+
_collection = collection;
3942
_renderMessage = renderMessage;
4043
_indexLevel = indexLevel;
4144
_indexTimestamp = indexTimestamp;
4245
_indexTemplate = indexTemplate;
46+
47+
Setup().GetAwaiter().GetResult();
4348
}
4449

45-
46-
public ArangoSerilogSink(
47-
IArangoContext arango,
48-
string database = "logs",
49-
string collection = "logs")
50+
private async Task Setup()
5051
{
51-
_arango = arango;
52-
_database = database;
53-
_collection = collection;
54-
5552
try
5653
{
57-
if (!_arango.Database.ExistAsync(_database).AsTask().GetAwaiter().GetResult())
58-
_arango.Database.CreateAsync(_database).AsTask().Wait();
59-
60-
var indexes = _arango.Index.ListAsync(_database, _collection)
61-
.AsTask().GetAwaiter().GetResult();
54+
if (!await _arango.Database.ExistAsync(_database))
55+
{
56+
await _arango.Database.CreateAsync(_database);
57+
}
6258

63-
if (!_arango.Collection.ExistAsync(_database, collection).AsTask().GetAwaiter().GetResult())
59+
if (!await _arango.Collection.ExistAsync(_database, _collection))
6460
{
65-
_arango.Collection.CreateAsync(_database, new ArangoCollection
61+
await _arango.Collection.CreateAsync(_database, new ArangoCollection
6662
{
6763
Name = _collection,
6864
KeyOptions = new ArangoKeyOptions
6965
{
7066
Type = ArangoKeyType.Padded
7167
}
72-
}).AsTask().Wait();
68+
});
7369
}
7470

75-
if (_indexLevel &&
76-
indexes.Any(x => x.Name == nameof(LogEventEntity.Level)))
71+
var indexes = (await _arango.Index.ListAsync(_database, _collection)).ToList();
72+
73+
if (_indexLevel && indexes.All(x => x.Name != nameof(LogEventEntity.Level)))
7774
{
78-
_arango.Index.CreateAsync(_database, _collection, new ArangoIndex
75+
await _arango.Index.CreateAsync(_database, _collection, new ArangoIndex
7976
{
8077
Type = ArangoIndexType.Persistent,
81-
Name = nameof(LogEventEntity.Level)
82-
}).AsTask().Wait();
78+
Name = nameof(LogEventEntity.Level),
79+
Fields = [nameof(LogEventEntity.Level)]
80+
});
8381
}
8482

85-
if (_indexTimestamp &&
86-
indexes.Any(x => x.Name == nameof(LogEventEntity.Timestamp)))
83+
if (_indexTimestamp && indexes.All(x => x.Name != nameof(LogEventEntity.Timestamp)))
8784
{
88-
_arango.Index.CreateAsync(_database, _collection, new ArangoIndex
85+
await _arango.Index.CreateAsync(_database, _collection, new ArangoIndex
8986
{
9087
Type = ArangoIndexType.Persistent,
91-
Name = nameof(LogEventEntity.Timestamp)
92-
}).AsTask().Wait();
88+
Name = nameof(LogEventEntity.Timestamp),
89+
Fields = [nameof(LogEventEntity.Timestamp)]
90+
});
9391
}
94-
95-
if (_indexTemplate &&
96-
indexes.Any(x => x.Name == nameof(LogEventEntity.MessageTemplate)))
92+
93+
if (_indexTemplate && indexes.All(x => x.Name != nameof(LogEventEntity.MessageTemplate)))
9794
{
98-
_arango.Index.CreateAsync(_database, _collection, new ArangoIndex
95+
await _arango.Index.CreateAsync(_database, _collection, new ArangoIndex
9996
{
10097
Type = ArangoIndexType.Persistent,
101-
Name = nameof(LogEventEntity.MessageTemplate)
102-
}).AsTask().Wait();
98+
Name = nameof(LogEventEntity.MessageTemplate),
99+
Fields = [nameof(LogEventEntity.MessageTemplate)]
100+
});
103101
}
104102
}
105103
catch (Exception)
@@ -114,7 +112,7 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
114112
{
115113
var renderMessage = _renderMessage.HasFlag(LoggingRenderStrategy.RenderMessage);
116114
var storeTemplate = _renderMessage.HasFlag(LoggingRenderStrategy.StoreTemplate);
117-
115+
118116
await _arango.Document.CreateManyAsync(_database, _collection, events.Select(x => new LogEventEntity
119117
{
120118
Level = x.Level.ToString(),

Core.Arango.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Arango.Migration", "Co
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Arango.Migration.Tests", "Core.Arango.Migration.Tests\Core.Arango.Migration.Tests.csproj", "{EDDB495F-5E0E-495B-8DEF-BEF495B9F8AA}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Arango.Serilog.Tests", "Core.Arango.Serilog.Tests\Core.Arango.Serilog.Tests.csproj", "{FE0F07AC-61FE-4757-9474-C4BC2E54C8AE}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{EDDB495F-5E0E-495B-8DEF-BEF495B9F8AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{EDDB495F-5E0E-495B-8DEF-BEF495B9F8AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{EDDB495F-5E0E-495B-8DEF-BEF495B9F8AA}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{FE0F07AC-61FE-4757-9474-C4BC2E54C8AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{FE0F07AC-61FE-4757-9474-C4BC2E54C8AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{FE0F07AC-61FE-4757-9474-C4BC2E54C8AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{FE0F07AC-61FE-4757-9474-C4BC2E54C8AE}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)