Skip to content

Commit d6c97c4

Browse files
committed
Merge branch 'release/v6.0.0'
2 parents 7f07a43 + 5f24d1a commit d6c97c4

18 files changed

+222
-40
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# ![mongo icon](https://raw.githubusercontent.com/ChangemakerStudios/serilog-sinks-mongodb/dev/assets/mongo-icon.png) Serilog.Sinks.MongoDB
22

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

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

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

12+
### New in v6.x
13+
* Upgraded to MongoDb v2.28 -- fixing breaking changes.
14+
1015
### New in v5.x
1116
* 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.
1217
* Rolling Log Collection Naming (Thanks to [Revazashvili](https://github.com/Revazashvili) for the PR!). MongoDBBson sink only.

src/Serilog.Sinks.MongoDB/Helpers/MongoDbDocumentHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.MongoDB/Helpers/MongoDbHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.MongoDB/Helpers/RollingIntervalHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.MongoDB/Helpers/StringHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.MongoDB/LoggerConfigurationMongoDBExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.MongoDB/Serilog.Sinks.MongoDB.csproj

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,38 @@
99
</PropertyGroup>
1010

1111
<PropertyGroup>
12-
<PackageVersion>5.4.0</PackageVersion>
12+
<PackageVersion>6.0.0</PackageVersion>
1313
<Authors>Kiran Makkapati, Jaben Cargman, Serilog Contributors</Authors>
1414
<Copyright>Copyright © Serilog Contributors 2014-2022</Copyright>
1515
<Description>The MongoDB sink for Serilog</Description>
16-
<PackageIconUrl>http://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
16+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1717
<PackageId>Serilog.Sinks.MongoDB</PackageId>
1818
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
19-
<RepositoryUrl>https://github.com/ChangemakerStudios/serilog-sinks-mongodb</RepositoryUrl>
20-
<RepositoryType>git</RepositoryType>
19+
<PackageIcon>serilog-sink-nuget.png</PackageIcon>
2120
<PackageTags>serilog, mongodb</PackageTags>
2221
<PackageReadmeFile>README.md</PackageReadmeFile>
2322
<PackageReleaseNotes>
24-
v5.4 - Upgraded to MongoDB.Driver to version 2.19 due to vulnerabilities.
23+
v6.0 - Upgraded to MongoDB.Driver to version 2.28 due to incompatibilities.
2524
</PackageReleaseNotes>
2625
</PropertyGroup>
2726

2827
<PropertyGroup>
28+
<RepositoryUrl>https://github.com/ChangemakerStudios/serilog-sinks-mongodb</RepositoryUrl>
29+
<RepositoryType>git</RepositoryType>
2930
<PublishRepositoryUrl>true</PublishRepositoryUrl>
3031
<EmbedUntrackedSources>true</EmbedUntrackedSources>
3132
<IncludeSymbols>true</IncludeSymbols>
3233
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3334
</PropertyGroup>
3435

36+
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
37+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
38+
</PropertyGroup>
39+
40+
<ItemGroup>
41+
<None Include="serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="" />
42+
</ItemGroup>
43+
3544
<ItemGroup>
3645
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
3746
<PackageReference Include="PolySharp" Version="1.14.1">
@@ -41,10 +50,10 @@
4150
</ItemGroup>
4251

4352
<ItemGroup>
44-
<PackageReference Include="Serilog" Version="2.12.0" />
53+
<PackageReference Include="Serilog" Version="3.1.1" />
4554
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
4655
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="3.1.0" />
47-
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
56+
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
4857
</ItemGroup>
4958

5059
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">

src/Serilog.Sinks.MongoDB/Sinks/MongoDB/LogEntry.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Diagnostics;
1617
using System.Linq;
1718

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

3435
public DateTime UtcTimeStamp { get; set; }
3536

37+
[BsonIgnoreIfNull]
3638
public MessageTemplate? MessageTemplate { get; set; }
3739

3840
public string? RenderedMessage { get; set; }
3941

4042
public BsonDocument? Properties { get; set; }
4143

4244
public BsonDocument? Exception { get; set; }
45+
[BsonIgnoreIfNull]
46+
public string? TraceId { get; set; }
47+
[BsonIgnoreIfNull]
48+
public string? SpanId { get; set; }
4349

44-
public static LogEntry MapFrom(LogEvent logEvent)
50+
public static LogEntry MapFrom(LogEvent logEvent, bool includeMessageTemplate)
4551
{
4652
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
4753

48-
return new LogEntry
54+
var logEntry = new LogEntry
55+
{
56+
RenderedMessage = logEvent.RenderMessage(),
57+
Level = logEvent.Level,
58+
UtcTimeStamp = logEvent.Timestamp.ToUniversalTime().UtcDateTime,
59+
TraceId = logEvent.TraceId?.ToString(),
60+
SpanId = logEvent.SpanId?.ToString(),
61+
Exception = logEvent.Exception?.ToBsonDocument().SanitizeDocumentRecursive(),
62+
Properties = BsonDocument.Create(
63+
logEvent.Properties.ToDictionary(
64+
s => s.Key.SanitizedElementName(),
65+
s => s.Value.ToBsonValue()))
66+
};
67+
68+
if (includeMessageTemplate)
4969
{
50-
MessageTemplate = logEvent.MessageTemplate,
51-
RenderedMessage = logEvent.RenderMessage(),
52-
Level = logEvent.Level,
53-
UtcTimeStamp = logEvent.Timestamp.ToUniversalTime().UtcDateTime,
54-
Exception = logEvent.Exception?.ToBsonDocument().SanitizeDocumentRecursive(),
55-
Properties = BsonDocument.Create(
56-
logEvent.Properties.ToDictionary(
57-
s => s.Key.SanitizedElementName(),
58-
s => s.Value.ToBsonValue()))
59-
};
70+
logEntry.MessageTemplate = logEvent.MessageTemplate;
71+
}
72+
73+
return logEntry;
6074
}
6175
}

