I am using AddHttpLogging middleware for my Dotnet 6 web api project. For diagnostic purposes I have such configuration:
var loggingFields = HttpLoggingFields.RequestPropertiesAndHeaders |
HttpLoggingFields.ResponsePropertiesAndHeaders |
HttpLoggingFields.ResponseStatusCode |
HttpLoggingFields.RequestQuery |
HttpLoggingFields.RequestBody;
Due to sensitive information, I want to prevent logging request body of some controllers. How can I them filter out?
In your case, use 'ExcludeFilter' method to prevent logging request body of some controllers, it is provided by the AddHttpLogging middleware. Like this;
app.UseAddHttpLogging(options => {
options.ExcludeFilter = context =>
context.ActionDescriptor.ControllerTypeInfo.FullName == "YourController";
});
Or another way is;
app.UseAddHttpLogging(options => {
options.ExcludeFilter = excludeFilterOptions => {
excludeFilterOptions.ControllerTypeNames.Add("YourController");
};
});
(Edited) Update: For some reason above solution didn't work. So I found another solution that worked. Here's a demo I coded for you; https://github.com/usama-mirza/HttpLoggingWithFilter
Try the following instead of app.UseHttpLogging();
app.UseWhen(
context => !context.Request.Path.StartsWithSegments("/Sensitive"), //SensitiveController.cs
builder => builder.UseHttpLogging());