Class HttpRequestEvidence
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assembly
- Cuemon.AspNetCore.dll
Provides detailed information about a given HttpRequest.
public class HttpRequestEvidence
- Inheritance
-
HttpRequestEvidence
Examples
HttpRequestEvidence captures HTTP request diagnostic data including headers, query parameters, form data, and the request body for logging and debugging scenarios. This example sets up a DefaultHttpContext with a POST request to https://api.example.com/orders, including an Authorization header, X-Trace-Id, a status=pending query string, form data with customerId=42, and a captured request body. Key steps include constructing an HttpRequestEvidence from the request and supplying a custom body converter delegate that redacts sensitive values by replacing "42" with "***". Console output displays each evidence property (location, method, headers, query, form, body, and redacted body).
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Cuemon.AspNetCore.Diagnostics;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNetCore.Http;
namespace MyApp.Diagnostics
{
public class HttpRequestEvidenceExample
{
public void Demonstrate()
{
var context = new DefaultHttpContext();
context.Request.Scheme = "https";
context.Request.Host = new HostString("api.example.com");
context.Request.Path = "/orders";
context.Request.QueryString = new QueryString("?status=pending");
context.Request.Method = HttpMethods.Post;
context.Request.ContentType = "application/x-www-form-urlencoded";
context.Request.Headers["Authorization"] = "Bearer eyJhbGci...";
context.Request.Headers["X-Trace-Id"] = "abc-123";
context.Request.Form = new FormCollection(
new Dictionary<string, StringValues>
{
{ "customerId", "42" }
});
// Capture the request body so HttpRequestEvidence can retrieve it
var bodyBytes = Encoding.UTF8.GetBytes("customerId=42");
context.Items[HttpRequestEvidence.HttpContextItemsKeyForCapturedRequestBody] =
new MemoryStream(bodyBytes);
var evidence = new HttpRequestEvidence(context.Request);
Console.WriteLine($"Location: {evidence.Location}");
Console.WriteLine($"Method: {evidence.Method}");
Console.WriteLine($"Auth Header: {evidence.Headers["Authorization"]}");
Console.WriteLine($"Query: status={evidence.Query["status"]}");
Console.WriteLine($"Form: customerId={evidence.Form["customerId"]}");
Console.WriteLine($"Body: {evidence.Body}");
// Provide a custom body converter to redact sensitive data
var redacted = new HttpRequestEvidence(context.Request,
stream => new StreamReader(stream).ReadToEnd().Replace("42", "***"));
Console.WriteLine($"Redacted Body: {redacted.Body}");
}}
}
Constructors
HttpRequestEvidence(HttpRequest, Func<Stream, string>)
Initializes a new instance of the HttpRequestEvidence class.
public HttpRequestEvidence(HttpRequest request, Func<Stream, string> bodyConverter = null)
Parameters
requestHttpRequestThe HttpRequest to provide evidence for.
bodyConverterFunc<Stream, string>The function delegate that determines the string result of a HTTP request body.
Fields
HttpContextItemsKeyForCapturedRequestBody
The key to set or get a copy of a captured request body.
public const string HttpContextItemsKeyForCapturedRequestBody = "CuemonAspNetCoreDiagnostics_HttpContextItemsKeyForCapturedRequestBody"
Field Value
Properties
Body
Gets the body of the request.
public string Body { get; }
Property Value
- string
The body of the request.
Cookies
Gets the collection of cookies for the request.
public IRequestCookieCollection Cookies { get; }
Property Value
- IRequestCookieCollection
The collection of cookies for the request.
Form
Gets the associated keys and values collection from the Form.
public IFormCollection Form { get; }
Property Value
- IFormCollection
The associated keys and values collection parsed from the Form.
Headers
Gets the request headers.
public IHeaderDictionary Headers { get; }
Property Value
- IHeaderDictionary
The headers of the request.
Location
Gets the request URL in a fully un-escaped form (except for the QueryString).
public string Location { get; }
Property Value
- string
The request URL in a fully un-escaped form (except for the QueryString).
Method
Gets the request HTTP method.
public string Method { get; }
Property Value
- string
The HTTP method.
Query
Gets the associated keys and values collection parsed from the QueryString.
public IQueryCollection Query { get; }
Property Value
- IQueryCollection
The associated keys and values collection parsed from the QueryString.