Maniceraf.SimpleMongoDbLogger is a versatile logging library that seamlessly integrates with MongoDB, providing developers with an efficient means to log messages of various severity levels directly into a MongoDB database.
To begin using Maniceraf.SimpleMongoDbLogger start by installing the latest version of the package from Nuget.
You can install the Maniceraf.SimpleMongoDbLogger via NuGet. Use the Package Manager Console or the .NET CLI:
dotnet add package Maniceraf.SimpleMongoDbLogger --version 1.0.1
Initialize the logger by providing the MongoDB connection string, database name, and collection name:
using Maniceraf.SimpleMongoDbLogger;
class Program
{
static void Main(string[] args)
{
string connectionString = "...";
string databaseName = "...";
string collectionName = "...";
var logger = new MongoDbLogger(connectionString, databaseName, collectionName);
// Log some messages
logger.WriteLog(LogLevel.Info, "This is an informational message");
logger.WriteLog(LogLevel.Warning, "This is a warning message");
logger.WriteLog(LogLevel.Error, "This is an error message");
await logger.WriteLogAsync(LogLevel.Info, "This is an informational message");
await logger.WriteLogAsync(LogLevel.Warning, "This is a warning message");
await logger.WriteLogAsync(LogLevel.Error, "This is an error message");
}
}
If you prefer using Dependency Injection:
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using MongoDBLogger;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// MongoDB configuration
string connectionString = "...";
string databaseName = "...";
string collectionName = "...";
// Register MongoDBLogger as a service
services.AddSimpleMongoDbLogger(options =>
{
options.ConnectionString = connectionString;
options.DatabaseName = databaseName;
options.CollectionName = collectionName;
});
// Or register MongoDBLogger with another way
services.AddSimpleMongoDbLogger(connectionString, databaseName, collectionName);
// Other service registrations...
}
}
using MongoDBLogger;
public class SomeClass
{
private readonly IMongoDbLogger _logger;
public SomeClass(IMongoDbLogger logger)
{
_logger = logger;
}
public void SomeMethod()
{
// Log some messages
_logger.WriteLog(LogLevel.Info, "This is an informational message");
await _logger.WriteLogAsync(LogLevel.Info, "This is an informational message");
// Logging other messages...
}
}
This library is licensed under the MIT License.