serilog-archive / serilog-sinks-loggly

A Serilog event sink that writes to Loggly
Apache License 2.0
27 stars 30 forks source link

Configuration from appsettings.json #30

Closed ddshpak closed 4 years ago

ddshpak commented 6 years ago

I haven't been able to find a good way to do configuration-driven setup of the Loggly sink in a .NET Core app. It doesn't look like I can specify anything like

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Loggly",
        "Args": {
          "logglyConfig": {
            "CustomerToken": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "ApplicationName": "SerilogTest"
          }
        }
      }
    ]
  }
}

because that fails with System.InvalidOperationException: Argument value should be of type Action<>.. And in a .NET Core app, I don't have an app.config file, so I can't use loggly-csharp-configuration's XML-based configuration (as far as I know).

Am I missing something? Or is the current state of affairs such that I need to do programmatic configuration of Loggly in a .NET Core application?

MiguelAlho commented 6 years ago

Hi and sorry for the very delayed response. Yes, at the moment, the LogglyConfiguration object should be built/composed to pass that info in.

I saw that you have created a PR (https://github.com/serilog/serilog-sinks-loggly/pull/31) and will look into it.

MiguelAlho commented 6 years ago

Is https://github.com/serilog/serilog-settings-configuration a viable solution for this (instead of changing the public interface of the extension method)?

MiguelAlho commented 6 years ago

I've seen from https://github.com/serilog/serilog-settings-configuration/issues/58 that it's not yet possible to configure complex objects as args, so the list of args would need to be flattened.

ddshpak commented 6 years ago

Hi @MiguelAlho,

My understanding of https://github.com/serilog/serilog-settings-configuration is that it will take any settings it finds in the supplied JSON, and inject them as constructor parameters...but, as you point out, it doesn't support complex objects as arguments.

I didn't want to flatten the entire LogglyConfiguration object into simple-valued constructor arguments, so I took the approach of just adding the three that seemed the most important: the customer token (absolutely required), the tags (very valuable), and the endpoint (probably not needed, to be honest).

With these new arguments in place, https://github.com/serilog/serilog-settings-configuration does the rest of the work of actually parsing the JSON and passing in the correct arguments. The change to the public interface is backwards-compatible (the new arguments have default values).

I'm quite new to Serilog, so I might be missing something that should be obvious. If there's a better way to leverage the existing configuration code, I'd be happy to try to implement it; I might just need to be pointed in the right direction.

Thanks!

MiguelAlho commented 6 years ago

I tried building a sample using serilog-settings-configuration, but it unfortunately does not support complex objects. If it did, the sample you provided above would have been enough. It may be better to include support for the params, as you have in your PR.

skomis-mm commented 5 years ago

Hi, @dshpak-iq , @MiguelAlho. This should work with 3.0+ of the Serilog.Settings.Configuration package.

MiguelAlho commented 4 years ago

I've added a sample project in the features/jsonConfig branch that can be used to test things like this out. At the moment, Using the sample, I can send messages to loggly using just the appsettings.json file to configure the logger, though I'm not testing the durable logger just yet.

PR: https://github.com/serilog/serilog-sinks-loggly/pull/53 to put this in the dev branch