Skip to content

Populate IConfiguration or IConfigurationSection parameter #97

Closed
@MV10

Description

@MV10

Proposal

The package will automatically populate any IConfiguration or IConfigurationSection parameter on the target method.

The Issue

Any library being initialized by this package can only receive literals defined in the WriteTo entries. If the target supports more complex configuration than WriteTo provides (for example, currently arrays are not supported), the target has no means to access any configuration data when the target method is executed.

(This was not an issue under the .NET Framework in packages such as Serilog.Settings.AppSettings because the old ConfigurationManager approach is a static class, which means it is always available. Microsoft.Extensions.Configuration has no equivalent facility, it is the responsibility of the referencing library or application to provide access.)

In theory this issue could be avoided by simply not using this package at all, but I argue this limitation won't be obvious to Serilog users. For example, the MS SQL sink can add custom columns via a separate configuration section, so using this package is mutually exclusive with that feature. Each target package could explain any such limitations, but it seems easier in the long run to just provide a means to fix the problem in a way that is transparent to Serilog users.

If we make this package "smart" enough to populate IConfiguration or IConfigurationSection then, as dependent Serilog packages are updated to full Microsoft.Extensions.Configuration support, the problem basically fixes itself. (Probably the README for this package should provide advice to prefer IConfiguration over IConfigurationSection but that isolates RTFM caveats to the docs in this single repo.)

Implementation

Currently, the log config extension accepts either IConfiguration or IConfigurationSection, but IConfiguration simply retrieves the Serilog section and calls the section-based extension method. The section is passed into ConfigurationReader where all of the main processing occurs when the main Serilog package calls the ILoggerSettings.Configure method.

I propose to create two ctor overloads which also accept IConfiguration. They will extract the Serilog section and pass it along to the existing ctors. All ctors will store a local reference to IConfiguration (if available) and IConfigurationSection.

Finally, CallConfigurationMethods would be modified to check each MethodInfo for an IConfigurationSection parameter and an IConfiguration parameter (when available), then add these to the call list before invoking the method.

A pair of related tests should also be added, and possibly the sample could be updated.

"All ya gotta do is..."

And yes, I'm proposing to do the work myself, although any Reflection Ninjas in the house are more than welcome to handle it (I'm a little rusty on the specifics).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions