HttpLoggingEventArgs Class
Represents the arguments for an event that is raised when an HTTP request or response is logged.
This class provides information about the logged HTTP message, including its request and response details.
Namespace: System.Net.Http
Assembly: System.Net.Http.dll
Syntax
public class HttpLoggingEventArgs : EventArgs
Remarks
The HttpLoggingEventArgs class is used in conjunction with the HttpLoggingHandler class. When a handler intercepts an HTTP request or response, it can raise an event, passing an instance of HttpLoggingEventArgs to the event subscribers. This allows developers to inspect and potentially log details of the HTTP traffic.
The event is typically raised for both outgoing requests and incoming responses, providing a comprehensive view of the communication between the client and the server.
Properties
| Property | Description |
|---|---|
Request |
Gets the HttpRequestMessage that was sent. |
Response |
Gets the HttpResponseMessage that was received. This property might be null if the event is raised for a request before a response is received. |
LogKind |
Gets the type of logging event. For example, whether it's for a request or a response. |
Example
The following example demonstrates how to use HttpLoggingHandler and HttpLoggingEventArgs to log HTTP request and response details.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class HttpLogger
{
public static async Task Main(string[] args)
{
var httpClient = new HttpClient();
var loggingHandler = new HttpLoggingHandler();
// Subscribe to the logging event
loggingHandler.LogMessage += LogHttpRequestAndResponse;
// Create a message handler chain
// The loggingHandler is placed before the actual HttpClientHandler
var httpClientWithLogging = new HttpClient(loggingHandler);
try
{
Console.WriteLine("Sending request...");
var response = await httpClientWithLogging.GetStringAsync("https://www.example.com");
Console.WriteLine("Request completed.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
private static void LogHttpRequestAndResponse(object sender, HttpLoggingEventArgs e)
{
Console.WriteLine($"--- HTTP Log Event ({e.LogKind}) ---");
if (e.Request != null)
{
Console.WriteLine($"Request URL: {e.Request.RequestUri}");
Console.WriteLine($"Request Method: {e.Request.Method}");
if (e.Request.Headers.Count > 0)
{
Console.WriteLine("Request Headers:");
foreach (var header in e.Request.Headers)
{
Console.WriteLine($" {header.Key}: {string.Join(",", header.Value)}");
}
}
}
if (e.Response != null)
{
Console.WriteLine($"Response Status Code: {e.Response.StatusCode}");
if (e.Response.Headers.Count > 0)
{
Console.WriteLine("Response Headers:");
foreach (var header in e.Response.Headers)
{
Console.WriteLine($" {header.Key}: {string.Join(",", header.Value)}");
}
}
}
Console.WriteLine("------------------------------------");
}
}
// Dummy HttpLoggingHandler for demonstration if not provided by a specific framework
namespace System.Net.Http
{
public class HttpLoggingHandler : DelegatingHandler
{
public event EventHandler LogMessage;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
OnLogMessage(new HttpLoggingEventArgs { Request = request, LogKind = "Request" });
var response = await base.SendAsync(request, cancellationToken);
OnLogMessage(new HttpLoggingEventArgs { Request = request, Response = response, LogKind = "Response" });
return response;
}
protected virtual void OnLogMessage(HttpLoggingEventArgs e)
{
LogMessage?.Invoke(this, e);
}
}
public class HttpLoggingEventArgs : EventArgs
{
public HttpRequestMessage Request { get; set; }
public HttpResponseMessage Response { get; set; }
public string LogKind { get; set; } // e.g., "Request", "Response"
}
}
This example shows how to attach a handler to the LogMessage event. The event handler then inspects the HttpLoggingEventArgs to extract and display information about the request and response.