Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aidanmorgan/1044486 to your computer and use it in GitHub Desktop.
Save aidanmorgan/1044486 to your computer and use it in GitHub Desktop.
/// <summary>
/// A <see cref="SingleThreadedMongoDbLogRepository"/> is an implementation of the <see cref="ILogRepository"/> interface that
/// wraps an existing <see cref="ILogRepository"/>, but performs all of the write operations in a background thread, controlled
/// by the <see cref="MongoDbLoggerWriteThread"/>.
///
/// This allows write operations to not impact on the calling code at all, ensuring the actual write is performed in the background.
/// </summary>
class SingleThreadedMongoDbLogRepository : ILogRepository
{
private readonly MongoDbLoggerWriteThread _writer;
private readonly ILogRepository _repository;
/// <summary>
/// Creates a new <see cref="SingleThreadedMongoDbLogRepository"/>, using the provided <see cref="ILogRepository"/> instance
/// to perform the actual database operations.
/// </summary>
public SingleThreadedMongoDbLogRepository(ILogRepository repo)
{
_writer = new MongoDbLoggerWriteThread(repo);
_repository = repo;
Thread t = new Thread(() => _writer.Run()) { Name = "Mongo DB Writer Thread" };
t.Start();
}
public void AddLog(string logger, LogEntry message)
{
_writer.AddLogEntry(message);
}
public void AddLog(string logger, KeyedLogEntry message)
{
_writer.AddLogEntry(message);
}
public IList<LogEntry> GetLog(string logger)
{
return _repository.GetLog(logger);
}
public IList<KeyedLogEntry> GetLog(string logger, string key)
{
return _repository.GetLog(logger, key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment