Inheritance: log4net.Appender.AppenderSkeleton
Exemplo n.º 1
0
        public MongoCollection GetCollection(MongoDBAppender appender)
        {
            MongoDatabase db = GetDB(appender);

            if (db == null)
            {
                throw new ApplicationException("can not retreive db");
            }

            string          colName    = string.IsNullOrWhiteSpace(appender.CollectionName) ? "logs" : appender.CollectionName;
            MongoCollection collection = db.GetCollection(colName);

            if (!string.IsNullOrWhiteSpace(appender.ShardKey))
            {
                Uri    u   = GetConnectionString(appender);
                string col = u + "#" + colName;
                bool   ok;
                if (!_colmap.TryGetValue(col, out ok) || !ok)
                {
                    lock (_colmap)
                    {
                        _colmap.AddOrUpdate(col, true, (s, b) => true);
                        ShardCollection(collection, appender);
                    }
                }
            }
            return(collection);
        }
Exemplo n.º 2
0
        static void ShardCollection(MongoCollection col, MongoDBAppender appender)
        {
            var cmd = new CommandDocument
            {
                { "shardCollection", col.Database.Name + '.' + col.Name },
                { "key", new BsonDocument(appender.ShardKey, 1) },
            };

            try
            {
                CreateIndex(col, appender.ShardKey);

                MongoServer server  = col.Database.Server;
                var         dbCfg   = new MongoDatabaseSettings();
                var         adminDb = new MongoDatabase(server, "admin", dbCfg);
                server.Connect();
                CommandResult shcr = adminDb.RunCommand(cmd);
            }
            catch (MongoCommandException mex)
            {
                if (!mex.Message.ToLower().Contains("already "))
                {
                    throw;
                }
                else
                {
                    Console.WriteLine("Sharding already enabled!");
                }
            }
        }
Exemplo n.º 3
0
        static Uri GetConnectionString(MongoDBAppender appender)
        {
            if (appender == null)
            {
                throw new ArgumentNullException("appender");
            }

            Uri u = new Uri(string.IsNullOrWhiteSpace(appender.ConnectionString) ? DEFAULT_CONNECTION + DEFAULT_DB : appender.ConnectionString);

            return(u);
        }
Exemplo n.º 4
0
        public static MongoDatabase GetDatabase(MongoDBAppender appender)
        {
            var port = appender.Port > 0 ? appender.Port : 27017;
            var mongoConnectionString = new StringBuilder(string.Format("Server={0}:{1}", appender.Host ?? "localhost", port));
            if(!string.IsNullOrEmpty(appender.UserName) && !string.IsNullOrEmpty(appender.Password))
            {
                // use MongoDB authentication
                mongoConnectionString.AppendFormat(";Username={0};Password={1}", appender.UserName, appender.Password);
            }

            MongoServer connection = MongoServer.Create(mongoConnectionString.ToString());
            connection.Connect();
            return connection.GetDatabase(appender.DatabaseName ?? "log4net_mongodb");
        }
        public static MongoDatabase GetDatabase(MongoDBAppender appender)
        {
            var port = appender.Port > 0 ? appender.Port : 27017;
            var mongoConnectionString = new StringBuilder(string.Format("Server={0}:{1}", appender.Host ?? "localhost", port));
            if (!string.IsNullOrEmpty(appender.UserName) && !string.IsNullOrEmpty(appender.Password))
            {
                // use MongoDB authentication
                mongoConnectionString.AppendFormat(";Username={0};Password={1}", appender.UserName, appender.Password);
            }

            MongoServer connection = MongoServer.Create(mongoConnectionString.ToString()); // TODO Should be replaced with MongoClient, but this will change default for WriteConcern. See http://blog.mongodb.org/post/36666163412/introducing-mongoclient and http://docs.mongodb.org/manual/release-notes/drivers-write-concern
            connection.Connect();
            return connection.GetDatabase(appender.DatabaseName ?? "log4net_mongodb");
        }
        public static MongoDatabase GetDatabase(MongoDBAppender appender)
        {
            var port = appender.Port > 0 ? appender.Port : 27017;
            var mongoConnectionString = new StringBuilder(string.Format("Server={0}:{1}", appender.Host ?? "localhost", port));

            if (!string.IsNullOrEmpty(appender.UserName) && !string.IsNullOrEmpty(appender.Password))
            {
                // use MongoDB authentication
                mongoConnectionString.AppendFormat(";Username={0};Password={1}", appender.UserName, appender.Password);
            }

            MongoServer connection = MongoServer.Create(mongoConnectionString.ToString());             // TODO Should be replaced with MongoClient, but this will change default for WriteConcern. See http://blog.mongodb.org/post/36666163412/introducing-mongoclient and http://docs.mongodb.org/manual/release-notes/drivers-write-concern

            connection.Connect();
            return(connection.GetDatabase(appender.DatabaseName ?? "log4net_mongodb"));
        }
        public static MongoDatabase GetDatabase(MongoDBAppender appender)
        {
            var port = appender.Port > 0 ? appender.Port : 27017;
            var mongoConnectionString = new StringBuilder(string.Format("Server={0}:{1}", appender.Host ?? "localhost", port));

            if (!string.IsNullOrEmpty(appender.UserName) && !string.IsNullOrEmpty(appender.Password))
            {
                // use MongoDB authentication
                mongoConnectionString.AppendFormat(";Username={0};Password={1}", appender.UserName, appender.Password);
            }

            MongoServer connection = MongoServer.Create(mongoConnectionString.ToString());

            connection.Connect();
            return(connection.GetDatabase(appender.DatabaseName ?? "log4net_mongodb"));
        }
Exemplo n.º 8
0
        public MongoDatabase GetDB(MongoDBAppender appender)
        {
            Uri           u = GetConnectionString(appender);
            MongoDatabase db;

            if (_dbmap.TryGetValue(u, out db) && db != null)
            {
                return(db);
            }
            else
            {
                MongoUrl            url      = MongoUrl.Create(u.ToString());
                MongoServerSettings settings = MongoServerSettings.FromUrl(url);
                lock (_dbmap) //lock only during creation...
                {
                    var    conn   = new MongoServer(settings);
                    string dbname = string.IsNullOrWhiteSpace(url.DatabaseName) ? DEFAULT_DB : url.DatabaseName;
                    db = conn.GetDatabase(dbname);
                    EnableSharding(db);
                    return(_dbmap.AddOrUpdate(u, db, (uri, currentDb) => db)); //replaces existing ones...
                }
            }
        }