ArieGato / serilog-sinks-rabbitmq

Serilog Sink for RabbitMq
Apache License 2.0
53 stars 51 forks source link

New simpler configuration api #69

Closed tsvetelintsonev closed 5 years ago

tsvetelintsonev commented 5 years ago

Usage examples:

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();
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();
var logger = new LoggerConfiguration()
    .WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) => {
        clientConfiguration.From(Configuration.Bind("RabbitMQClientConfiguration", new RabbitMQClientConfiguration()));
        sinkConfiguration.TextFormatter = new JsonFormatter();
}).CreateLogger();
tsvetelintsonev commented 5 years ago

All changes have been applied according to the comments.

madslyng commented 5 years ago

Could you update the instructions for this (readme.md), .. I'm writing a test-host application for a docker-compose setup, and instructions are unclear how to populate Hostnames ;-)

tsvetelintsonev commented 5 years ago

I just updated the usage examples. Please say if there is something else you need to know regarding the api usage :-)

madslyng commented 5 years ago

With multiple hosts, is it unrealistic that the port-numbers will change ? (i guess so since they are meant for failover etc,, right? :)

tsvetelintsonev commented 5 years ago

I will look into the docs whether it is possible to register multiple Hostnames with different ports. This makes me thinking about different messaging protocols per Hostname. Should we look at this as well?

madslyng commented 5 years ago

Let's wait for the feature request to come in instead, and wrap this milestone up

tsvetelintsonev commented 5 years ago

Ok, I will only take a look at ports support for this PR.

madslyng commented 5 years ago

I think we should just keep it like this -- if it arises as a feature request, it will come in fairly quickly as a request.. I'll merge this PR.