This project is to allow Serilog to log to RabbitMQ using the RabbitMQ.Client package. The aim is to expose RabbitMQ.Client functionality, in a logical way, and not to build in additional logic into the sink. So expect pure RabbitMQ.Client behavior, but perhaps a little bit simpler interface.
As of v3.0.0 we use Semantic Versioning to express changes in the API.
Serilog.Sinks.RabbitMQ | .NETStandard | .NETFramework | Serilog | RabbitMQ.Client |
---|---|---|---|---|
2.0.0 | 1.6.0 | 4.5.1 | 2.3.0 | 4.* |
3.0.0 | 1.6.1 | 4.5.1 | 2.8.0 | 5.1.0 |
Using Nuget:
Install-Package Serilog.Sinks.RabbitMQ
To use with ILoggerFactory
via dependency injection,
add the following to ConfigureServices
in your Startup
class.
See the logging documentation
for specific help on using the ILoggerFactory
and ILogger<T>
.
using Serilog;
using Serilog.Formatting.Json;
using Serilog.Sinks.RabbitMQ;
using Serilog.Sinks.RabbitMQ.Sinks.RabbitMQ;
public class Startup
{
private readonly IConfiguration _config;
// ...
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var config = new RabbitMQConfiguration
{
Hostname = _config["RABBITMQ_HOST"],
Username = _config["RABBITMQ_USER"],
Password = _config["RABBITMQ_PASSWORD"],
Exchange = _config["RABBITMQ_EXCHANGE"],
ExchangeType = _config["RABBITMQ_EXCHANGE_TYPE"],
DeliveryMode = RabbitMQDeliveryMode.Durable,
RouteKey = "Logs",
Port = 5672
};
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.RabbitMQ(config, new JsonFormatter())
.CreateLogger();
var loggerFactory = new LoggerFactory();
loggerFactory
.AddSerilog()
.AddConsole(LogLevel.Information);
services.AddSingleton<ILoggerFactory>(loggerFactory);
}
// ...
}
There are multiple ways for configuring the RabbitMqSink with the release of v3.0.0
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) => {
clientConfiguration.Username = _config["RABBITMQ_USER"];
clientConfiguration.Password = _config["RABBITMQ_PASSWORD"];
clientConfiguration.Exchange = _config["RABBITMQ_EXCHANGE"];
clientConfiguration.ExchangeType = _config["RABBITMQ_EXCHANGE_TYPE"];
clientConfiguration.DeliveryMode = RabbitMQDeliveryMode.Durable;
clientConfiguration.RouteKey = "Logs";
clientConfiguration.Port = 5672;
foreach (string hostname in _config["RABBITMQ_HOSTNAMES"]) {
clientConfiguration.Hostnames.Add(hostname);
}
sinkConfiguration.TextFormatter = new JsonFormatter();
}).CreateLogger();
// Or
var config = new RabbitMQClientConfiguration
{
Port = 5672,
DeliveryMode = RabbitMQ.RabbitMQDeliveryMode.Durable,
Exchange = "test_exchange",
Username = "guest",
Password = "guest",
ExchangeType = "fanout"
};
foreach (string hostname in _config["RABBITMQ_HOSTNAMES"]) {
config .Hostnames.Add(hostname);
}
var logger = new LoggerConfiguration()
.WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) => {
clientConfiguration.From(config);
sinkConfiguration.TextFormatter = new JsonFormatter();
}) .CreateLogger();
// Or
var logger = new LoggerConfiguration()
.WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) => {
clientConfiguration.From(Configuration.Bind("RabbitMQClientConfiguration", new RabbitMQClientConfiguration()));
sinkConfiguration.TextFormatter = new JsonFormatter();
}).CreateLogger();
// Or
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
IConfigurationSection rabbitMqSection = configuration.GetSection("log:rabbitMq");
loggerConfiguration = loggerConfiguration
.WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) =>
{
rabbitMqSection.Bind(clientConfiguration);
sinkConfiguration.RestrictedToMinimumLevel = LogEventLevel.Warning;
});
// At last, don't forget to register the logger into the services
var loggerFactory = new LoggerFactory();
loggerFactory
.AddSerilog()
.AddConsole(LogLevel.Information);
services.AddSingleton<ILoggerFactory>(loggerFactory);