Created
June 29, 2020 00:55
-
-
Save smaglio81/8ebcbb63021ebee2427c96b9ddcd4bf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.ApplicationInsights; | |
using Microsoft.ApplicationInsights.DataContracts; | |
using Microsoft.AspNetCore.Mvc.Filters; | |
namespace YourNamespace | |
{ | |
public class TrackConstants | |
{ | |
public const string JsonBody = "JsonBody"; | |
} | |
public class TrackRequestBodyAttribute : ActionFilterAttribute | |
{ | |
private readonly TelemetryClient _telemtry; | |
public TrackRequestBodyAttribute( | |
TelemetryClient telemetry = null | |
) | |
{ | |
_telemtry = telemetry; | |
} | |
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | |
{ | |
var request = context.HttpContext.Request; | |
request.Body.Position = 0; | |
using var stream = new StreamReader(request.Body, Encoding.UTF8, false, 1024, true); | |
var body = await stream.ReadToEndAsync(); | |
request.Body.Position = 0; | |
context.HttpContext.Items.Add(TrackConstants.JsonBody, body); | |
var toperation = _telemtry.StartNewOperation<DependencyTelemetry>("JsonBody"); | |
toperation.TData.Add(TrackConstants.JsonBody, body); | |
toperation.Stop(); | |
await base.OnActionExecutionAsync(context, next); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment