matthiasguentert/azure-appinsights-logger

Add "StatusCode" filter to ResponseLoggerOptions

Closed this issue · 4 comments

I'd like to only log errors to lighten the amount of data written to my app insights account.

I'd be happy to make a pull request if you think this would be a valuable addition.

Hi Christian

I have started a major clean-up of the code basis to make it more testable, and also to implement the desired functionality. You can find the code in the branch feature/conditional_logging.

Also, I got rid of the path filter feature, as I think people will more likely use it the way you've suggested and in case an error occurred. See current options in BodyLoggerOptions.cs

You can use the middleware as shown below. Please let me know what you think.

public void ConfigureServices(IServiceCollection services)
{
	...
	
	services.AddApplicationInsightsTelemetry(Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
	services.AddAppInsightsHttpBodyLogging(o =>
	{
		o.MaxBytes = 50000;
		o.HttpCodes = new List<int>
		{
			StatusCodes.Status200OK,
			StatusCodes.Status400BadRequest,
			StatusCodes.Status500InternalServerError
		};
	});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
	if (env.IsDevelopment())
	{
		app.UseDeveloperExceptionPage();
		app.UseAppInsightsHttpBodyLogging();
	}
	
	... 
}

First of all really appreciate all the work you're putting into this, I think it's going to be very useful.

Here are some of my thoughts:

  1. From a user's perspective I'd want an easy way to target a range of status codes rather than have to define them so granularly. For me personally I'd want to target all errors (400 and 500 level status codes)
  2. Nitpick but it'd be nice if HttpVerbs were strongly typed. Like maybe instead of a string it takes in an HttpMethods
  3. Defaults for HttpVerbs should probably include "PATCH" as well
  4. My preference on defaults for HttpCodes is to include all errors (400 and 500 level status codes)

Hi Christian, thanks again for your valuable feedback & praise 😃

I have just released a new version 2.0.0 of the nuget package that incorporates the changes. You might also want to have a look at the updated README.

Happy coding!

Awesome!! Can't wait to try it out