Skip to content

Commit 303d826

Browse files
committed
Merged with master
2 parents d227857 + 3c07f09 commit 303d826

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Columns can be defined with the name and data type of the column in SQL Server.
148148

149149
#### JSON (LogEvent column)
150150

151-
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.
151+
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.
152152

153153
#### XML (Properties column)
154154

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)