Exemplo n.º 1
0
        public Boolean SetupCollection()
        {
            var            uri    = new MongoUrl(ConnectionString);
            var            client = new MongoClient(uri);
            IMongoDatabase db     = client.GetDatabase(uri.DatabaseName);

            Task.Factory.StartNew(() =>
            {
                var result = client.ListDatabases();
                Console.Write(result.ToList());
            }); //forces a database connection
            Int32        spinCount = 0;
            ClusterState clusterState;

            while ((clusterState = client.Cluster.Description.State) != ClusterState.Connected &&
                   spinCount++ < 100)
            {
                Thread.Sleep(20);
            }
            //database is not operational;
            if (clusterState != ClusterState.Connected)
            {
                return(false);
            }

            var   createCollectionOptions = new CreateCollectionOptions();
            Int64 cappedSize;

            if (Int64.TryParse(CappedSizeInMb, out cappedSize))
            {
                createCollectionOptions.Capped  = true;
                createCollectionOptions.MaxSize = 1024L * 1024L * cappedSize;
            }
            var collections = db.ListCollections()
                              .ToList()
                              .Select(d => d["name"].AsString)
                              .ToList();

            if (!collections.Contains(CollectionName))
            {
                db.CreateCollection(CollectionName, createCollectionOptions);
            }
            LogCollection = db.GetCollection <BsonDocument>(CollectionName);
            CreateIndexOptions options = new CreateIndexOptions();

            if (ExpireAfter != null)
            {
                if (createCollectionOptions.Capped == true)
                {
                    LogLog.Error(typeof(MongoLog), "Cannot use Capped and TTL at the same time, only capped collection setting will be used! Please configure the appender specifying only CappedSizeInMb or ExpireAfter, but not both.");
                }
                else
                {
                    options.ExpireAfter = ExpireAfter.ToTimeSpan();
                }
            }

            LogCollection.Indexes.CreateOne(Builders <BsonDocument> .IndexKeys.Descending(FieldNames.Timestamp), options);
            LogCollection.Indexes.CreateOne(Builders <BsonDocument> .IndexKeys
                                            .Ascending(FieldNames.Level).Ascending(FieldNames.Thread).Ascending(FieldNames.Loggername));
            return(true);
        }
Exemplo n.º 2
0
        public void SetupCollection()
        {
            var           uri    = new MongoUrl(ConnectionString);
            var           client = new MongoClient(uri);
            MongoDatabase db     = client.GetServer().GetDatabase(uri.DatabaseName);
            Int64         cappedSize;

            if (!Int64.TryParse(CappedSizeInMb, out cappedSize))
            {
                cappedSize = 5 * 1024L;
            }
            if (!db.CollectionExists(CollectionName))
            {
                CollectionOptionsBuilder options = CollectionOptions
                                                   .SetCapped(true)
                                                   .SetMaxSize(1024L * 1024L * cappedSize); //5 gb.
                db.CreateCollection(CollectionName, options);
            }
            LogCollection = db.GetCollection(CollectionName);
            var builder = new IndexOptionsBuilder();

            const string ttlIndex = FieldNames.Timestamp + "_-1";
            var          index    = LogCollection.GetIndexes().SingleOrDefault(x => x.Name == ttlIndex);

            if (index != null)
            {
                if (ExpireAfter != null)
                {
                    if (index.TimeToLive != ExpireAfter.ToTimeSpan())
                    {
                        var d = new CommandDocument()
                        {
                            { "collMod", CollectionName },
                            {
                                "index", new BsonDocument
                                {
                                    { "keyPattern", new BsonDocument {
                                          { FieldNames.Timestamp, -1 }
                                      } },
                                    { "expireAfterSeconds", (int)(ExpireAfter.ToTimeSpan().TotalSeconds) }
                                }
                            }
                        };

                        db.RunCommand(d);
                    }
                }
            }
            else
            {
                if (ExpireAfter != null)
                {
                    builder.SetTimeToLive(ExpireAfter.ToTimeSpan());
                }

                LogCollection.CreateIndex(IndexKeys.Descending(FieldNames.Timestamp), builder);
            }

            LogCollection.CreateIndex(IndexKeys
                                      .Ascending(FieldNames.Level, FieldNames.Thread, FieldNames.Loggername)
                                      );
        }