src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBJsonFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -84,7 +84,7 @@ protected override void WriteJsonProperty(
8484
}
8585
else
8686
{
87-
base.WriteJsonProperty(name, value, ref precedingDelimiter, output);
87+
base.WriteJsonProperty(name, value!, ref precedingDelimiter, output);
8888
}
8989
}
9090

src/Serilog.Sinks.MongoDB/Sinks/MongoDB/MongoDBSink.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2022 Serilog Contributors
1+
// Copyright 2014-2024 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,9 +14,11 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Diagnostics.CodeAnalysis;
1718
using System.Linq;
1819
using System.Threading.Tasks;
1920

21+
using MongoDB.Bson;
2022
using MongoDB.Bson.Serialization;
2123

2224
using Serilog.Events;
@@ -37,6 +39,10 @@ static MongoDBSink()
3739
cm.MapProperty(s => s.StackTrace);
3840
cm.MapProperty(s => s.Data);
3941
});
42+
43+
// fixes https://github.com/serilog/serilog/issues/2101
44+
BsonTypeMapper.RegisterCustomTypeMapper(typeof(IntPtr), new CustomIntPtrMapper());
45+
BsonTypeMapper.RegisterCustomTypeMapper(typeof(UIntPtr), new CustomIntPtrMapper());
4046
}
4147

4248
public MongoDBSink(MongoDBSinkConfiguration configuration)
@@ -46,6 +52,26 @@ public MongoDBSink(MongoDBSinkConfiguration configuration)
4652

4753
public override Task EmitBatchAsync(IEnumerable<LogEvent> events)
4854
{
49-
return this.InsertMany(events.Select(LogEntry.MapFrom));
55+
return this.InsertMany(
56+
events.Select(@event => LogEntry.MapFrom(@event, this.IncludeMessageTemplate)));
57+
}
58+
59+
private class CustomIntPtrMapper : ICustomBsonTypeMapper
60+
{
61+
public bool TryMapToBsonValue(object value, [UnscopedRef] out BsonValue? bsonValue)
62+
{
63+
switch (value)
64+
{
65+
case IntPtr intPtr:
66+
bsonValue = intPtr.ToInt32();
67+
return true;
68+
case UIntPtr uIntPtr:
69+
bsonValue = uIntPtr.ToUInt32();
70+
return true;
71+
default:
72+
bsonValue = null;
73+
return false;
74+
}
75+
}
5076
}
5177
}

0 commit comments

Comments
 (0)