You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Upgrade MongoDB.Driver to v3.0 - .NET Standard 2.0 support has been removed.
New in v6.x
Upgrade MongoDB.Driver to v2.28.0 (Thanks to Memoyu)
Add trace context to LogEntry (Thanks to fernandovmp)
New in v5.x
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.
Rolling Log Collection Naming (Thanks to Revazashvili for the PR!). MongoDBBson sink only.
Expire TTL support. MongoDBBson sink only.
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:
usingSerilog;// use BSON structured logsvarlog=newLoggerConfiguration().WriteTo.MongoDBBson("mongodb://mymongodb/logs").CreateLogger();log.Information("This is a test log message");
Capped Collection:
// capped collection using BSON structured logsvarlog=newLoggerConfiguration().WriteTo.MongoDBBson("mongodb://mymongodb/logs", cfg =>{// optional configuration options:cfg.SetCollectionName("log");cfg.SetBatchPeriod(TimeSpan.FromSeconds(1));// create capped collection that is max 100mbcfg.SetCreateCappedCollection(100);}).CreateLogger();
Custom Mongodb Settings:
// create sink instance with custom mongodb settings.varlog=newLoggerConfiguration().WriteTo.MongoDBBson(cfg =>{// custom MongoDb configurationvarmongoDbSettings=newMongoClientSettings{UseTls=true,AllowInsecureTls=true,Credential=MongoCredential.CreateCredential("databaseName","username","password"),Server=newMongoServerAddress("127.0.0.1")};varmongoDbInstance=newMongoClient(mongoDbSettings).GetDatabase("serilog");// sink will use the IMongoDatabase instance providedcfg.SetMongoDatabase(mongoDbInstance);cfg.SetRollingInternal(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: