Generate detailed Audit Logs for HttpClient instances.
$ dotnet add package Audit.HttpClientHttpClient audit extension for Audit.NET library.
Generate Audit Logs by intercepting HttpClient REST calls.
Audit.HttpClient provides the infrastructure to create audit logs for an instance of HttpClient class.
It relies on a message handler to incercept the calls to HttpClient methods.
NuGet Package
To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.HttpClient
To enable the audit log for HttpClient, you have to set an AuditHttpClientHandler as a message handler for the HttpClient instance being audited.
This can be done in different ways:
Audit.Http.ClientFactory.Create() method to get a new audit-enabled instance of HttpClient:var httpClient = Audit.Http.ClientFactory.Create(_ => _
.IncludeRequestBody()
.IncludeResponseHeaders()
.FilterByRequest(req => req.Method.Method == "GET"));
The
ClientFactory.Createmethod is just a shortcut to create a newHttpClientwith a customAuditHttpClientHandleras its message handler.
HttpClientFactory, you can add the message handler with the extension method on your startup:AddAuditHandler()using Audit.Http;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("GitHub", c =>
{
c.BaseAddress = new Uri("https://api.github.com/");
})
.AddAuditHandler(audit => audit
.IncludeRequestBody()
.IncludeResponseHeaders());
}
}Note:
AddAuditHandler(config)is a shortcut forAddHttpMessageHandler(() => new AuditHttpClientHandler(config))
HttpClient passing the handler to its constructor:var httpClient = new HttpClient(new AuditHttpClientHandler(_ => _
.IncludeRequestBody()
.IncludeResponseHeaders());Each method call on the audited HttpClient instances will generate an Audit Event.
The audit events are stored using a Data Provider. You can use one of the available data providers or implement your own. Please refer to the data providers section on Audit.NET documentation.
The AuditHttpClientHandler class allows to configure the following settings:
AuditScopeFactory is used.The following table describes the Audit.HttpClient output fields:
Describes an operation call event
| Field Name | Type | Description |
|---|---|---|
| Method | string | HTTP rest method |
| Url | string | Request URL |
| Version | string | Http client version |
| Exception | string | Exception details when an exception is thrown |
| Request | Request | Request audit information |
| Response | Response | Response audit information |
Describes a HTTP request
| Field Name | Type | Description |
|---|---|---|
| QueryString | string | Query string portion of the request URL |
| Scheme | string | Request scheme (http, https) |
| Path | string | Path portion of the request URL |
| Headers | Dictionary | Request headers |
| Content | Content | Request content |
Describes a HTTP response
| Field Name | Type | Description |
|---|---|---|
| StatusCode | int | Response HTTP status code |
| Status | string | String representation of the response status code |
| Reason | string | Response status reason phrase |
| IsSuccess | bool | Indicates if the HTTP response was successful. |
| Headers | Dictionary | Request headers |
| Content | Content | Request content |
Describes the content of a HTTP request or response
| Field Name | Type | Description |
|---|---|---|
| Body | string | Response body content decoded as a string |
| Headers | Dictionary | Content headers |
{
"EventType": "GET http://google.com/doesnotexists",
"Environment": {
"UserName": "Federico",
"MachineName": "FEDE",
"DomainName": "FEDE",
"Culture": "en-US"
},
"StartDate": "2019-05-21T23:18:38.3251378Z",
"EndDate": "2019-05-21T23:18:40.4623427Z",
"Duration": 2137,
"Action": {
"Method": "GET",
"Url": "http://google.com/doesnotexists",
"Version": "1.1",
"Request": {
"QueryString": "",
"Scheme": "http",
"Path": "/doesnotexists",
"Headers": {
}
},
"Response": {
"Headers": {
"Date": "Tue, 21 May 2019 23:18:19 GMT",
"Referrer-Policy": "no-referrer"
},
"Content": {
"Body": "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n ....",
"Headers": {
"Content-Length": "1574",
"Content-Type": "text/html; charset=UTF-8"
}
},
"StatusCode": 404,
"Status": "NotFound",
"Reason": "Not Found",
"IsSuccess": false
}
}
}Entity Framework Extensions and Dapper Plus are major sponsors and are proud to contribute to the development of Audit.NET
Combine the power of auditing with the speed of Bulk Operations to get the best of both worlds — audit and performance.