Created
July 28, 2011 18:33
-
-
Save jasonrdsouza/1112193 to your computer and use it in GitHub Desktop.
Simple C# class to connect to and write messages to MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using MongoDB.Bson; | |
using MongoDB.Bson.IO; | |
using MongoDB.Driver; | |
using MongoDB.Driver.Builders; | |
namespace Tester | |
{ | |
class MongoLogger : Logger | |
{ | |
MongoServer server; | |
MongoDatabase db; | |
MongoCollection coll; | |
public MongoLogger(String db_name, String collection_name) | |
{ | |
server = MongoServer.Create(); //leave out connectionString if connecting to localhost mongo server | |
db = server.GetDatabase(db_name); | |
coll = db.GetCollection<BsonDocument>(collection_name); | |
} | |
public MongoLogger(String db_name, String collection_name, String connectionString) | |
{ | |
server = MongoServer.Create(connectionString); | |
db = server.GetDatabase(db_name); | |
coll = db.GetCollection<BsonDocument>(collection_name); | |
} | |
public void EnsureIndex(String index_key) | |
{ | |
coll.EnsureIndex(new String[] { index_key }); | |
} | |
public void EnsureIndex(String[] index_keys) | |
{ | |
coll.EnsureIndex(index_keys); | |
} | |
public void addLogMessage(BsonDocument message) | |
{ | |
coll.Insert<BsonDocument>(message); | |
} | |
public void addLogMessage(List<BsonDocument> message_batch) | |
{ | |
coll.InsertBatch<BsonDocument>(message_batch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment