Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure from JSON, but then get a reference to the switch? #206

Closed
dazinator opened this issue Feb 28, 2020 · 11 comments · Fixed by #352
Closed

How to configure from JSON, but then get a reference to the switch? #206

dazinator opened this issue Feb 28, 2020 · 11 comments · Fixed by #352

Comments

@dazinator
Copy link

I'd like to use the json configuration to configure the seq sink. However I need access to the switch in code, because I want to allow this to be changed at runtime in code. How should I go about that?

@nblumhardt
Copy link
Member

Hi Darrell; this isn't possible right now, I think a solution would need to be baked into one of the Serilog.Settings.x packages, but we haven't got a workable API/proposal figured out (input and ideas welcome). I'll transfer this to the .Configuration repo, which is the more active one, currently.

@nblumhardt nblumhardt transferred this issue from datalust/serilog-sinks-seq Mar 1, 2020
@mrxrsd
Copy link

mrxrsd commented Jun 13, 2020

I was trying to do this just right now. I've tought it will be possible like we pass formatters to json using static variables.

It will be a great feature!

{
    "Serilog": {
        "LevelSwitches": { 
           "$controlSwitch": "Verbose",
           "exposeAt": "Sample.Switchlevel, Sample"
        },
        "WriteTo": [
            {
                "Name": "Seq",
                "Args": {
                    "serverUrl": "http://localhost:5341",
                    "apiKey": "yeEZyL3SMcxEKUijBjN",
                    "controlLevelSwitch": "$controlSwitch"
                }
            }
        ]
    }
}

@dazinator
Copy link
Author

dazinator commented Jun 14, 2020

@mrxrsd your proposal couples the json a bit too tightly to code structure for my level of comfort. Id prefer just naming the switch in the json, and then serilog exposing some dictionary at runtime to retrieve named switches. That way, refactoring code wouldn't break anything.

@tsimbalar
Copy link
Member

FYI, you should be able to use the "hack" proposed in this issue until there's an official way to do it :

serilog/serilog-settings-appsettings#24 (comment)

@skomis-mm
Copy link
Contributor

Would be great to add support for this . Without upstream support we could solve it with adding additional parameter or new overloads to ReadFrom.Configuration with switch collector type:

var switchCollector = new LoggerSwitchCollector();

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration, switchCollector)
    .CreateLogger();

var controlSwitch = switchCollector.GetLevelSwitch("controlSwitch");

@tsimbalar
Copy link
Member

By touching the core Seriloglibrary, we could also somehow expose the underlying LevelOverrideMap available in a Loggerbut this seems like leaking internals a bit too much...

@skomis-mm I like your suggestion, and wonder, if we could/should even make it a more generic way to "introspect" on the results of configuring through the ConfigurationSettings Provider.

So for instance :

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration, out var loadedConfiguration)
    .CreateLogger();

var switch = loadedConfiguration.GetLevelSwith("controlSwitch");

i.e. ReadFrom.Configurationtakes a second optional argument that returns some sort of object that contains information about the configuration that we loaded.

For now, it would just allow to look up switches, but we could extend it later to have, possibly, diagnostics about the process of loading the configuration or other useful information.

@skomis-mm
Copy link
Contributor

@tsimbalar some generic type was in my mind too. 👍

out modifier seems good addition. No need to import additional namespace for the type in case of inline initialization .

@gowon
Copy link

gowon commented Jul 24, 2020

I wanted to share my solution for this problem when I hit it a few months ago:

  • Create a dummy sink that takes the switch and a label
  • The LoggerSinkConfiguration extension method adds the dynamically generated switches and labels as key-value-pairs to a singleton collection that can be refererenced outside of the logger.
  • (optional) Add an extension to attach the singleton collection to the generic host DI

I wrapped it into a package here: https://github.com/gowon/Serilog.Sinks.DynamicSwitch. It feels a bit hacky, and I wish I could make it less verbose on the json side, but it gets the job done until there's some first-class interface for reaching those switches.

0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 5, 2023
0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 6, 2023
@0xced
Copy link
Member

0xced commented Feb 6, 2023

I just submitted #347 to address this issue. I am looking forward to your feedback over there.

0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 8, 2023
0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 8, 2023
0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 8, 2023
0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Feb 8, 2023
@mrxrsd
Copy link

mrxrsd commented Mar 4, 2023

I've been working in a solution using reflection, still a WIP but is already usable and publish.

https://github.com/mrxrsd/dynamic-serilog-switch

@0xced
Copy link
Member

0xced commented Mar 10, 2023

Pull request #352 is now up for review. 😀

0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Mar 10, 2023
0xced added a commit to 0xced/serilog-settings-configuration that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants