Skip to content

Make Http Logging Middleware Endpoint aware #43222

@Kahbazi

Description

@Kahbazi

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. when EnableLoggingAttribute is available the middleware use its LoggingFields to log the request/response and if there are no metadata available, it would use HttpLoggingOptions.LoggingFields.

Also should other HttpLoggingOptions properties be available in the metadata or is logging fields enough?

Proposed API

namespace Microsoft.AspNetCore.HttpLogging;

+ public class EnableLoggingAttribute : Attribute
+{
+    public EnableLoggingAttribute(HttpLoggingFields fields) { }
+    public HttpLoggingFields LoggingFields { get; }
+}

+ public class DisableLoggingAttribute : Attribute
+{
+}

namespace Microsoft.AspNetCore.Builder;

+public static class HttpLoggingEndpointConventionBuilderExtensions
+{
+    public static TBuilder EnableLogging<TBuilder>(this TBuilder builder, HttpLoggingFields loggingFields) where TBuilder : IEndpointConventionBuilder;
+    public static TBuilder DisableLogging<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder;
+}

Usage Examples

app.MapPost("/login", ... ).DisableLogging();
app.MapPost("/uploadFile", ... ).EnableLogging(HttpLoggingFields.RequestPropertiesAndHeaders);

Alternative Designs

Risks

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions