Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertiesColumnWriter can now exclude properties #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,42 @@ public void NoProperties_ShouldReturnEmptyJsonObject()

Assert.Equal("{}", result);
}

[Fact]
public void OneProperty_ShouldReturnJsonWithOneProperty()
{
var writer = new PropertiesColumnWriter();

var testEvent = new LogEvent(DateTime.Now, LogEventLevel.Debug, null, new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), new[] { new LogEventProperty("Prop", new ScalarValue(1)) });

var result = writer.GetValue(testEvent);

Assert.Equal(@"{""Prop"":1}", result);
}

[Fact]
public void TwoProperties_ShouldReturnJsonWithTwoProperties()
{
var writer = new PropertiesColumnWriter();

var properties = new[] { new LogEventProperty("Prop1", new ScalarValue(1)), new LogEventProperty("Prop2", new ScalarValue(2)) };
var testEvent = new LogEvent(DateTime.Now, LogEventLevel.Debug, null, new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), properties);

var result = writer.GetValue(testEvent);

Assert.Equal(@"{""Prop1"":1, ""Prop2"":2}", result);
}

[Fact]
public void OnlyExcludedProperty_ShouldReturnEmptyJsonObject()
{
var writer = new PropertiesColumnWriter(exclude: new[] { "Prop" });

var testEvent = new LogEvent(DateTime.Now, LogEventLevel.Debug, null, new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), new[] { new LogEventProperty("Prop", new ScalarValue(1)) });

var result = writer.GetValue(testEvent);

Assert.Equal("{}", result);
}
}
}
26 changes: 22 additions & 4 deletions Serilog.Sinks.PostgreSQL/Sinks/PostgreSQL/ColumnWriters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NpgsqlTypes;
using Serilog.Events;
Expand Down Expand Up @@ -121,8 +123,11 @@ public override object GetValue(LogEvent logEvent, IFormatProvider formatProvide
/// </summary>
public class PropertiesColumnWriter : ColumnWriterBase
{
public PropertiesColumnWriter(NpgsqlDbType dbType = NpgsqlDbType.Jsonb) : base(dbType)
private readonly ICollection<string> _exclude;

public PropertiesColumnWriter(NpgsqlDbType dbType = NpgsqlDbType.Jsonb, ICollection<string> exclude = null) : base(dbType)
{
_exclude = exclude;
}

public override object GetValue(LogEvent logEvent, IFormatProvider formatProvider = null)
Expand All @@ -143,17 +148,30 @@ private object PropertiesToJson(LogEvent logEvent)

using (var writer = new System.IO.StringWriter(sb))
{
foreach (var logEventProperty in logEvent.Properties)
var propertiesToWrite = (IEnumerable<KeyValuePair<string, LogEventPropertyValue>>)logEvent.Properties;

if (_exclude != null)
{
propertiesToWrite = propertiesToWrite.Where(x => !_exclude.Contains(x.Key));
}

var isFirst = true;

foreach (var logEventProperty in propertiesToWrite)
{
if (!isFirst)
{
sb.Append(", ");
}

sb.Append($"\"{logEventProperty.Key}\":");

valuesFormatter.Format(logEventProperty.Value, writer);

sb.Append(", ");
isFirst = false;
}
}

sb.Remove(sb.Length - 2, 2);
sb.Append("}");

return sb.ToString();
Expand Down