Skip to content

Commit

Permalink
Feature for Issue #82
Browse files Browse the repository at this point in the history
Bson sink supports excluding the "MessageTemplate" as it can be
redundant/unnecessary since the RenderedMessage exists.
  • Loading branch information
Jaben committed Feb 18, 2024
1 parent 19dd834 commit bb6e973
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
31 changes: 19 additions & 12 deletions src/Serilog.Sinks.MongoDB/Sinks/MongoDB/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class LogEntry

public DateTime UtcTimeStamp { get; set; }

[BsonIgnoreIfNull]
public MessageTemplate? MessageTemplate { get; set; }

public string? RenderedMessage { get; set; }
Expand All @@ -41,21 +42,27 @@ public class LogEntry

public BsonDocument? Exception { get; set; }

public static LogEntry MapFrom(LogEvent logEvent)
public static LogEntry MapFrom(LogEvent logEvent, bool includeMessageTemplate)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));

return new LogEntry
var logEntry = new LogEntry
{
RenderedMessage = logEvent.RenderMessage(),
Level = logEvent.Level,
UtcTimeStamp = logEvent.Timestamp.ToUniversalTime().UtcDateTime,
Exception = logEvent.Exception?.ToBsonDocument().SanitizeDocumentRecursive(),
Properties = BsonDocument.Create(
logEvent.Properties.ToDictionary(
s => s.Key.SanitizedElementName(),
s => s.Value.ToBsonValue()))
};

if (includeMessageTemplate)
{
MessageTemplate = logEvent.MessageTemplate,
RenderedMessage = logEvent.RenderMessage(),
Level = logEvent.Level,
UtcTimeStamp = logEvent.Timestamp.ToUniversalTime().UtcDateTime,
Exception = logEvent.Exception?.ToBsonDocument().SanitizeDocumentRecursive(),
Properties = BsonDocument.Create(
logEvent.Properties.ToDictionary(
s => s.Key.SanitizedElementName(),
s => s.Value.ToBsonValue()))
};
logEntry.MessageTemplate = logEvent.MessageTemplate;
}

return logEntry;
}
}
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public MongoDBSink(MongoDBSinkConfiguration configuration)

public override Task EmitBatchAsync(IEnumerable<LogEvent> events)
{
return this.InsertMany(events.Select(LogEntry.MapFrom));
return this.InsertMany(events.Select(@event => LogEntry.MapFrom(@event, this.IncludeMessageTemplate)));
}
}
4 changes: 3 additions & 1 deletion src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBSinkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class MongoDBSinkBase : IBatchedLogEventSink
/// </summary>
protected MongoDBSinkBase(MongoDBSinkConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (configuration! == null) throw new ArgumentNullException(nameof(configuration));

this._configuration = configuration;

Expand All @@ -51,6 +51,8 @@ protected MongoDBSinkBase(MongoDBSinkConfiguration configuration)
LazyThreadSafetyMode.ExecutionAndPublication);
}

protected bool IncludeMessageTemplate => !this._configuration.ExcludeMessageTemplate;

protected string CollectionName => this._configuration.CollectionName;

protected RollingInterval RollingInterval => this._configuration.RollingInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class MongoDBSinkConfiguration

public bool Legacy { get; internal set; }

public bool ExcludeMessageTemplate { get; internal set; }

public InsertManyOptions? InsertManyOptions { get; internal set; }

public RollingInterval RollingInterval { get; private set; } = RollingInterval.Infinite;
Expand All @@ -58,6 +60,11 @@ public void Validate()
throw new ArgumentNullException(
nameof(this.ExpireTTL),
"Expiration TTL is only supported on the MongoDBBson Sink");

if (this.ExcludeMessageTemplate && this.Legacy)
throw new ArgumentNullException(
nameof(this.ExcludeMessageTemplate),
"Exclude Message Template is only supported on the MongoDBBson Sink");
}

/// <summary>
Expand Down Expand Up @@ -88,6 +95,17 @@ public void SetExpireTTL(TimeSpan? timeToLive)
this.ExpireTTL = timeToLive;
}

/// <summary>
/// Sets if the log should include the "MessageTemplate" field,
/// as the RenderedMessage field is already included the "MessageTemplate"
/// may be unnecessary/redundant.
/// </summary>
/// <param name="excludeMessageTemplate"></param>
public void SetExcludeMessageTemplate(bool excludeMessageTemplate)
{
this.ExcludeMessageTemplate = excludeMessageTemplate;
}

/// <summary>
/// Allows configuring "InsertManyOptions" in MongoDb.
/// </summary>
Expand Down

0 comments on commit bb6e973

Please sign in to comment.