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.* |
6.0.0 | 2.0.0 | 4.7.2 | 2.8.0 | 6.* |
7.0.0 | 2.0.0 | - | 3.1.1 | 6.8.* |
Using Nuget:
Install-Package Serilog.Sinks.RabbitMQ
See changelog.
- Sink Configuration Options
- External configuration using Serilog.Settings.AppSettings
- External configuration using Serilog.Settings.Configuration
- Audit Sink Configuration
- Multihost configuration
- Configuration via code
The sink can be configured completely through code, by using configuration files (or other types of configuration providers),
a combination of both, or by using the various Serilog configuration packages.
The sink is configured with a typical Serilog WriteTo
configuration method (or AuditTo
, or similar variations).
All sink configuration methods accept the following arguments, though not necessarily in this order. Use of named arguments is strongly recommended.
hostnames
username
password
exchange
exchangeType
deliveryMode
routeKey
port
vHost
heartbeat
sslEnabled
sslServerName
sslVersion
sslAcceptablePolicyErrors
sslCheckCertificateRevocation
batchPostingLimit
period
queueLimit
formatter
autoCreateExchange
maxChannels
levelSwitch
Parameters exchange
, exchangeType
, deliveryMode
, routeKey
provide additional configuration when connecting to RabbitMQ.
If autoCreateExchange
is true
, the sink will create the exchange if an exchange by that name doesn't exist.
Exchange is not created by default.
If sslEnabled
is true
, the sink will use secure connection to server.
By default, the server name is the same as the host name, no TLS version is specified, no server certificate errors are accepted,
and certificate revocation checking is disabled. You can change server name through by setting the sslServerName
, sslVersion
,
sslAcceptablePolicyErrors
, sslCheckCertificateRevocation
arguments.
This is a "periodic batching sink." The sink will queue a certain number of log events before they're actually written to RabbitMQ.
There is also a timeout period so that the batch is always written even if it has not been filled.
By default, the batch size is 50 rows and the timeout is 2 seconds.
You can change these through by setting the batchPostingLimit
and period
arguments.
Refer to the Formatter for details about the formatter arguments.
All sink features are configurable from code. Here is a typical example that works the same way for any .NET target.
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.RabbitMQ(
username: "usr",
password: "pwd",
hostnames: new[] { "localhost" },
port: 5672,
exchange = "LogExchange",
formatter: new JsonFormatter()
).CreateLogger();
Refer to the Serilog.Settings.AppSettings package documentation for complete details about sink configuration. This is an example of setting some of the configuration parameters for this sink.
<add key="serilog:using:RabbitMQ" value="Serilog.Sinks.RabbitMQ"/>
<add key="serilog:write-to:RabbitMQ.username" value="user"/>
<add key="serilog:write-to:RabbitMQ.password" value="pwd"/>
<add key="serilog:write-to:RabbitMQ.hostnames" value="server1,server2"/>
<add key="serilog:write-to:RabbitMQ.exchange" value="LogExchange"/>
<add key="serilog:write-to:RabbitMQ.batchPostingLimit" value="1000"/>
<add key="serilog:write-to:RabbitMQ.period" value="0.00:00:02.00"/>
Refer to the Serilog.Settings.Configuration package documentation for complete details about sink configuration. Keys and values are not case-sensitive. This is an example of configuring the sink arguments.
{
"Serilog": {
"Using": ["Serilog.Sinks.RabbitMQ"],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RabbitMQ",
"Args": {
"username": "usr",
"password": "pwd",
"hostnames": [
"localhost"
],
"port": 5672,
"exchange": "LogExchange",
"autoCreateExchange": true,
"batchPostingLimit": 1000,
"period": "0.00.00.02.00"
}
}
]
}
}
A Serilog audit sink writes log events which are of such importance that they must succeed, and that verification of a successful write is more
important than write performance. Unlike the regular sink, an audit sink does not fail silently - it can throw exceptions. You should wrap
audit logging output in a try/catch
block. The usual example is bank account withdrawal events - a bank would certainly not want to allow a
failure to record those transactions to fail silently.
The constructor accepts most of the same arguments, and like other Serilog audit sinks, you configure one by using AuditTo
instead of WriteTo
.
hostnames
username
password
exchange
exchangeType
deliveryMode
routeKey
port
vHost
heartbeat
sslEnabled
sslServerName
sslVersion
sslAcceptablePolicyErrors
sslCheckCertificateRevocation
formatter
autoCreateExchange
maxChannels
levelSwitch
The batchPostingLimit and period parameters are not available because the audit sink writes log events immediately.
{
"Serilog": {
"Using": ["Serilog.Sinks.RabbitMQ"],
"MinimumLevel": "Debug",
"AuditTo": [
{
"Name": "RabbitMQ",
"Args": {
"username": "usr",
"password": "pwd",
"hostnames": [
"localhost"
],
"port": 5672,
"exchange": "LogExchange",
"autoCreateExchange": true
}
}
]
}
}
The sink can be configured taking multiple host names.
To keep the Serilog.Setting.ApSettings external configuration, additional hosts are added to the hostnames
argument separated by commas.
This is an example of configuring the multihost using Serilog.Settings.AppSettings.
<add key="serilog:using:RabbitMQ" value="Serilog.Sinks.RabbitMQ"/>
<add key="serilog:write-to:RabbitMQ.hostnames" value="host1,host2"/>
<add key="serilog:write-to:RabbitMQ.username" value="user"/>
<add key="serilog:write-to:RabbitMQ.pasword" value="pwd"/>
ASP.NET has the possibility to encrypt connection string in the web.config.
There are multiple ways for configuring the RabbitMQSink with the release of v3.0.0
Log.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.GetSection("RABBITMQ_HOSTNAMES").Get<string[]>())
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);
Log.Logger = new LoggerConfiguration()
.WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) =>
{
clientConfiguration.From(config);
sinkConfiguration.TextFormatter = new JsonFormatter();
}) .CreateLogger();
// Or
Log.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() // if you are not assigning the logger to Log.Logger, then you need to add your logger here.
.AddConsole(LogLevel.Information);
services.AddSingleton<ILoggerFactory>(loggerFactory);