HttpRequestEvidence Class
Definition
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assemblies
- Cuemon.AspNetCore.dll
Provides detailed information about a given HttpRequest.
public class HttpRequestEvidence
- Inheritance
-
objectHttpRequestEvidence
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
| Name | Description |
|---|---|
| HttpRequestEvidence(HttpRequest, Func<Stream, string>) | Initializes a new instance of the HttpRequestEvidence class. |
Fields
| Name | Description |
|---|---|
| HttpContextItemsKeyForCapturedRequestBody | The key to set or get a copy of a captured request body. |
Properties
| Name | Description |
|---|---|
| Body | Gets the body of the request. |
| Cookies | Gets the collection of cookies for the request. |
| Form | Gets the associated keys and values collection from the Form. |
| Headers | Gets the request headers. |
| Location | Gets the request URL in a fully un-escaped form (except for the QueryString). |
| Method | Gets the request HTTP method. |
| Query | Gets the associated keys and values collection parsed from the QueryString. |