|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Globalization;
|
4 | 4 | using Microsoft.Extensions.Configuration;
|
| 5 | +using Serilog.Core; |
5 | 6 | using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
|
6 | 7 | using Xunit;
|
7 | 8 | using Xunit.Abstractions;
|
@@ -148,6 +149,50 @@ public void SinkOptionsFromConfigSection()
|
148 | 149 | VerifyDatabaseColumnsWereCreated(standardNames);
|
149 | 150 | }
|
150 | 151 |
|
| 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 | + |
151 | 196 | private static IConfiguration TestConfiguration() =>
|
152 | 197 | new ConfigurationBuilder()
|
153 | 198 | .AddInMemoryCollection(new Dictionary<string, string>
|
|
0 commit comments