0

Is it possible to catch request/response bodies and add them into a telemetry initializer? or is this doable at all? I have been trying the below approach with no success, this is for a web application in .framework 4.6.1 after reading a bit about it, I found that NET 4.6.1 uses GetBufferlessInputStream() to get the stream, and this stream is "read once" so, by the time the code hits this initializer GetBufferlessInputStream() is already consumed and therefore the body is an empty string, I have seen other solutions but they are all for MVC .NET CORE and suggest to add code in the controller and right now it is not an option for me.

Edit for more context: this is a "rest-ish" based wcf webapp hosted in IIS that uses .net framework 4.6.1, we have a sepparated Monitoring project that implements App insights which is distributed as an internal nuget package for several projects, and here is where I would like to implement a new telemetry intializer (if possible) that would allow me to catch request bodies

Thank you all for you help.

public void Initialize(ITelemetry telemetry)
        {
            try
            {                
                    var requestTelemetry = telemetry as RequestTelemetry;
                    if (requestTelemetry != null && (HttpContext.Current.Request.HttpMethod == HttpMethod.Post.ToString() || HttpContext.Current.Request.HttpMethod == HttpMethod.Put.ToString()))
                    {
                        using (var reader = new StreamReader(HttpContext.Current.Request.GetBufferlessInputStream()))
                        {
                            string requestBody = reader.ReadToEnd();
                            requestTelemetry.Properties.Add("requestBody", requestBody);
                        }
                    }
                
            }
            catch (Exception ex)
            {
                Logger.Error($"Error initializing telemetry.{Environment.NewLine}{ex}");
            }
        }
  • 1
    I'm not sure if I misunderstood in some place but according to your sample code, it seems that you wanna azure AI to capture http request information. For azure app insights, there always 2 ways to integrate it to your project, [code-based](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net) and [codeless](https://learn.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net). Did you check if the default captured information can meet your requirement? See azure-portal->your ai instance->transaction search after enabling ai for your program. – Tiny Wang Jul 17 '21 at 14:11
  • 1
    If you are using OWIN, then you can write [middleware](https://stackoverflow.com/a/47550595/16343759) to capture this . If you want to target only specific actions, you can use [Action Filters](https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs) – ondrosk3 Jul 17 '21 at 20:22
  • thanks @tiny-wa I do want app insights to capture more details about my requests, in this case the body would be desired, we have already our AI implementation capturing requests, but they dont have much info included. – zandMonster Jul 19 '21 at 12:57
  • thanks @ondrosk3, but no, I am not using owin, this is a rest-ish based web app hosted in IIS that uses .net framework 4.6.1, we have a sepparated Monitoring project that implements App insights which is distributed as an internal nuget package, and here is where I would like to implement a new telemetry intializer (if possible) that would allow me to catch request bodies. – zandMonster Jul 19 '21 at 13:26

0 Answers0