Skip to content

Commit

Permalink
Merge branch 'release/v6.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaben committed Sep 20, 2024
2 parents 7f07a43 + 5f24d1a commit d6c97c4
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 40 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# ![mongo icon](https://raw.githubusercontent.com/ChangemakerStudios/serilog-sinks-mongodb/dev/assets/mongo-icon.png) Serilog.Sinks.MongoDB

![Build status](https://github.com/ChangemakerStudios/serilog-sinks-mongodb/actions/workflows/deploy.yml/badge.svg)
[![NuGet version](https://badge.fury.io/nu/Serilog.Sinks.MongoDB.svg)](https://badge.fury.io/nu/Serilog.Sinks.MongoDB)
[![Downloads](https://img.shields.io/nuget/dt/Serilog.Sinks.MongoDB.svg?logo=nuget&color=purple)](https://www.nuget.org/packages/Serilog.Sinks.MongoDB)
[![Build status](https://github.com/ChangemakerStudios/serilog-sinks-mongodb/actions/workflows/deploy.yml/badge.svg)](https://github.com/ChangemakerStudios/serilog-sinks-mongodb/actions)

A Serilog sink that writes events as documents to [MongoDB](http://mongodb.org).

**Package** - [Serilog.Sinks.MongoDB](http://nuget.org/packages/serilog.sinks.mongodb)
| **Platforms** - .NET 4.7.2, .NET Standard 2.0,, .NET Standard 2.1

### New in v6.x
* Upgraded to MongoDb v2.28 -- fixing breaking changes.

### New in v5.x
* Output structured MongoDB Bson logs by switching to the .MongoDBBson() extensions. Existing the .MongoDB() extensions will continue to work converting logs to Json and then to Bson.
* Rolling Log Collection Naming (Thanks to [Revazashvili](https://github.com/Revazashvili) for the PR!). MongoDBBson sink only.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.MongoDB/Helpers/MongoDbHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.MongoDB/Helpers/RollingIntervalHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.MongoDB/Helpers/StringHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
23 changes: 16 additions & 7 deletions src/Serilog.Sinks.MongoDB/Serilog.Sinks.MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,38 @@
</PropertyGroup>

<PropertyGroup>
<PackageVersion>5.4.0</PackageVersion>
<PackageVersion>6.0.0</PackageVersion>
<Authors>Kiran Makkapati, Jaben Cargman, Serilog Contributors</Authors>
<Copyright>Copyright © Serilog Contributors 2014-2022</Copyright>
<Description>The MongoDB sink for Serilog</Description>
<PackageIconUrl>http://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageId>Serilog.Sinks.MongoDB</PackageId>
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
<RepositoryUrl>https://github.com/ChangemakerStudios/serilog-sinks-mongodb</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIcon>serilog-sink-nuget.png</PackageIcon>
<PackageTags>serilog, mongodb</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
v5.4 - Upgraded to MongoDB.Driver to version 2.19 due to vulnerabilities.
v6.0 - Upgraded to MongoDB.Driver to version 2.28 due to incompatibilities.
</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup>
<RepositoryUrl>https://github.com/ChangemakerStudios/serilog-sinks-mongodb</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="PolySharp" Version="1.14.1">
Expand All @@ -41,10 +50,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="3.1.0" />
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
Expand Down
40 changes: 27 additions & 13 deletions src/Serilog.Sinks.MongoDB/Sinks/MongoDB/LogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Diagnostics;
using System.Linq;

using MongoDB.Bson;
Expand All @@ -33,29 +34,42 @@ public class LogEntry

public DateTime UtcTimeStamp { get; set; }

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

public string? RenderedMessage { get; set; }

public BsonDocument? Properties { get; set; }

public BsonDocument? Exception { get; set; }
[BsonIgnoreIfNull]
public string? TraceId { get; set; }
[BsonIgnoreIfNull]
public string? SpanId { 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,
TraceId = logEvent.TraceId?.ToString(),
SpanId = logEvent.SpanId?.ToString(),
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -84,7 +84,7 @@ protected override void WriteJsonProperty(
}
else
{
base.WriteJsonProperty(name, value, ref precedingDelimiter, output);
base.WriteJsonProperty(name, value!, ref precedingDelimiter, output);
}
}

Expand Down
30 changes: 28 additions & 2 deletions src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBSink.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

using MongoDB.Bson;
using MongoDB.Bson.Serialization;

using Serilog.Events;
Expand All @@ -37,6 +39,10 @@ static MongoDBSink()
cm.MapProperty(s => s.StackTrace);
cm.MapProperty(s => s.Data);
});

// fixes https://github.com/serilog/serilog/issues/2101
BsonTypeMapper.RegisterCustomTypeMapper(typeof(IntPtr), new CustomIntPtrMapper());
BsonTypeMapper.RegisterCustomTypeMapper(typeof(UIntPtr), new CustomIntPtrMapper());
}

public MongoDBSink(MongoDBSinkConfiguration configuration)
Expand All @@ -46,6 +52,26 @@ 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)));
}

private class CustomIntPtrMapper : ICustomBsonTypeMapper
{
public bool TryMapToBsonValue(object value, [UnscopedRef] out BsonValue? bsonValue)
{
switch (value)
{
case IntPtr intPtr:
bsonValue = intPtr.ToInt32();
return true;
case UIntPtr uIntPtr:
bsonValue = uIntPtr.ToUInt32();
return true;
default:
bsonValue = null;
return false;
}
}
}
}
6 changes: 4 additions & 2 deletions src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBSinkBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.MongoDB/Sinks/MongoDB/RollingInterval.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2022 Serilog Contributors
// Copyright 2014-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Binary file added src/Serilog.Sinks.MongoDB/serilog-sink-nuget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d6c97c4

Please sign in to comment.