Skip to content

Commit 3c07f09

Browse files
sorenandersenmivano
authored andcommitted
Added support for ExcludeAdditionalProperties for LogEvent column. (#27)
Feature filterable log event properties via new option "ExcludeAdditionalProperties' located in the LogEvent column options.
1 parent 992da0d commit 3c07f09

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2.0.33
2+
* Option added to exclude redundant properties from serialized JSON in column LogEvent. (https://github.com/serilog/serilog-sinks-mssqlserver/pull/27)
3+
14
2.0.32
25
* Safe conversion of data types. Also included selflog for bulk operation errors. (https://github.com/serilog/serilog-sinks-mssqlserver/pull/4)
36

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ However, if the data is to stay in SQL Server, then the additional properties ma
103103

104104
The log event JSON can be stored to the LogEvent column. This can be enabled with the `columnOptions.Store` parameter.
105105

106+
Use the `columnOptions.LogEvent.ExcludeAdditionalProperties` parameter to exclude redundant properties from the JSON. This is analogue to excluding redundant items from XML in the Properties column.
107+
106108
### Options for serialization of the Properties column
107109

108110
The serialization of the properties column can be controlled by setting values in the in the `columnOptions.Properties` parameter.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public ColumnOptions()
3232
};
3333

3434
TimeStamp = new TimeStampColumnOptions();
35+
36+
LogEvent = new LogEventColumnOptions();
3537
}
3638

3739
/// <summary>
@@ -77,6 +79,11 @@ public ICollection<StandardColumn> Store
7779
/// </summary>
7880
public TimeStampColumnOptions TimeStamp { get; private set; }
7981

82+
/// <summary>
83+
/// Options for the LogEvent column.
84+
/// </summary>
85+
public LogEventColumnOptions LogEvent { get; private set; }
86+
8087
/// <summary>
8188
/// Options for the Level column.
8289
/// </summary>
@@ -178,5 +185,16 @@ public class TimeStampColumnOptions
178185
/// </summary>
179186
public bool ConvertToUtc { get; set; }
180187
}
188+
189+
/// <summary>
190+
/// Options for the LogEvent column.
191+
/// </summary>
192+
public class LogEventColumnOptions
193+
{
194+
/// <summary>
195+
/// Exclude properties from the LogEvent column if they are being saved to additional columns.
196+
/// </summary>
197+
public bool ExcludeAdditionalProperties { get; set; }
198+
}
181199
}
182200
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,
357357

358358
private string LogEventToJson(LogEvent logEvent)
359359
{
360+
if (_columnOptions.LogEvent.ExcludeAdditionalProperties)
361+
{
362+
var filteredProperties = logEvent.Properties.Where(p => !_additionalDataColumnNames.Contains(p.Key));
363+
logEvent = new LogEvent(logEvent.Timestamp, logEvent.Level, logEvent.Exception, logEvent.MessageTemplate, filteredProperties.Select(x => new LogEventProperty(x.Key, x.Value)));
364+
}
365+
360366
var sb = new StringBuilder();
361367
using (var writer = new System.IO.StringWriter(sb))
362368
_jsonFormatter.Format(logEvent, writer);

0 commit comments

Comments
 (0)