ChangemakerStudios / serilog-sinks-mongodb

A sink for Serilog that writes events to MongoDB
Apache License 2.0
92 stars 53 forks source link

Capped collection is not created #44

Closed vanamingo closed 3 years ago

vanamingo commented 5 years ago

I call:

var log = new LoggerConfiguration()
    .WriteTo.MongoDBCapped("mongodb://mymongodb/logs", collectionName: "rollingapplog")
    .CreateLogger();

Expected: MongoDBCapped method creates Capped collection. Actual: It doesn't create a capped collection. A common collection is created at the moment when some log writes to the collection.

Root cause description: I have debugged your code and found that MongoDBCapped method verifies if the collection already exists. If not, then it creates a capped collection. It seems that the following method is broken. database.GetCollection always return some object. And it always returns true even for nonexistent collection.

internal static bool CollectionExists(this IMongoDatabase database, string collectionName)
        {
            var collection = database.GetCollection<BsonDocument>(collectionName);
            return collection != null;
        }

I use: MongoDB v 4.0.5 MongoDB driver 2.7.3

Serilog.AspNetCore 2.1.1 Serilog.Settings.Configuration 3.0.1 Serilog.Sinks.Console 3.1.1 Serilog.Sinks.ElasticSearch 7.1.0 Serilog.Sinks.MongoDB 4.0.0 Serilog.Sinks.SyslogServer 1.0.10

cubanx commented 5 years ago

Yep, I verified this as well. If you create the collection ahead of time with a cap in place, it works fine.

It's a workaround, but it'd be nice if it works out of the box.

karthik-v-raman commented 4 years ago

I created a pull request to fix this issue. It would be quite great if it could be reviewed (https://github.com/serilog/serilog-sinks-mongodb/pull/54).

Jaben commented 3 years ago

Fix is in latest dev.