Skip to content

Commit

Permalink
Added support for ExcludeAdditionalProperties for LogEvent column. (#27)
Browse files Browse the repository at this point in the history
Feature filterable log event properties via new option "ExcludeAdditionalProperties' located in the LogEvent column options.
  • Loading branch information
sorenandersen authored and mivano committed Jun 7, 2016
1 parent 992da0d commit 3c07f09
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public ColumnOptions()
};

TimeStamp = new TimeStampColumnOptions();

LogEvent = new LogEventColumnOptions();
}

/// <summary>
Expand Down Expand Up @@ -77,6 +79,11 @@ public ICollection<StandardColumn> Store
/// </summary>
public TimeStampColumnOptions TimeStamp { get; private set; }

/// <summary>
/// Options for the LogEvent column.
/// </summary>
public LogEventColumnOptions LogEvent { get; private set; }

/// <summary>
/// Options for the Level column.
/// </summary>
Expand Down Expand Up @@ -178,5 +185,16 @@ public class TimeStampColumnOptions
/// </summary>
public bool ConvertToUtc { get; set; }
}

/// <summary>
/// Options for the LogEvent column.
/// </summary>
public class LogEventColumnOptions
{
/// <summary>
/// Exclude properties from the LogEvent column if they are being saved to additional columns.
/// </summary>
public bool ExcludeAdditionalProperties { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,

private string LogEventToJson(LogEvent logEvent)
{
if (_columnOptions.LogEvent.ExcludeAdditionalProperties)
{
var filteredProperties = logEvent.Properties.Where(p => !_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);
Expand Down

0 comments on commit 3c07f09

Please sign in to comment.