Serilog.Sinks.MongoDB
7.2.0
dotnet add package Serilog.Sinks.MongoDB --version 7.2.0
NuGet\Install-Package Serilog.Sinks.MongoDB -Version 7.2.0
<PackageReference Include="Serilog.Sinks.MongoDB" Version="7.2.0" />
<PackageVersion Include="Serilog.Sinks.MongoDB" Version="7.2.0" />
<PackageReference Include="Serilog.Sinks.MongoDB" />
paket add Serilog.Sinks.MongoDB --version 7.2.0
#r "nuget: Serilog.Sinks.MongoDB, 7.2.0"
#:package Serilog.Sinks.MongoDB@7.2.0
#addin nuget:?package=Serilog.Sinks.MongoDB&version=7.2.0
#tool nuget:?package=Serilog.Sinks.MongoDB&version=7.2.0
Serilog.Sinks.MongoDB
A Serilog sink that writes events as documents to MongoDB.
Package - Serilog.Sinks.MongoDB Platforms - .NET 4.7.2, .NET 6.0+, .NET Standard 2.1
Whats New
See CHANGES.md for complete version history.
New in v7.2
- Fixed MongoDB v7.x compatibility - Error handling now uses error codes instead of string matching (#98, #99)
- Added comprehensive unit tests for MongoDB error handling
- Improved CI performance
Installation
Install the sink via NuGet Package Manager Console:
Install-Package Serilog.Sinks.MongoDB
or via the .NET CLI:
dotnet add package Serilog.Sinks.MongoDB
Usage Examples
In the examples below, the sink is writing to the database logs with structured Bson. The default collection name is log, but a custom collection can be supplied with the optional CollectionName parameter. The database and collection will be created if they do not exist.
Basic:
using Serilog;
// use BSON structured logs
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson("mongodb://mymongodb/logs")
.CreateLogger();
log.Information("This is a test log message");
Capped Collection:
// capped collection using BSON structured logs
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson("mongodb://mymongodb/logs", cfg =>
{
// optional configuration options:
cfg.SetCollectionName("log");
cfg.SetBatchPeriod(TimeSpan.FromSeconds(1));
// create capped collection that is max 100mb
cfg.SetCreateCappedCollection(100);
})
.CreateLogger();
Custom Mongodb Settings:
// create sink instance with custom mongodb settings.
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(cfg =>
{
// custom MongoDb configuration
var mongoDbSettings = new MongoClientSettings
{
UseTls = true,
AllowInsecureTls = true,
Credential = MongoCredential.CreateCredential("databaseName", "username", "password"),
Server = new MongoServerAddress("127.0.0.1")
};
var mongoDbInstance = new MongoClient(mongoDbSettings).GetDatabase("serilog");
// sink will use the IMongoDatabase instance provided
cfg.SetMongoDatabase(mongoDbInstance);
cfg.SetRollingInterval(RollingInterval.Month);
})
.CreateLogger();
JSON (Microsoft.Extensions.Configuration)
Keys and values are not case-sensitive. This is an example of configuring the MongoDB sink arguments from Appsettings.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Error",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "MongoDBBson",
"Args": {
"databaseUrl": "mongodb://username:password@ip:port/dbName?authSource=admin",
"collectionName": "logs",
"cappedMaxSizeMb": "1024",
"cappedMaxDocuments": "50000",
"rollingInterval": "Month"
}
}
]
}
}
Advanced Configuration
Authentication & Secure Connections
For password-protected MongoDB instances, Azure Cosmos DB, or SSL/TLS connections:
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(cfg =>
{
var mongoDbSettings = new MongoClientSettings
{
UseTls = true,
AllowInsecureTls = false, // set true only for dev/testing
Credential = MongoCredential.CreateCredential("databaseName", "username", "password"),
Server = new MongoServerAddress("your-server.com", 27017)
};
var mongoDbInstance = new MongoClient(mongoDbSettings).GetDatabase("logs");
cfg.SetMongoDatabase(mongoDbInstance);
})
.CreateLogger();
Azure Cosmos DB (MongoDB API):
var connectionString = "mongodb://cosmosdb-account:key@cosmosdb-account.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false";
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(connectionString)
.CreateLogger();
TTL / Auto-Expiration
Automatically delete old logs using MongoDB's TTL feature:
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(cfg =>
{
cfg.SetMongoUrl("mongodb://localhost/logs");
cfg.SetExpireTTL(TimeSpan.FromDays(30)); // logs expire after 30 days
})
.CreateLogger();
Exclude Redundant Fields
Reduce storage costs by excluding the MessageTemplate field (the rendered message is still stored):
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(cfg =>
{
cfg.SetMongoUrl("mongodb://localhost/logs");
cfg.SetExcludeMessageTemplate(true); // saves storage space
})
.CreateLogger();
Rolling Collections
Create time-based collections (e.g., one per day/month):
var log = new LoggerConfiguration()
.WriteTo.MongoDBBson(cfg =>
{
cfg.SetMongoUrl("mongodb://localhost/logs");
cfg.SetCollectionName("log");
cfg.SetRollingInterval(RollingInterval.Day); // creates: log-20251004, log-20251005, etc.
})
.CreateLogger();
Collection naming patterns:
RollingInterval.Day→log-yyyyMMdd(e.g.,log-20251004)RollingInterval.Month→log-yyyyMM(e.g.,log-202510)RollingInterval.Year→log-yyyy(e.g.,log-2025)
Querying rolling collections:
// Query specific date range - you need to target the correct collection(s)
var collectionName = $"log-{DateTime.UtcNow:yyyyMMdd}";
var collection = database.GetCollection<BsonDocument>(collectionName);
var todayLogs = collection.Find(Builders<BsonDocument>.Filter.Empty).ToList();
Migration Guide
From Legacy .MongoDB() to .MongoDBBson()
The legacy .MongoDB() sink converts logs to JSON then to BSON. The newer .MongoDBBson() writes structured BSON directly for better performance and features.
Legacy (still supported):
.WriteTo.MongoDB("mongodb://localhost/logs") // converts to JSON first
New Bson sink (recommended):
.WriteTo.MongoDBBson("mongodb://localhost/logs") // native BSON
MongoDBBson exclusive features:
- TTL/Expiration (
SetExpireTTL) - Exclude message template (
SetExcludeMessageTemplate) - Rolling collections (
SetRollingInterval) - Better type mapping and performance
Troubleshooting
MongoDB Connection Issues
Problem: Application hangs or fails when MongoDB is unavailable Solution: Ensure your MongoDB connection string includes appropriate timeouts:
"mongodb://localhost/logs?connectTimeoutMS=3000&serverSelectionTimeoutMS=3000"
Type Mapping Errors
Problem: System.Guid cannot be mapped to BsonValue
Solution: Ensure you're using the latest version (v7.1+) which includes Guid mapping fixes.
Capped Collections Not Created
Problem: Capped collection configuration not working Solution: Ensure the collection doesn't already exist. Drop existing collection first:
database.DropCollection("logs");
// Then configure with capped settings
Rolling Collections Not Named Correctly
Problem: Collection created without time format suffix
Solution: Ensure you're using MongoDBBson sink (not legacy MongoDB) and v7.1+.
Icon
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| .NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- MongoDB.Driver (>= 3.0.0)
- Serilog (>= 3.1.1)
- Serilog.Formatting.Compact (>= 1.1.0)
- Serilog.Sinks.PeriodicBatching (>= 3.1.0)
-
.NETStandard 2.1
- MongoDB.Driver (>= 3.0.0)
- Serilog (>= 3.1.1)
- Serilog.Formatting.Compact (>= 1.1.0)
- Serilog.Sinks.PeriodicBatching (>= 3.1.0)
-
net6.0
- MongoDB.Driver (>= 3.0.0)
- Serilog (>= 3.1.1)
- Serilog.Formatting.Compact (>= 1.1.0)
- Serilog.Sinks.PeriodicBatching (>= 3.1.0)
NuGet packages (54)
Showing the top 5 NuGet packages that depend on Serilog.Sinks.MongoDB:
| Package | Downloads |
|---|---|
|
RG3.PF.Web.StartUsed
1、【核心业务使用包RG3.PF.Web.StartUsed】(可配置化,标准化,灵活配置,集成百度、腾讯、阿里、短信通等巨头接口,可脱离开发环境,集成nodejs中间件、统一认证中心) 2、以Rg3.开头且类继承 IService 或 IRepository的在Starup里面不用注入,案例如下 3、public class ListRepository : IRepository 4、public class ListService : IService 5、使用请到github获取 RG3.PF.WebApp.Host 6、Quartz集成redis订阅、Quartz 7、修复多引用CLDC 8、接入微信公众号wxconfig 9、Prometheus+Grafana https://doc.rg1008.com/docs/rg_pass_log/rg_pass_log-1dpcuns9s6r8c 10、Headers添加VerifyApiValue 用于验证 11、添加时间戳 Convert.ToInt64(DateTimePFUtil.ToTimestampSecond(this.Expires)); 12、版本记录:https://doc.rg1008.com/docs/rg_pass_log/rg_pass_log-1dpubsabl25v4 13、 20240815 升级到.netcore sdk 8.0 |
|
|
TianCheng.Model
实体对象基类,及其常用操作。 常用操作包括:对象转换、序列化、日志、常用异常处理、依赖注入。 |
|
|
Basic.Logging.Serilog
Package Description |
|
|
uBeac.Logging.MongoDB
Easily implement logging with MongoDB in your .NET projects! |
|
|
PegasusLoggingService
Package Description |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Serilog.Sinks.MongoDB:
| Repository | Stars |
|---|---|
|
serilog-contrib/serilog-ui
Simple Serilog log viewer UI for several sinks.
|
|
|
keremvaris/Sennedjem
Sennedjem CQRS (Command Query Responsibility Segregation) yaklaşımını benimseyen ve SOLID prensiplerini ve Clean Architecture yöntemlerini odaklayan bir yazılım geliştirme alt yapısıdır. RabbitMq, ElasticSearch vb araçlara entegre olmak konusunda çok yeteneklidir.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 7.2.0 | 34 | 10/26/2025 |
| 7.1.0 | 5,509 | 10/5/2025 |
| 7.0.0 | 318,645 | 11/5/2024 |
| 6.0.0 | 113,458 | 9/20/2024 |
| 5.4.1 | 377,698 | 2/18/2024 |
| 5.3.1 | 1,122,969 | 9/28/2022 |
| 5.2.2 | 8,135 | 9/26/2022 |
| 5.2.1 | 33,175 | 9/3/2022 |
| 5.2.1-tags-v5-2-0-0000 | 406 | 9/3/2022 |
| 5.2.0 | 1,097 | 9/3/2022 |
| 5.1.5 | 307,203 | 4/24/2022 |
| 5.1.2 | 180,172 | 1/19/2022 |
| 5.1.2-dev-00124 | 458 | 3/12/2022 |
| 5.1.2-dev-00123 | 669 | 1/19/2022 |
| 5.1.2-dev-00121 | 644 | 1/19/2022 |
| 5.1.1 | 259,509 | 9/25/2021 |
| 5.1.1-dev-00120 | 706 | 1/19/2022 |
| 5.1.1-dev-00119 | 637 | 1/19/2022 |
| 5.1.1-dev-00118 | 675 | 1/19/2022 |
| 5.1.1-dev-00116 | 515 | 9/25/2021 |
| 5.1.0-dev-00114 | 538 | 9/25/2021 |
| 5.0.0 | 81,933 | 9/2/2021 |
| 5.0.0-dev-00112 | 575 | 9/24/2021 |
| 5.0.0-dev-00111 | 622 | 9/12/2021 |
| 5.0.0-dev-00110 | 580 | 9/12/2021 |
| 5.0.0-dev-00109 | 510 | 9/2/2021 |
| 5.0.0-dev-00107 | 522 | 9/2/2021 |
| 5.0.0-dev-00105 | 812 | 8/1/2021 |
| 5.0.0-dev-00104 | 2,044 | 5/8/2021 |
| 5.0.0-dev-00103 | 597 | 5/7/2021 |
| 5.0.0-dev-00100 | 24,524 | 5/6/2021 |
| 5.0.0-dev-00099 | 584 | 5/6/2021 |
| 5.0.0-dev-00098 | 571 | 5/6/2021 |
| 5.0.0-dev-00097 | 938 | 5/6/2021 |
| 4.1.0 | 446,092 | 5/6/2021 |
| 4.1.0-dev-00096 | 569 | 5/6/2021 |
| 4.1.0-dev-00094 | 549 | 5/6/2021 |
| 4.1.0-dev-00084 | 10,726 | 10/21/2020 |
| 4.1.0-dev-00069 | 20,702 | 1/29/2019 |
| 4.0.0 | 1,689,541 | 11/9/2018 |
| 4.0.0-dev-00068 | 683 | 1/29/2019 |
| 4.0.0-dev-00064 | 800 | 11/9/2018 |
| 4.0.0-dev-00062 | 7,640 | 2/15/2018 |
| 4.0.0-dev-00060 | 1,330 | 11/30/2017 |
| 3.1.1-dev-00059 | 1,114 | 11/30/2017 |
| 3.1.0 | 514,856 | 11/1/2016 |
| 3.1.0-dev-00049 | 1,283 | 10/30/2016 |
| 3.1.0-dev-00046 | 1,750 | 10/7/2016 |
| 3.1.0-dev-00044 | 1,161 | 10/7/2016 |
| 3.1.0-dev-00042 | 1,144 | 10/5/2016 |
| 3.1.0-dev-00037 | 1,231 | 10/5/2016 |
| 3.1.0-dev-00035 | 1,200 | 9/9/2016 |
| 3.1.0-dev-00033 | 1,157 | 9/8/2016 |
| 3.0.0 | 3,775 | 8/9/2016 |
| 3.0.0-dev-00030 | 1,180 | 8/9/2016 |
| 3.0.0-dev-00029 | 1,148 | 8/9/2016 |
| 3.0.0-beta-24 | 1,587 | 4/26/2016 |
| 2.0.19 | 13,278 | 2/16/2016 |
| 2.0.18 | 1,670 | 2/16/2016 |
| 2.0.14 | 23,174 | 1/26/2016 |
| 2.0.13 | 4,695 | 7/26/2015 |
| 2.0.11 | 2,602 | 4/18/2015 |
| 2.0.8-pre | 1,298 | 4/2/2015 |
| 2.0.7-pre | 1,261 | 3/26/2015 |
| 2.0.6-pre | 1,229 | 3/16/2015 |
| 2.0.3-pre | 1,241 | 3/10/2015 |
| 2.0.1-pre | 1,261 | 2/1/2015 |
| 2.0.0-beta-27 | 1,156 | 5/6/2016 |
| 1.4.139 | 2,409 | 1/23/2015 |
| 1.4.118 | 1,660 | 1/13/2015 |
| 1.4.113 | 1,809 | 1/6/2015 |
| 1.4.102 | 2,171 | 12/21/2014 |
| 1.4.99 | 2,082 | 12/18/2014 |
| 1.4.97 | 1,920 | 12/18/2014 |
| 1.4.76 | 2,031 | 12/8/2014 |
| 1.4.39 | 1,891 | 11/26/2014 |
| 1.4.34 | 1,855 | 11/24/2014 |
| 1.4.28 | 1,912 | 11/24/2014 |
| 1.4.27 | 1,922 | 11/23/2014 |
| 1.4.23 | 2,019 | 11/21/2014 |
| 1.4.21 | 1,903 | 11/21/2014 |
| 1.4.18 | 2,007 | 11/18/2014 |
| 1.4.15 | 2,952 | 11/4/2014 |
| 1.4.14 | 1,863 | 10/23/2014 |
| 1.4.13 | 1,737 | 10/23/2014 |
| 1.4.12 | 1,817 | 10/12/2014 |
| 1.4.11 | 1,755 | 10/8/2014 |
| 1.4.10 | 1,778 | 9/26/2014 |
| 1.4.9 | 1,861 | 9/17/2014 |
| 1.4.8 | 1,775 | 9/11/2014 |
| 1.4.7 | 1,806 | 9/1/2014 |
| 1.4.6 | 1,726 | 8/31/2014 |
| 1.4.5 | 1,898 | 8/27/2014 |
| 1.4.4 | 1,766 | 8/27/2014 |
| 1.4.3 | 1,871 | 8/25/2014 |
| 1.4.2 | 1,803 | 8/23/2014 |
| 1.4.1 | 1,779 | 8/23/2014 |
| 1.3.43 | 2,052 | 8/4/2014 |
| 1.3.42 | 1,801 | 7/30/2014 |
| 1.3.41 | 1,772 | 7/28/2014 |
| 1.3.40 | 1,712 | 7/26/2014 |
| 1.3.39 | 1,733 | 7/25/2014 |
| 1.3.36 | 1,740 | 7/20/2014 |
| 1.3.35 | 1,729 | 7/17/2014 |
| 1.3.34 | 2,424 | 7/6/2014 |
| 1.3.33 | 1,746 | 6/30/2014 |
| 1.3.30 | 1,798 | 6/19/2014 |
| 1.3.29 | 1,789 | 6/19/2014 |
| 1.3.28 | 1,716 | 6/19/2014 |
| 1.3.27 | 1,753 | 6/18/2014 |
| 1.3.25 | 1,804 | 6/9/2014 |
| 1.3.24 | 1,862 | 5/21/2014 |
| 1.3.23 | 1,757 | 5/20/2014 |
| 1.3.20 | 1,808 | 5/18/2014 |
| 1.3.19 | 1,741 | 5/17/2014 |
| 1.3.18 | 1,715 | 5/17/2014 |
| 1.3.17 | 1,736 | 5/17/2014 |
| 1.3.16 | 1,748 | 5/17/2014 |
| 1.3.15 | 1,745 | 5/16/2014 |
| 1.3.14 | 1,848 | 5/16/2014 |
| 1.3.13 | 1,751 | 5/16/2014 |
| 1.3.12 | 1,759 | 5/14/2014 |
| 1.3.7 | 1,752 | 5/11/2014 |
| 1.3.6 | 1,786 | 5/9/2014 |
| 1.3.5 | 1,759 | 5/6/2014 |
| 1.3.4 | 1,878 | 5/4/2014 |
| 1.3.3 | 1,942 | 4/28/2014 |
| 1.3.1 | 1,783 | 4/26/2014 |
| 1.2.53 | 1,806 | 4/26/2014 |
| 1.2.52 | 2,033 | 4/24/2014 |
| 1.2.51 | 1,952 | 4/18/2014 |
| 1.2.50 | 1,920 | 4/18/2014 |
| 1.2.49 | 1,904 | 4/17/2014 |
| 1.2.48 | 1,880 | 4/14/2014 |
| 1.2.47 | 2,000 | 4/14/2014 |
| 1.2.45 | 2,036 | 4/13/2014 |
| 1.2.44 | 1,939 | 4/9/2014 |
| 1.2.41 | 1,890 | 4/7/2014 |
| 1.2.40 | 1,858 | 4/7/2014 |
| 1.2.39 | 1,842 | 3/29/2014 |
| 1.2.37 | 1,852 | 3/29/2014 |
| 1.2.29 | 1,868 | 3/16/2014 |
| 1.2.26 | 1,787 | 3/12/2014 |
| 1.2.25 | 1,914 | 2/20/2014 |
| 0.9.9 | 1,954 | 11/23/2013 |
| 0.9.1 | 1,929 | 8/24/2013 |
| 0.8.5 | 1,951 | 7/22/2013 |
| 0.8.1 | 1,998 | 7/9/2013 |
| 0.7.2 | 2,017 | 7/6/2013 |
| 0.6.1 | 1,945 | 6/13/2013 |
| 0.5.2 | 1,909 | 5/27/2013 |
| 0.5.1 | 1,911 | 5/26/2013 |
| 0.4.3 | 1,917 | 5/25/2013 |
| 0.3.2 | 1,869 | 5/19/2013 |
| 0.3.1 | 1,856 | 5/19/2013 |
| 0.2.10 | 1,896 | 5/13/2013 |
| 0.2.9 | 1,820 | 5/10/2013 |
| 0.2.7 | 1,822 | 5/8/2013 |
| 0.2.6 | 1,894 | 5/8/2013 |
| 0.2.5 | 1,868 | 5/7/2013 |
| 0.2.1 | 1,890 | 4/8/2013 |
| 0.1.18 | 1,915 | 4/6/2013 |
| 0.1.17 | 1,859 | 4/4/2013 |
| 0.1.16 | 1,868 | 4/3/2013 |
| 0.1.12 | 1,921 | 4/1/2013 |
| 0.1.11 | 3,819 | 3/30/2013 |
v7.2 - MongoDB v7.x compatibility fix: Error handling now uses error codes instead of string matching (#98, #99). Comprehensive unit tests added (#100). CI improvements.