Azureblue.ApplicationInsights.RequestLogging 2.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Azureblue.ApplicationInsights.RequestLogging --version 2.2.1                
NuGet\Install-Package Azureblue.ApplicationInsights.RequestLogging -Version 2.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Azureblue.ApplicationInsights.RequestLogging" Version="2.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Azureblue.ApplicationInsights.RequestLogging --version 2.2.1                
#r "nuget: Azureblue.ApplicationInsights.RequestLogging, 2.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Azureblue.ApplicationInsights.RequestLogging as a Cake Addin
#addin nuget:?package=Azureblue.ApplicationInsights.RequestLogging&version=2.2.1

// Install Azureblue.ApplicationInsights.RequestLogging as a Cake Tool
#tool nuget:?package=Azureblue.ApplicationInsights.RequestLogging&version=2.2.1                

Extended request logging with Application Insights

Introduction

This nuget package provides a custom middleware that allows to write the body of an HTTP request/response to a custom dimension.

alternate text is missing from this package README image

Features

  • Log request & response body to Application Insights
  • Configure HTTP verbs that will trigger logging
  • Configure HTTP status code ranges that will trigger logging
  • Configure maximum body length to store
  • Provide optional cut off text
  • Configure name of custom dimension keys
  • Disable IP masking without the need to modify the App Insights resource as described here

A word of warning! Writing the content of an HTTP body to Application Insights might reveal sensitive user information that otherwise would be hidden and protected in transfer via TLS. So use this with care and only during debugging or developing!

Installation

Just pull in the nuget package like so:

dotnet add package Azureblue.ApplicationInsights.RequestLogging

Then you'll have to register the middleware in your Startup class with your container.

using Azureblue.ApplicationInsights.RequestLogging;

// ...

public void ConfigureServices(IServiceCollection services)
{
    // ...

    // Register App Insights 
    services.AddApplicationInsightsTelemetry();
    
    // Register this middleware
    services.AddAppInsightsHttpBodyLogging();

    // ...
}

Finally configure the request pipeline. Make sure the call to UseAppInsightsHttpBodyLogging happens as early as possible as the order matters. Have a look at this issue

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

Configuration

You can overwrite the default settings as follows...

services.AddAppInsightsHttpBodyLogging(o => {
    o.HttpCodes.Add(StatusCodes.Status200OK);
    o.HttpVerbs.Add(HttpMethods.Get);
    o.MaxBytes = 10000;
    o.Appendix = "\nSNIP";
    o.DisableIpMasking = true;
});

...or stick with the defaults which are defined in BodyLoggerOptions.

BodyLoggerOptions

public class BodyLoggerOptions
{
    public BodyLoggerOptions()
    {
        HttpCodes.AddRange(StatusCodeRanges.Status4xx);
        HttpCodes.AddRange(StatusCodeRanges.Status5xx);
    }

    /// <summary>
    ///     Only write to App Insights on these HTTP status codes
    /// </summary>
    public List<int> HttpCodes { get; set; } = new List<int>();

    /// <summary>
    ///     Only these HTTP verbs will trigger logging
    /// </summary>
    public List<string> HttpVerbs { get; set; } = new List<string>()
    {
        HttpMethods.Post, 
        HttpMethods.Put,
        HttpMethods.Patch
    };

    /// <summary>
    ///     Which property key should be used
    /// </summary>
    public string RequestBodyPropertyKey { get; set; } = "RequestBody";

    /// <summary>
    ///     Which property key should be used
    /// </summary>
    public string ResponseBodyPropertyKey { get; set; } = "ResponseBody";

    /// <summary>
    ///     Which property key should be used
    /// </summary>
    public string ClientIpPropertyKey { get; set; } = "ClientIp";
    
    /// <summary>
    ///     Defines the amount of bytes that should be read from HTTP context
    /// </summary>
    public int MaxBytes { get; set; } = 80000;

    /// <summary>
    ///     Defines the text to append in case the body should be truncated <seealso cref="MaxBytes"/>
    /// </summary>
    public string Appendix { get; set; } = "\n---8<------------------------\nTRUNCATED DUE TO MAXBYTES LIMIT";

    /// <summary>
    ///     Controls storage of client IP addresses https://learn.microsoft.com/en-us/azure/azure-monitor/app/ip-collection?tabs=net
    /// </summary>
    public bool DisableIpMasking { get; set; } = false;
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.1 49,389 4/24/2024
2.3.0 139 4/23/2024
2.2.1 4,832 3/26/2024
2.2.0 4,736 2/20/2024
2.1.1 205,205 10/28/2022
2.1.0 1,720 10/13/2022
2.0.2 3,592 10/4/2022
2.0.1 73,764 12/9/2021
2.0.0 2,445 11/19/2021
1.0.2 745 10/28/2021
1.0.1 411 10/28/2021
1.0.0 372 10/28/2021