A Serilog sink that writes events as documents to MongoDB.
Package - Serilog.Sinks.MongoDB | Platforms - .NET 4.6, .NETStandard 1.5
In the example shown, the sink will write to the database logs
. 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 usage defaults to writing to `log` collection
var log = new LoggerConfiguration()
.WriteTo.MongoDB("mongodb://mymongodb/logs")
.CreateLogger();
// creates custom collection `applog`
var log = new LoggerConfiguration()
.WriteTo.MongoDB("mongodb://mymongodb/logs", collectionName: "applog")
.CreateLogger();
Additionally, you can utilize a Capped Collection.
// basic with custom collection name
var log = new LoggerConfiguration()
.WriteTo.MongoDBCapped("mongodb://mymongodb/logs", collectionName: "rollingapplog")
.CreateLogger();
// optional parameters cappedMaxSizeMb and cappedMaxDocuments specified
var log = new LoggerConfiguration()
.WriteTo.MongoDBCapped("mongodb://mymongodb/logs", cappedMaxSizeMb: 50, cappedMaxDocuments: 1000)
.CreateLogger();