|
7 | 7 | A Serilog sink that writes events as documents to [MongoDB](http://mongodb.org). |
8 | 8 |
|
9 | 9 | **Package** - [Serilog.Sinks.MongoDB](http://nuget.org/packages/serilog.sinks.mongodb) |
10 | | -**Platforms** - .NET 4.7.2, .NET 6.0, .NET Standard 2.1 |
| 10 | +**Platforms** - .NET 4.7.2, .NET 6.0+, .NET Standard 2.1 |
11 | 11 |
|
12 | 12 | ## Whats New |
13 | 13 |
|
14 | | -#### New in v7.x |
15 | | -* Upgrade MongoDB.Driver to v3.0 - .NET Standard 2.0 support has been removed. |
| 14 | +See [CHANGES.md](CHANGES.md) for complete version history. |
16 | 15 |
|
17 | | -#### New in v6.x |
18 | | -* Upgrade MongoDB.Driver to v2.28.0 (Thanks to [Memoyu](https://github.com/Memoyu)) |
19 | | -* Add trace context to LogEntry (Thanks to [fernandovmp](https://github.com/fernandovmp)) |
20 | | - |
21 | | -#### New in v5.x |
22 | | -* Output structured MongoDB Bson logs by switching to `.MongoDBBson()` extensions. Existing `.MongoDB()` extensions will continue to work converting logs to Json and then to Bson. |
23 | | -* Rolling Log Collection Naming (Thanks to [Revazashvili](https://github.com/Revazashvili) for the PR!). MongoDBBson sink only. |
24 | | -* Expire TTL support. MongoDBBson sink only. |
| 16 | +#### New in v7.2 |
| 17 | +* Fixed MongoDB v7.x compatibility - Error handling now uses error codes instead of string matching (#98, #99) |
| 18 | +* Added comprehensive unit tests for MongoDB error handling |
| 19 | +* Improved CI performance |
25 | 20 |
|
26 | 21 | ## Installation |
27 | 22 | Install the sink via NuGet Package Manager Console: |
@@ -106,21 +101,156 @@ Keys and values are not case-sensitive. This is an example of configuring the Mo |
106 | 101 | } |
107 | 102 | }, |
108 | 103 | "WriteTo": [ |
109 | | - { |
110 | | - "Name": "MongoDBBson", |
111 | | - "Args": { |
| 104 | + { |
| 105 | + "Name": "MongoDBBson", |
| 106 | + "Args": { |
112 | 107 | "databaseUrl": "mongodb://username:password@ip:port/dbName?authSource=admin", |
113 | 108 | "collectionName": "logs", |
114 | 109 | "cappedMaxSizeMb": "1024", |
115 | 110 | "cappedMaxDocuments": "50000", |
116 | 111 | "rollingInterval": "Month" |
117 | 112 | } |
118 | | - } |
| 113 | + } |
119 | 114 | ] |
120 | 115 | } |
121 | 116 | } |
122 | 117 | ``` |
123 | 118 |
|
| 119 | +## Advanced Configuration |
| 120 | + |
| 121 | +### Authentication & Secure Connections |
| 122 | + |
| 123 | +For password-protected MongoDB instances, Azure Cosmos DB, or SSL/TLS connections: |
| 124 | + |
| 125 | +```csharp |
| 126 | +var log = new LoggerConfiguration() |
| 127 | + .WriteTo.MongoDBBson(cfg => |
| 128 | + { |
| 129 | + var mongoDbSettings = new MongoClientSettings |
| 130 | + { |
| 131 | + UseTls = true, |
| 132 | + AllowInsecureTls = false, // set true only for dev/testing |
| 133 | + Credential = MongoCredential.CreateCredential("databaseName", "username", "password"), |
| 134 | + Server = new MongoServerAddress("your-server.com", 27017) |
| 135 | + }; |
| 136 | + |
| 137 | + var mongoDbInstance = new MongoClient(mongoDbSettings).GetDatabase("logs"); |
| 138 | + cfg.SetMongoDatabase(mongoDbInstance); |
| 139 | + }) |
| 140 | + .CreateLogger(); |
| 141 | +``` |
| 142 | + |
| 143 | +**Azure Cosmos DB** (MongoDB API): |
| 144 | +```csharp |
| 145 | +var connectionString = "mongodb://cosmosdb-account:key@cosmosdb-account.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false"; |
| 146 | +var log = new LoggerConfiguration() |
| 147 | + .WriteTo.MongoDBBson(connectionString) |
| 148 | + .CreateLogger(); |
| 149 | +``` |
| 150 | + |
| 151 | +### TTL / Auto-Expiration |
| 152 | + |
| 153 | +Automatically delete old logs using MongoDB's TTL feature: |
| 154 | + |
| 155 | +```csharp |
| 156 | +var log = new LoggerConfiguration() |
| 157 | + .WriteTo.MongoDBBson(cfg => |
| 158 | + { |
| 159 | + cfg.SetMongoUrl("mongodb://localhost/logs"); |
| 160 | + cfg.SetExpireTTL(TimeSpan.FromDays(30)); // logs expire after 30 days |
| 161 | + }) |
| 162 | + .CreateLogger(); |
| 163 | +``` |
| 164 | + |
| 165 | +### Exclude Redundant Fields |
| 166 | + |
| 167 | +Reduce storage costs by excluding the `MessageTemplate` field (the rendered message is still stored): |
| 168 | + |
| 169 | +```csharp |
| 170 | +var log = new LoggerConfiguration() |
| 171 | + .WriteTo.MongoDBBson(cfg => |
| 172 | + { |
| 173 | + cfg.SetMongoUrl("mongodb://localhost/logs"); |
| 174 | + cfg.SetExcludeMessageTemplate(true); // saves storage space |
| 175 | + }) |
| 176 | + .CreateLogger(); |
| 177 | +``` |
| 178 | + |
| 179 | +### Rolling Collections |
| 180 | + |
| 181 | +Create time-based collections (e.g., one per day/month): |
| 182 | + |
| 183 | +```csharp |
| 184 | +var log = new LoggerConfiguration() |
| 185 | + .WriteTo.MongoDBBson(cfg => |
| 186 | + { |
| 187 | + cfg.SetMongoUrl("mongodb://localhost/logs"); |
| 188 | + cfg.SetCollectionName("log"); |
| 189 | + cfg.SetRollingInterval(RollingInterval.Day); // creates: log-20251004, log-20251005, etc. |
| 190 | + }) |
| 191 | + .CreateLogger(); |
| 192 | +``` |
| 193 | + |
| 194 | +**Collection naming patterns:** |
| 195 | +- `RollingInterval.Day` → `log-yyyyMMdd` (e.g., `log-20251004`) |
| 196 | +- `RollingInterval.Month` → `log-yyyyMM` (e.g., `log-202510`) |
| 197 | +- `RollingInterval.Year` → `log-yyyy` (e.g., `log-2025`) |
| 198 | + |
| 199 | +**Querying rolling collections:** |
| 200 | +```csharp |
| 201 | +// Query specific date range - you need to target the correct collection(s) |
| 202 | +var collectionName = $"log-{DateTime.UtcNow:yyyyMMdd}"; |
| 203 | +var collection = database.GetCollection<BsonDocument>(collectionName); |
| 204 | +var todayLogs = collection.Find(Builders<BsonDocument>.Filter.Empty).ToList(); |
| 205 | +``` |
| 206 | + |
| 207 | +## Migration Guide |
| 208 | + |
| 209 | +### From Legacy `.MongoDB()` to `.MongoDBBson()` |
| 210 | + |
| 211 | +The legacy `.MongoDB()` sink converts logs to JSON then to BSON. The newer `.MongoDBBson()` writes structured BSON directly for better performance and features. |
| 212 | + |
| 213 | +**Legacy (still supported):** |
| 214 | +```csharp |
| 215 | +.WriteTo.MongoDB("mongodb://localhost/logs") // converts to JSON first |
| 216 | +``` |
| 217 | + |
| 218 | +**New Bson sink (recommended):** |
| 219 | +```csharp |
| 220 | +.WriteTo.MongoDBBson("mongodb://localhost/logs") // native BSON |
| 221 | +``` |
| 222 | + |
| 223 | +**MongoDBBson exclusive features:** |
| 224 | +- TTL/Expiration (`SetExpireTTL`) |
| 225 | +- Exclude message template (`SetExcludeMessageTemplate`) |
| 226 | +- Rolling collections (`SetRollingInterval`) |
| 227 | +- Better type mapping and performance |
| 228 | + |
| 229 | +## Troubleshooting |
| 230 | + |
| 231 | +### MongoDB Connection Issues |
| 232 | +**Problem:** Application hangs or fails when MongoDB is unavailable |
| 233 | +**Solution:** Ensure your MongoDB connection string includes appropriate timeouts: |
| 234 | +```csharp |
| 235 | +"mongodb://localhost/logs?connectTimeoutMS=3000&serverSelectionTimeoutMS=3000" |
| 236 | +``` |
| 237 | + |
| 238 | +### Type Mapping Errors |
| 239 | +**Problem:** `System.Guid cannot be mapped to BsonValue` |
| 240 | +**Solution:** Ensure you're using the latest version (v7.1+) which includes Guid mapping fixes. |
| 241 | + |
| 242 | +### Capped Collections Not Created |
| 243 | +**Problem:** Capped collection configuration not working |
| 244 | +**Solution:** Ensure the collection doesn't already exist. Drop existing collection first: |
| 245 | +```csharp |
| 246 | +database.DropCollection("logs"); |
| 247 | +// Then configure with capped settings |
| 248 | +``` |
| 249 | + |
| 250 | +### Rolling Collections Not Named Correctly |
| 251 | +**Problem:** Collection created without time format suffix |
| 252 | +**Solution:** Ensure you're using `MongoDBBson` sink (not legacy `MongoDB`) and v7.1+. |
| 253 | + |
124 | 254 | ## Icon |
125 | 255 |
|
126 | 256 | [MongoDB](https://icons8.com/icon/74402/mongodb) icon by [Icons8](https://icons8.com) |
0 commit comments