Skip to content

Allow RequestLoggingOptions configuration outside of Configure(IApplicationBuilder) #197

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

Merged
merged 1 commit into from
Jun 18, 2020

Conversation

skomis-mm
Copy link

It is a common pattern to configure middleware options outside of Configure(IApplicationBuilder). It could be a different module, for example. It also integrates with Microsoft.Extensions.Options. This PR will allow such configuration:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RequestLoggingOptions>(Configuration.GetSection("Serilog")); // get MessageTemplate from appsettings.json
    services.PostConfigure<RequestLoggingOptions>(options =>
    {
        options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
        {
            diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.MapToIPv4());
        };
    });
}

public void Configure(IApplicationBuilder app)
{
    // ..
    app.UseSerilogRequestLogging();
    app.UseRouting();
    // ...
}

Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Ready to merge?

@skomis-mm
Copy link
Author

@nblumhardt , yes, its pretty complete

@nblumhardt nblumhardt merged commit 1b8b9f5 into serilog:dev Jun 18, 2020
/// <summary>
/// The logger through which request completion events will be logged. The default is to use the
/// static <see cref="Log"/> class.
/// </summary>
public ILogger Logger { get; set; }

internal RequestLoggingOptions() { }
/// <summary>
/// Constructor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

@@ -70,12 +62,9 @@ public static class SerilogApplicationBuilderExtensions
Action<RequestLoggingOptions> configureOptions = null)
Copy link
Contributor

@sungam3r sungam3r Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter seems (a bit) redundant now.

@nblumhardt nblumhardt mentioned this pull request Jul 24, 2020
@skomis-mm skomis-mm deleted the configureOptions branch July 24, 2020 07:27
@mkgn
Copy link

mkgn commented Dec 29, 2020

Can RequestLoggingOptions() be enabled/disabled via appSettings.json file under Serilog settings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants