コード例 #1
0
        public MongoDBContext(MongoDBLoggerOptions options)
        {
            _loggerOptions = options;

            var mongoUrl = MongoUrl.Create(options.DatabaseUrl);

            _database = new MongoClient(mongoUrl)
                        .GetDatabase(mongoUrl.DatabaseName);

            ConventionRegistry.Register("IgnoreIfDefault",
                                        new ConventionPack {
                new IgnoreIfDefaultConvention(true)
            }, t => true);

            ConventionRegistry.Register("EnumStringConvention",
                                        new ConventionPack {
                new EnumRepresentationConvention(BsonType.String)
            }, t => true);

            ConventionRegistry.Register("IgnoreExtraElementsConvention",
                                        new ConventionPack {
                new IgnoreExtraElementsConvention(true)
            }, t => true);
        }
コード例 #2
0
 public MongoDBLogger(MongoDBLoggerOptions options)
 {
     Options = options;
     CreateMongoDBContext(Options);
 }
コード例 #3
0
        /// <summary>
        /// Gets a collection. If not exists, creates the collection with the specified options.
        /// </summary>
        /// <typeparam name="TDocument">the document type.</typeparam>
        /// <param name="database">MongoDB database.</param>
        /// <param name="options">MongoDB logger options.</param>
        /// <returns>An implementation of a collection.</returns>
        public static IMongoCollection <TDocument> GetCollection <TDocument>(this IMongoDatabase database, MongoDBLoggerOptions options)
        {
            var collection = database.GetCollection <TDocument>(options.CollectionName);

            if (!collection.Exists())
            {
                var collectionOptions = new CreateCollectionOptions();
                collectionOptions.Capped = options.Capped;
                if (options.Capped)
                {
                    collectionOptions.MaxSize      = options.MaxSize;
                    collectionOptions.MaxDocuments = options.MaxDocuments;
                }

                database.CreateCollection(options.CollectionName, collectionOptions);
            }
            return(collection);
        }