Similar question like this: I'm using HttpRequestLogging like this:
builder.Services.AddHttpLogging(loggingOptions =>
{
loggingOptions.LoggingFields = HttpLoggingFields.Request | HttpLoggingFields.Response;
}
However, for certain request paths, I would like to change the logging fields to not include logging the body, basically switch to
LoggingFields = HttpLoggingFields.RequestProperties | HttpLoggingFields.ResponsePropertiesAndHeaders;
if the request path contains /myspecial/request/notToLog
Is that possible?
There are at least 3 ways which can help you to achieve that (based on actual case/setup):
Use endpoint-specific configuration via attributes (if you want to customize specific endpoints), works both for controllers and minimal APIs endpoints:
Use
IHttpLoggingInterceptor:In some cases it can be appropriate to conditionally enable the logging with middleware branching (effectively disabling it for the rest):