|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Text; |
| 1 | +using Dapper; |
| 2 | +using FluentAssertions; |
| 3 | +using System.Data.SqlClient; |
| 4 | +using Xunit; |
4 | 5 |
|
5 | 6 | namespace Serilog.Sinks.MSSqlServer.Tests
|
6 | 7 | {
|
7 |
| - class TestPropertiesColumnFiltering |
| 8 | + [Collection("LogTest")] |
| 9 | + public class TestPropertiesColumnFiltering |
8 | 10 | {
|
| 11 | + internal class PropertiesColumns |
| 12 | + { |
| 13 | + public string Properties { get; set; } |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void FilteredProperties() |
| 18 | + { |
| 19 | + // arrange |
| 20 | + var columnOptions = new ColumnOptions(); |
| 21 | + columnOptions.Properties.PropertiesFilter = (propName) => propName == "A"; |
| 22 | + |
| 23 | + Log.Logger = new LoggerConfiguration() |
| 24 | + .WriteTo.MSSqlServer |
| 25 | + ( |
| 26 | + connectionString: DatabaseFixture.LogEventsConnectionString, |
| 27 | + tableName: DatabaseFixture.LogTableName, |
| 28 | + columnOptions: columnOptions, |
| 29 | + autoCreateSqlTable: true |
| 30 | + ) |
| 31 | + .CreateLogger(); |
| 32 | + |
| 33 | + // act |
| 34 | + Log.Logger |
| 35 | + .ForContext("A", "AValue") |
| 36 | + .ForContext("B", "BValue") |
| 37 | + .Information("Logging message"); |
| 38 | + |
| 39 | + Log.CloseAndFlush(); |
| 40 | + |
| 41 | + // assert |
| 42 | + using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString)) |
| 43 | + { |
| 44 | + var logEvents = conn.Query<PropertiesColumns>($"SELECT Properties from {DatabaseFixture.LogTableName}"); |
| 45 | + |
| 46 | + logEvents.Should().Contain(e => e.Properties.Contains("AValue")); |
| 47 | + logEvents.Should().NotContain(e => e.Properties.Contains("BValue")); |
| 48 | + } |
| 49 | + } |
9 | 50 | }
|
10 | 51 | }
|
0 commit comments