Purge Old Mongo Logs without User Intervention

[复制链接]
查看11 | 回复1 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
One of thecoolest feature of Mongo is the concept of Capped Collection, or “fixed size” collection. They are based ona FIFO queue where the first record to be discharded is the first inserted, andthis is exceptional to create a log-collection that automatically purge all oldlogs without any user intervention.
To be ableto automatically enable this feature on the Log4Net Mongo appender you need to do a little modification to thecode, this is because the original code simply gets a reference to thecollection with this code.connection = MongoServer.Create(mongoConnectionString.ToString());
connection.Connect();
var db = connection.GetDatabase(DatabaseName);
collection = db.GetCollection(CollectionName);
复制代码



回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
C# drivers for Mongo automatically creates a collection if it is not present, this means that when you call db.GetCollection if the collection is not present it will be automatically created, but it is not capped. To solve this problem you can modify the initialization code with this code.if (!db.CollectionExists(collectionName)) {
var options =CollectionOptions
.SetCapped(true)
.SetMaxSize(CappedSize);
db.CreateCollection(collectionName, options);
}
collection = db.GetCollection(CollectionName);
复制代码MongoDb C# drivers has a class called CollectionOptions used to setup options to create a new MongoCollection and it can be accessed with a really easy Fluent-Interface, in my example I call SetCapped(true) to enable a capped collection and SetMaxSize() to setup the maximum size in bytes. The size of the capped-collection is stored in the appender property called CappedSize, the default is 500MB, but you can setup any size you likes in standard log4Net configuration.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行