![]() |
VOOZH | about |
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" />Directory.Packages.props
<PackageReference Include="Serilog.Sinks.MongoDB" />Project file
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.0Install as a Cake Addin
#tool nuget:?package=Serilog.Sinks.MongoDB&version=7.2.0Install as a Cake Tool
👁 NuGet version
👁 Downloads
👁 Build status
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
See for complete version history.
Install the sink via NuGet Package Manager Console:
Install-Package Serilog.Sinks.MongoDB
or via the .NET CLI:
dotnet add package Serilog.Sinks.MongoDB
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.
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 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();
// 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();
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"
}
}
]
}
}
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();
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();
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();
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();
.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:
SetExpireTTL)SetExcludeMessageTemplate)SetRollingInterval)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"
Problem: System.Guid cannot be mapped to BsonValue
Solution: Ensure you're using the latest version (v7.1+) which includes Guid mapping fixes.
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
Problem: Collection created without time format suffix
Solution: Ensure you're using MongoDBBson sink (not legacy MongoDB) and v7.1+.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net472 net472 is compatible. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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
实体对象基类,及其常用操作。 常用操作包括:对象转换、序列化、日志、常用异常处理、依赖注入。 |
|
|
AIC.Core.Logging.Serilog
Shared .NET library for Aerospace, Intelligence, and Cyber solutions. |
|
|
Basic.Logging.Serilog
Package Description |
|
|
Pina.SharedApi
Package Description |
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 | 228,579 | 10/26/2025 |
| 7.1.0 | 44,459 | 10/5/2025 |
| 7.0.0 | 590,079 | 11/5/2024 |
| 6.0.0 | 175,976 | 9/20/2024 |
| 5.4.1 | 478,688 | 2/18/2024 |
| 5.3.1 | 1,266,354 | 9/28/2022 |
| 5.2.2 | 8,656 | 9/26/2022 |
| 5.2.1 | 34,429 | 9/3/2022 |
| 5.2.1-tags-v5-2-0-0000 | 480 | 9/3/2022 |
| 5.2.0 | 1,992 | 9/3/2022 |
| 5.1.5 | 324,198 | 4/24/2022 |
| 5.1.2 | 185,503 | 1/19/2022 |
| 5.1.2-dev-00124 | 538 | 3/12/2022 |
| 5.1.2-dev-00123 | 756 | 1/19/2022 |
| 5.1.2-dev-00121 | 719 | 1/19/2022 |
| 5.1.1-dev-00120 | 782 | 1/19/2022 |
| 5.1.1-dev-00119 | 725 | 1/19/2022 |
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.