Skip to content

Commit a453a8b

Browse files
authored
Merge pull request #100 from nascimentod/feature/properties_column_predicate
Properties column predicate
2 parents 930e900 + d63b1aa commit a453a8b

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ public PropertiesColumnOptions()
207207
/// If true, will use the property key as the element name.
208208
/// </summary>
209209
public bool UsePropertyKeyAsElementName { get; set; }
210+
211+
/// <summary>
212+
/// If set, will only store properties allowed by the filter.
213+
/// </summary>
214+
public Predicate<string> PropertiesFilter { get; set; }
210215
}
211216

212217
/// <summary>

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,
303303
if (options.ExcludeAdditionalProperties)
304304
properties = properties.Where(p => !_additionalDataColumnNames.Contains(p.Key));
305305

306+
if (options.PropertiesFilter != null)
307+
{
308+
try
309+
{
310+
properties = properties.Where(p => options.PropertiesFilter(p.Key));
311+
}
312+
catch (Exception ex)
313+
{
314+
SelfLog.WriteLine("Unable to filter properties to store in {0} due to following error: {1}", this, ex);
315+
}
316+
}
317+
306318
var sb = new StringBuilder();
307319

308320
sb.AppendFormat("<{0}>", options.RootElementName);
@@ -414,7 +426,7 @@ protected override void Dispose(bool disposing)
414426
base.Dispose(disposing);
415427

416428
if (_eventsTable != null)
417-
{
429+
{
418430
_eventsTable.Dispose();
419431
_eventsTable = null;
420432
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Dapper;
2+
using FluentAssertions;
3+
using System.Data.SqlClient;
4+
using Xunit;
5+
6+
namespace Serilog.Sinks.MSSqlServer.Tests
7+
{
8+
[Collection("LogTest")]
9+
public class TestPropertiesColumnFiltering
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+
}
50+
}
51+
}

0 commit comments

Comments
 (0)