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 6ac17815..9a8d6585 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ Columns can be defined with the name and data type of the column in SQL Server. #### JSON (LogEvent column) -The log event JSON can be stored to the LogEvent column. This can be enabled by adding the LogEvent column to the `columnOptions.Store` collection. +The log event JSON can be stored to the LogEvent column. This can be enabled by adding the LogEvent column to the `columnOptions.Store` collection. 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. #### XML (Properties column) 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);