Skip to content

dwfrancis/serilog-sinks-rabbitmq

 
 

Repository files navigation

serilog-sinks-rabbitmq

Serilog Sink for RabbitMq

Installation

Using Nuget:

Install-Package Serilog.Sinks.RabbitMQ

Usage

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);
   }
   // ...
}

References

About

Serilog Sink for RabbitMq

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 83.3%
  • PowerShell 16.7%