ArieGato / serilog-sinks-rabbitmq

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

Problem picking up config from appsettings.json #89

Closed HubertoKusters closed 5 years ago

HubertoKusters commented 5 years ago

Describe the bug Unable to overwrite the TextFormatter via appsettings.json

To Reproduce Add configuration in appsetting.json:

"WriteTo": [
    {
      "Name": "RabbitMQ",
      "Args": {
        "sinkConfiguration": {
          "TextFormatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
          "BatchPostingLimit": 300,
          "RestrictedToMinimumLevel": "Information"
        },
        "clientConfiguration": {
          "Hostnames": [
            "<hostname>"
          ],
          "Exchange": "log_events",
          "ExchangeType": "direct",
          "DeliveryMode": "Durable",
          "RouteKey": "logs",
          "Port": 5672,
          "Password": "<password>",
          "Username": "<user>"
        }
      }
    }
]

Expected behavior Use specified formatter

Operating System and Software

Additional context All attributes specified in both sinkConfiguration and clientConfiguration are used except for the TextFormatter attribute. It always reverts back to the default: RawFormatter If tried "Serilog.Formatting.Json.JsonFormatter, Serilog" as well, but it just does not work. It looks like you can only use an assembly reference as a parameter to the extension method (like in version 2.0.2) but not as a property in a class parameter.

I am not sure if I am missing something or if this is a bug.

HubertoKusters commented 5 years ago

@sonicjolt: I submitted a pullrequest with commit #90 to solve this. My solution is to add extra extension methods, which are also backward-compatible with previous configurations (2.0.2)

tsvetelintsonev commented 5 years ago

Hey, thank you for your PR. We have previously had issues with not picking serilog settings from appsettings.json We have provided a fix for that in the latest version. Ref: 79

HubertoKusters commented 5 years ago

The problem is not that the settings are not picked up. We cannot overrule the TextFormatter. For some reason you cannot provide a fully-qualified name string for an class instance if it is a property in a class. But you can do so as a parameter for the extension method. I am already using the latest version (NuGet 3.0.3) with the mentioned fix.

TomNieuwenhuis commented 5 years ago

Hi, Although picking up appsettings should be working, I cannot get it to work. I've even copied all mentioned settings from above, without any result. What I'm trying to do is to write to File and to RabbitMQ at the same time. My settings are as follows:

"Serilog": { "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.RabbitMQ" ], "MinimumLevel": "Debug", "WriteTo": [ { "Name": "RabbitMQ", "Args": { "sinkConfiguration": { "TextFormatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", "BatchPostingLimit": 300, "RestrictedToMinimumLevel": "Debug" }, "clientConfiguration": { "Hostnames": [ "localhost" ], "Exchange": "logsdirect", "ExchangeType": "direct", "DeliveryMode": "Durable", "RouteKey": "logging", "Port": 15672, "Password": "myPassword", "Username": "myUsername", } } }, { "Name": "File", "Args": { "path": "myLogFile.log", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}", "rollingInterval": "Day" } } ] }

Sending messages like so, from Startup.cs, via DI: _logger.LogInformation("this should be logged");

This line is coming in fine inside the rolling file, but not inside RabbitMQ.

I've tried changing several arguments, but to no avail.

Packages used: Serilog.Sinks.RabbitMQ 3.0.3 Serilog.Sinks.File 4.0.0 Serilog.Settings.Configuration 3.1.0 Serilog.Extensions.Logging 3.0.1 Serilog.AspNetCore 2.1.1 Serilog.Formatting.Compact 1.0.0

Am I missing something? Any help is appreciated!

Regards, Tom

HubertoKusters commented 5 years ago

@TomNieuwenhuis

I recommend that you verify that Serilog is picking everything up as expected and that there is no problem with you configuration:

#if DEBUG
            var logfile = File.CreateText(Path.Combine(Directory.GetCurrentDirectory(), "serilog.log"));
            Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(logfile));
#endif

If you run your application Serilog will produce the serilog.log file. Check its contents and report back.

TomNieuwenhuis commented 5 years ago

HI Huberto, Thanks for your very quick reply! I followed your instructions, and this is what I got:

2019-08-27T08:13:11.3617697Z Exception while emitting periodic batch from Serilog.Sinks.RabbitMQ.RabbitMQSink: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.IO.IOException: connection.start was never received, likely due to a network timeout at RabbitMQ.Client.Framing.Impl.Connection.StartAndTune() at RabbitMQ.Client.Framing.Impl.Connection.Open(Boolean insist) at RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(IFrameHandler fh) at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName) --- End of inner exception stack trace --- at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName) at Serilog.Sinks.RabbitMQ.RabbitMQClient.GetChannel() at Serilog.Sinks.RabbitMQ.RabbitMQSink.EmitBatch(IEnumerable1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(IEnumerable1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()

TomNieuwenhuis commented 5 years ago

(by the way, RabbitMQ is running fine, and accessible via the Management plugin on the specified port)

HubertoKusters commented 5 years ago

@TomNieuwenhuis

I think that the port is wrong. 15672 is for Management Page. You should use 5672 instead.

TomNieuwenhuis commented 5 years ago

doh! I thought RabbitMQ itself was running on 15672, had no idea that only the management page made use of 15672. Thank you so much! Regards, Tom

EvryJohan commented 5 years ago

Is there any prognosis on when we will get a new release with #90 incorporated?

madslyng commented 5 years ago

@EvryJohan Shouldn't be long now. Hopefully in the weekend.

HubertoKusters commented 5 years ago

@steffenlyng Any projections on when this is released to NuGet?

madslyng commented 5 years ago

@HubertoKusters @EvryJohan It should be available now. I merge an old PR from @huan086 that should fix some bootstrapping problem. Please let me know if I messed something up. I'm going to delete the development branch on the Github repo, because it causes more problems than anything else.