Skip to content

Commit 0333c21

Browse files
committed
Added LevelSwitch integration test
1 parent e1f6a57 commit 0333c21

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Extensions/Hybrid/ConfigurationExtensionsTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using Microsoft.Extensions.Configuration;
5+
using Serilog.Core;
56
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
67
using Xunit;
78
using Xunit.Abstractions;
@@ -148,6 +149,50 @@ public void SinkOptionsFromConfigSection()
148149
VerifyDatabaseColumnsWereCreated(standardNames);
149150
}
150151

152+
[Fact]
153+
public void LogLevelSwitchIsApplied()
154+
{
155+
// Arrange
156+
var columnOptionsSection = TestConfiguration().GetSection(_columnOptionsSection);
157+
const string message1 = "info message 1";
158+
const string message2 = "error message 2";
159+
const string message3 = "info message 3";
160+
const string message4 = "error message 4";
161+
const string message5 = "info message 5";
162+
const string message6 = "error message 6";
163+
var levelSwitch = new LoggingLevelSwitch(Events.LogEventLevel.Information);
164+
165+
// Act
166+
var loggerConfiguration = new LoggerConfiguration();
167+
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
168+
connectionString: DatabaseFixture.LogEventsConnectionString,
169+
sinkOptions: new MSSqlServerSinkOptions
170+
{
171+
TableName = DatabaseFixture.LogTableName,
172+
AutoCreateSqlTable = true
173+
},
174+
columnOptionsSection: columnOptionsSection,
175+
formatProvider: CultureInfo.InvariantCulture,
176+
levelSwitch: levelSwitch)
177+
.CreateLogger();
178+
179+
Log.Information(message1);
180+
Log.Error(message2);
181+
levelSwitch.MinimumLevel = Events.LogEventLevel.Error;
182+
Log.Information(message3);
183+
Log.Error(message4);
184+
levelSwitch.MinimumLevel = Events.LogEventLevel.Information;
185+
Log.Information(message5);
186+
Log.Error(message6);
187+
188+
Log.CloseAndFlush();
189+
190+
// Assert
191+
var writtenMessages = new List<string> { message1, message2, message4, message5, message6 };
192+
var notWrittenMessages = new List<string> { message3 }; // Information message was filtered by level switch
193+
VerifyStringColumnMultipleValuesWrittenAndNotWritten("CustomMessage", writtenMessages, notWrittenMessages);
194+
}
195+
151196
private static IConfiguration TestConfiguration() =>
152197
new ConfigurationBuilder()
153198
.AddInMemoryCollection(new Dictionary<string, string>

test/Serilog.Sinks.MSSqlServer.Tests/TestUtils/DatabaseTestsBase.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ protected static void VerifyStringColumnWritten(string columnName, string expect
119119
}
120120
}
121121

122+
protected static void VerifyStringColumnMultipleValuesWrittenAndNotWritten(
123+
string columnName,
124+
List<string> valuesWritten,
125+
List<string> valuesNotWritten)
126+
{
127+
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
128+
{
129+
var logEvents = conn.Query<string>($"SELECT {columnName} FROM {DatabaseFixture.LogTableName}");
130+
131+
valuesWritten?.ForEach(v => logEvents.Should().Contain(v));
132+
valuesNotWritten?.ForEach(v => logEvents.Should().NotContain(v));
133+
}
134+
}
135+
122136
protected static void VerifyIntegerColumnWritten(string columnName, int expectedValue)
123137
{
124138
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))

0 commit comments

Comments
 (0)