HttpLoggingEventArgs Class
System.Net.Http
Provides data for the HttpPipeline.Logging event.
Syntax
public class HttpLoggingEventArgs : EventArgs
{
// Fields
public string Log { get; }
public HttpRequestMessage Request { get; }
public HttpResponseMessage Response { get; }
}
Members
Properties
Log Property
string
Gets the log message.
Remarks: This property contains the log message generated by the HTTP pipeline.
Request Property
Gets the HTTP request message.
Remarks: This property provides access to the original HTTP request message that was sent.
Response Property
Gets the HTTP response message.
Remarks: This property provides access to the HTTP response message received.
Requirements
| Interface | Implementation |
|---|---|
| Namespace | System.Net.Http |
| Assembly | System.Net.Http.dll |
Example
The following example shows how to handle the HttpPipeline.Logging event to log request and response details.
using System;
using System.Net.Http;
public class HttpLogger
{
public static void Main(string[] args)
{
var httpClient = new HttpClient();
// Subscribe to the logging event
HttpPipeline.Logging += HttpPipeline_Logging;
try
{
var response = httpClient.GetAsync("https://www.example.com").Result;
response.EnsureSuccessStatusCode();
var content = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Content fetched successfully.");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request exception: {e.Message}");
}
finally
{
// Unsubscribe from the event when done
HttpPipeline.Logging -= HttpPipeline_Logging;
}
}
private static void HttpPipeline_Logging(object sender, HttpLoggingEventArgs e)
{
Console.WriteLine("--- HTTP LOG START ---");
Console.WriteLine($"Log Message: {e.Log}");
Console.WriteLine($"Request Method: {e.Request.Method}");
Console.WriteLine($"Request URI: {e.Request.RequestUri}");
if (e.Response != null)
{
Console.WriteLine($"Response Status Code: {e.Response.StatusCode}");
Console.WriteLine($"Response Reason Phrase: {e.Response.ReasonPhrase}");
}
Console.WriteLine("--- HTTP LOG END ---");
}
}
// Dummy HttpPipeline class for demonstration purposes
public static class HttpPipeline
{
public static event EventHandler<HttpLoggingEventArgs> Logging;
internal static void Log(HttpRequestMessage request, HttpResponseMessage response, string logMessage)
{
Logging?.Invoke(null, new HttpLoggingEventArgs(request, response, logMessage));
}
}
// Dummy HttpLoggingEventArgs class for demonstration purposes
public class HttpLoggingEventArgs : EventArgs
{
public string Log { get; }
public HttpRequestMessage Request { get; }
public HttpResponseMessage Response { get; }
public HttpLoggingEventArgs(HttpRequestMessage request, HttpResponseMessage response, string logMessage)
{
Request = request;
Response = response;
Log = logMessage;
}
}
Note: The
HttpPipeline and HttpLoggingEventArgs classes used in the example are simplified for illustrative purposes. In a real-world scenario, these would be part of the .NET framework.