Skip to content
cgcel edited this page Sep 11, 2024 · 3 revisions

Catalog

Description

It's necessary to create an ILogger when initializing RabbitHelper.

Basic Usage

For example, when using RabbitHelper directly, follow these codes to initialize:

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});

var logger = loggerFactory.CreateLogger("RabbitHelper");

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Producers = new List<ProducerOptions> { 
        new ProducerOptions {
            ProducerName = "FooProducer",
            ExchangeName = "amq.topic",
            RoutingKey = "foo.key"
        }
    }
}, logger);

If you don't want to use a logger, you could create a NullLogger instead:

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});

var logger = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Producers = new List<ProducerOptions> { 
        new ProducerOptions {
            ProducerName = "FooProducer",
            ExchangeName = "amq.topic",
            RoutingKey = "foo.key"
        }
    }
}, logger);

Dependency Injection

When using RabbitHelper by dependency injection functions, you should inject a global logger first, if you call AddRabbitHelper() directly, NanoRabbit will create a NullLogger automatically.

For example:

builder.Services.AddRabbitHelper(builder =>
{
    builder.SetHostName("localhost")
        .SetPort(5672)
        .SetVirtualHost("/")
        .SetUserName("admin")
        .SetPassword("admin")
        .AddProducerOption(producer =>
        {
            producer.ProducerName = "FooProducer";
            producer.ExchangeName = "amq.topic";
            producer.RoutingKey = "foo.key";
            producer.Type = ExchangeType.Topic;
        })
});

If you have injected an Logger, just get the logger and pass it into AddRabbitHelper():

services.AddLogging(loggingBuilder =>
{
    // configure Logging with NLog
    loggingBuilder.ClearProviders();
    loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
    loggingBuilder.AddNLog(context.Configuration);
}).BuildServiceProvider();

services.AddRabbitHelper(builder =>
{
    builder.SetHostName("localhost")
        .SetPort(5672)
        .SetVirtualHost("/")
        .SetUserName("admin")
        .SetPassword("admin")
        .AddProducerOption(producer =>
        {
            producer.ProducerName = "FooProducer";
            producer.ExchangeName = "amq.topic";
            producer.RoutingKey = "foo.key";
            producer.Type = ExchangeType.Topic;
        });
}, loggerFactory: serviceCollection =>
{
    var serviceProvider = serviceCollection.BuildServiceProvider();
    var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
    return logger;
});
Clone this wiki locally