Skip to content

Instantly share code, notes, and snippets.

@jasonrdsouza
Created July 28, 2011 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrdsouza/1112193 to your computer and use it in GitHub Desktop.
Save jasonrdsouza/1112193 to your computer and use it in GitHub Desktop.
Simple C# class to connect to and write messages to MongoDB
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