-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Make Http Logging Middleware Endpoint aware #43222
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
Comments
Would there be a functional difference between EnableLoggingAttribute(None) and DisableLoggingAttribute? |
Hmm, No! |
Triage: We'll consider this for 8, endpoint-awareness in general is goodness for Middlewares. |
For debugging, I need to temporarily enable logging for only one endpoint. |
Something like rote pattern would be nice to have. builder.Services.AddHttpLogging(options =>
{
options.LoggingFields = HttpLoggingFields.All;
options.Disable.Add("/swagger*");
}); |
The point of using endpoints would be to add the enable/disable metadata directly to them and avoid re-implementing routing locally. |
It is possible to use UseWhen to selectively add http logging to only some endpoints, like so:
|
Yes |
RequestBodyLogLimit and ResponseBodyLogLimit would be easy options to include. Request/ResponseHeaders and MediaTypeOptions wouldn't be feasible to expose directly on an attribute. If we wanted to do those we'd need to shift to a named policy model. Those settings are far less endpoint specific so we might not need them. Compare to #45732 |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review Notes:
We can revisit this when we have answers to these questions and the people working on the feature are available for API review. |
Ah, the definition above is missing the EnableLoggingAttribute constructor. The constructor should be required as to avoid this ambiguity. fixed
Precedence:
These do not combine; you only get the results from the first one that's available.
I think the approved API is fine.
The func returns null when it doesn't want to make a decision, it falls back to the attribute and then options. Implementing the attribute check inside the default func seems unnecessary from a code hygiene perspective. At most, the property itself might be non-nullable with a default |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API review notes:
API Approved! namespace Microsoft.AspNetCore.HttpLogging;
+ public sealed class HttpLoggingAttribute : Attribute
+{
+ public HttpLoggingAttribute(HttpLoggingFields loggingFields, int? requestBodyLogLimit = null, int? responseBodyLogLimit = null) { }
+ public HttpLoggingFields LoggingFields { get; }
+ public int? RequestBodyLogLimit { get; }
+ public int? ResponseBodyLogLimit { get; }
+}
namespace Microsoft.AspNetCore.Builder;
+public static class HttpLoggingEndpointConventionBuilderExtensions
+{
+ public static TBuilder WithHttpLogging<TBuilder>(this TBuilder builder, HttpLoggingFields loggingFields, int? requestBodyLogLimit = null, int? responseBodyLogLimit = null) where TBuilder : IEndpointConventionBuilder;
+} |
In order to compile, a small modification to the approved API is needed because generics ( + public sealed class HttpLoggingAttribute : Attribute
+{
- public HttpLoggingAttribute(HttpLoggingFields loggingFields, int? requestBodyLogLimit = null, int? responseBodyLogLimit = null) { }
+ public HttpLoggingAttribute(HttpLoggingFields loggingFields, int requestBodyLogLimit = -1, int responseBodyLogLimit = -1) { }
+ public HttpLoggingFields LoggingFields { get; }
+ public int RequestBodyLogLimit { get; }
+ public int ResponseBodyLogLimit { get; }
+}
namespace Microsoft.AspNetCore.Builder;
+public static class HttpLoggingEndpointConventionBuilderExtensions
+{
+ public static TBuilder WithHttpLogging<TBuilder>(this TBuilder builder, HttpLoggingFields loggingFields, int requestBodyLogLimit = -1, int responseBodyLogLimit = -1) where TBuilder : IEndpointConventionBuilder;
+} |
Thoughts about the inconsistent parameter types between the attribute constructor, properties, and the extension method? Is that going to be confusing for people? |
Assuming I write decent API docs I don't think it's a big issue. |
I think it might be better to make the properties non-nullable too for consistency. We're going to have to explain what |
Should |
I think that makes sense for consistency. Keeps the docs the same for everything, and I figure most people aren't going to write out |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review Notes:
API Approved with IsSet properties! namespace Microsoft.AspNetCore.HttpLogging;
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
+ public sealed class HttpLoggingAttribute : Attribute
+ {
+ public HttpLoggingAttribute(HttpLoggingFields loggingFields) { }
+ public HttpLoggingFields LoggingFields { get; }
+ public bool IsRequestBodyLogLimitSet { get; }
+ public int RequestBodyLogLimit { get; set; }
+ public bool IsResponseBodyLogLimitSet { get; }
+ public int ResponseBodyLogLimit { get; set; }
+ }
namespace Microsoft.AspNetCore.Builder;
+ public static class HttpLoggingEndpointConventionBuilderExtensions
+ {
+ public static TBuilder WithHttpLogging<TBuilder>(this TBuilder builder, HttpLoggingFields loggingFields, int? requestBodyLogLimit = null, int? responseBodyLogLimit = null) where TBuilder : IEndpointConventionBuilder;
+ } |
Background and Motivation
I need to log the body of every requests that are coming to the application, but I want to exclude some endpoints. For example I don't want to log the body of the login methods. I suggest adding metadata for enabling and disabling log for http logging middleware.
The new behavior would be like these: if endpoint has
DisableLoggingAttribute
the middleware would be skipped completely. whenEnableLoggingAttribute
is available the middleware use itsLoggingFields
to log the request/response and if there are no metadata available, it would useHttpLoggingOptions.LoggingFields
.Also should other
HttpLoggingOptions
properties be available in the metadata or is logging fields enough?Proposed API
Usage Examples
Alternative Designs
Risks
The text was updated successfully, but these errors were encountered: