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

Integrate with ILogger{T} #4

Open
AKlaus opened this issue Sep 27, 2020 · 2 comments
Open

Integrate with ILogger{T} #4

AKlaus opened this issue Sep 27, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@AKlaus
Copy link
Owner

AKlaus commented Sep 27, 2020

Provide a way of logging non-OK responses via ILogger.
Have a way to automatically call ILogger.LogError() on returning NotFound, Failed, etc. Note: this option must be off by default, as the sad path of end-points execution usually leads to logging anyway.

Thought to add an option of manually logging on the call of ToActionResult (the ILogger needs to be added in the parameters), but that seems to be unnecessary. If there's a pressing need to log a custom message, a logger must be injected into the corresponding method of the service layer.

@AKlaus AKlaus added the enhancement New feature or request label Sep 27, 2020
@AKlaus AKlaus self-assigned this Sep 27, 2020
@AKlaus
Copy link
Owner Author

AKlaus commented Jul 11, 2022

In case of an invalid ModelState, the official recommendation for logging is via leveraging ApiBehaviorOptions and described in dotnet/AspNetCore.Docs/issues/12157.

With a sample like this:

services.AddControllers()
    .ConfigureApiBehaviorOptions(options =>
    {
        // To preserve the default behavior, capture the original delegate to call later.
        var builtInFactory = options.InvalidModelStateResponseFactory;

        options.InvalidModelStateResponseFactory = context =>
        {
            var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Startup>>();

            // Perform logging here.
            // ...

            // Invoke the default behavior, which produces a ValidationProblemDetails response.
            // To produce a custom response, return a different implementation of IActionResult instead.
            return builtInFactory(context);
        };
    });

Need to consider a similar approach for ToActionResult().

@AKlaus
Copy link
Owner Author

AKlaus commented Jul 11, 2022

Another option is to implement a ProblemDetailsFactory (docs).

As an example, check out an implementation of that factory from khellang.

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

No branches or pull requests

1 participant