ArieGato / serilog-sinks-rabbitmq

Serilog Sink for RabbitMq
Apache License 2.0
54 stars 52 forks source link

how to use this project in my own project? #87

Closed abolfazlmohammadiseif closed 5 years ago

abolfazlmohammadiseif commented 5 years ago

Hi, Im going to use this project in my for example console App. I want to send logs to rabbitmq and receive in another App. what are the steps? Thanks

madslyng commented 5 years ago

@abolfazlmohammadiseif You'll need to setup a RabbitMQ instance. Then in your "console App", you add this library, and configure it to write logs to the RabbitMQ instance. In your "another App", you can use the official .NET RabbitMQ.Client library here: https://www.rabbitmq.com/dotnet.html

Good luck.

abolfazlmohammadiseif commented 5 years ago

Hi again, thanks for answering my question. In a .NetCoreWebApp, I've added the following code in Startup.cs , inside 'ConfigureServices' method.

var config = new RabbitMQClientConfiguration
            {
                Port = 5672,
                DeliveryMode = Serilog.Sinks.RabbitMQ.RabbitMQDeliveryMode.Durable,
                Exchange = "",
                Username = "guest",
                Password = "guest",
                ExchangeType = "fanout",
                Hostnames = { HostName },
            };

            var logger = new LoggerConfiguration()
                .WriteTo.RabbitMQ((clientConfiguration, sinkConfiguration) =>
                {
                    clientConfiguration.From(config);
                    sinkConfiguration.TextFormatter = new JsonFormatter();
                }).CreateLogger();

            var loggerFactory = new LoggerFactory();
            loggerFactory
              .AddSerilog()
              .AddConsole(LogLevel.Information);

            services.AddSingleton<ILoggerFactory>(loggerFactory);

then i've injected in the Home controller, the following:

        ILogger _logger;
        public HomeController(ILoggerFactory logger)
        {
            _logger = logger.CreateLogger(typeof(HomeController));
        }

then ive used like this: _logger.LogInformation("My Message"); Is there anything else I should do? I mean 1- where should I add 'QueueName' and 'connection' and 'channel'? 2- what is the configuration in Receiver App and the diffrence between the Sender App? Thanks

madslyng commented 5 years ago

@abolfazlmohammadiseif

  1. RabbitMQ has what is called an Exchange. I guess you are not intended to write directly to a queue, albeit you can. I suggest you read up on that subject, for instance here: https://www.cloudamqp.com/blog/2015-09-03-part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html

Serilog.Sinks.RabbitMq doesn't 'as such' support Queue, but again, you can still do it. Find an example of that in the Unit tests. https://github.com/sonicjolt/serilog-sinks-rabbitmq/search?q=QueueName&unscoped_q=QueueName

  1. That is two different mechanisms you seem to mixing together. Serilog.Sinks.RabbitMq is a logging-sink based on Serilogs core functionality, and extends it to support writing logs to RabbitMq. And it sounds like you also want to read messages from a RabbitMQ message queue. Well, for you to be able to do that, you will not need Serilog.Sinks.RabbitMq, but a different library that is not related to Serilog.Sinks.RabbitMq. I suggested that you look at RabbitMq.Client: