From 3c07f0919ab2bcbd6481d3fc03e532b5496b033f Mon Sep 17 00:00:00 2001 From: sorenandersen Date: Tue, 7 Jun 2016 23:29:29 +0200 Subject: [PATCH] Added support for ExcludeAdditionalProperties for LogEvent column. (#27) Feature filterable log event properties via new option "ExcludeAdditionalProperties' located in the LogEvent column options. --- CHANGES.md | 3 +++ README.md | 2 ++ .../Sinks/MSSqlServer/ColumnOptions.cs | 18 ++++++++++++++++++ .../Sinks/MSSqlServer/MSSqlServerSink.cs | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 6e73fb73..2641d891 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +2.0.33 + * Option added to exclude redundant properties from serialized JSON in column LogEvent. (https://github.com/serilog/serilog-sinks-mssqlserver/pull/27) + 2.0.32 * Safe conversion of data types. Also included selflog for bulk operation errors. (https://github.com/serilog/serilog-sinks-mssqlserver/pull/4) diff --git a/README.md b/README.md index 2bb52082..af030a17 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ However, if the data is to stay in SQL Server, then the additional properties ma The log event JSON can be stored to the LogEvent column. This can be enabled with the `columnOptions.Store` parameter. +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. + ### Options for serialization of the Properties column The serialization of the properties column can be controlled by setting values in the in the `columnOptions.Properties` parameter. diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs index d4cda268..100fefa8 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs @@ -32,6 +32,8 @@ public ColumnOptions() }; TimeStamp = new TimeStampColumnOptions(); + + LogEvent = new LogEventColumnOptions(); } /// @@ -77,6 +79,11 @@ public ICollection Store /// public TimeStampColumnOptions TimeStamp { get; private set; } + /// + /// Options for the LogEvent column. + /// + public LogEventColumnOptions LogEvent { get; private set; } + /// /// Options for the Level column. /// @@ -178,5 +185,16 @@ public class TimeStampColumnOptions /// public bool ConvertToUtc { get; set; } } + + /// + /// Options for the LogEvent column. + /// + public class LogEventColumnOptions + { + /// + /// Exclude properties from the LogEvent column if they are being saved to additional columns. + /// + public bool ExcludeAdditionalProperties { get; set; } + } } } \ No newline at end of file diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs index 4065f667..0c759190 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs @@ -357,6 +357,12 @@ private string ConvertPropertiesToXmlStructure(IEnumerable !_additionalDataColumnNames.Contains(p.Key)); + logEvent = new LogEvent(logEvent.Timestamp, logEvent.Level, logEvent.Exception, logEvent.MessageTemplate, filteredProperties.Select(x => new LogEventProperty(x.Key, x.Value))); + } + var sb = new StringBuilder(); using (var writer = new System.IO.StringWriter(sb)) _jsonFormatter.Format(logEvent, writer);