question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConfigurationMethod selector dependent on definition order

See original GitHub issue

Issue As of now ConfigurationMethod selector is vaguely dependent on definition order of the configuration methods. Consider the following configuration class:

MyLoggerConfiguration.cs

static class MyLoggerConfiguration
    {
        public static LoggerConfiguration DemoLoggerConfiguration(
            LoggerSinkConfiguration loggerSinkConfiguration,
            IEnumerable<string> pathFormat)
        {
            return null;
        }

        public static LoggerConfiguration DemoLoggerConfiguration(
            LoggerSinkConfiguration loggerSinkConfiguration,
            string pathFormat)
        {
            return null;
        }

        public static LoggerConfiguration DemoLoggerConfiguration(
            LoggerSinkConfiguration loggerSinkConfiguration,
            bool usesPathFormat)
        {
            return null;
        }
    }

appsettings.json

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "MyLogger",
        "Args": {
          "pathFormat": "test",        
        }
      }
    ]
  }
}

Current Behavior Currently the code will try to get one of the 3 DemoLoggerConfiguration methods. As of now it will select the two DemoLoggerConfiguration instances with the matching parameter name pathFormat - however it will use the .First() or the top one and try to invoke it and causing an TypeCastException to IEnumerable<string> from string. The problem being that it ignored the other method which was a valid candidate.

Temporary Workaround As of now we can reorder the methods so the method with string pathFormat appears before the other method that has it as IEnumerable<string> but it is easy to miss and not a very elegant approach since the order shouldn’t matter.

Expected Behavior The configuration selector should’ve picked and invoked the 2nd DemoLoggerConfiguration which defined the parameter as a string since it is the most straightforward choice, irrespective of the position where it’s defined.

PR https://github.com/serilog/serilog-settings-configuration/pull/132

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MV10commented, Sep 16, 2018

I suppose the general argument is that if a matching string argument exists, that’s going to be the best choice given that all config values are string values first and foremost.

There is a conflict with #128 but I’ll merge your PR once that is resolved.

0reactions
nbazteccommented, Sep 11, 2018

Yes indeed, that was my thought process as well. It was encountered while I was patching a bug in the Udp sink package and the maintainer expressed that it was an easy to miss error which they frequently encountered themselves.

Basically, in true configuration-by-code manner, the first and original method accepted IPAddress and a secondary overload, to read JSON configuration and accept hostnames + IPs, would accept string. The string value would then be parsed to an IPAddress within the function body or remain a hostname. However due to the ordering, the value "localhost" was always being tried to be cast as an instance of IPAddress.

While one can argue that they could’ve used separate names for IP address and hostname parameters, that however would’ve been a workaround if the key is simply stated as "remoteHost".

Read more comments on GitHub >

github_iconTop Results From Across the Web

Order type and dependent parameters
Create Order Type dependent parameters, TC: OPL8, here you maintain parameters for routing selection, operation & BOM Application.
Read more >
Rearrange code | IntelliJ IDEA Documentation
For the Keep dependent methods together option, you can select depth-first order or breadth-first order. The former will arrange the methods ...
Read more >
Documentation
Lets you specify individual methods to run. -methodselectors, A comma-separated list of Java classes and method priorities that define method selectors. Lets ...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
The specificity algorithm calculates the weight of a CSS selector to determine ... Order of appearance becomes relevant when the selector ...
Read more >
5 Selectors
A simple selector is either a type selector or universal selector followed immediately by zero or more attribute selectors, ID selectors, or pseudo-classes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found