HttpClient CustomFields #673
-
With the Audit.Mvc.Core package I'm using:
The other calls are used to set some CustomFields for the Audit.
How can I achieve the same with the Audit.HttpClient? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
There are several ways to achieve this, such as by setting a custom action or providing an audit scope factory that returns a scope with the custom fields populated. However, it ultimately depends on the source of the data for the custom fields. How do you retrieve the projectId and buildingId? |
Beta Was this translation helpful? Give feedback.
-
@thepirat000 As an example, I've an Azure Function which gets called with a buildingId. ProjectId gets resolved from the DB.
The HttpService is injected in the function
With an injected type HttpClient
program.cs
|
Beta Was this translation helpful? Give feedback.
-
I think what you're asking for is a way to add contextual information to each HTTP action within an audited HttpClient. We cannot provide an instance method to add audit data to a request, as is possible in MVC, because the audit scope is created, saved, and disposed within a single 'GetAsync' (or 'SendAsync') call. Therefore, adding an extension method to the HttpClient instance is not feasible: httpClient.GetCurrentAuditScope().SetCustomField("ContextId", contextId); // <-- This
var response = await httpClient.GetAsync("https://jsonplaceholder.typicode.com/posts"); Even if feasible, this approach would only work with transient HttpClient instances, as it is not thread-safe otherwise. However, we could use the HttpRequestMessage.Options as a workaround to add contextual information to the HTTP Audit Event. If the library provided a way to include the Options in the audit output, we could use that to add any extra information that is not sent as part of the request. You would need to change your var request = new HttpRequestMessage(HttpMethod.Get, "https://jsonplaceholder.typicode.com/posts");
request.Options.Set(new HttpRequestOptionsKey<int>("ContextId"), contextId);
await httpClient.SendAsync(request); The library will need a change to provide a configuration and logic to store the Options values. For example: services.AddHttpClient<IAuditHttpClient, AuditHttpClient>()
.AddAuditHandler(audit =>
{
audit.IncludeRequestBody();
audit.IncludeResponseBody();
audit.IncludeOptions(["ContextId"]); // <-- This
}); With this setup, the AuditEvent will include the option values under Will something like this work for your use case? |
Beta Was this translation helpful? Give feedback.
-
The For example: services.AddHttpClient<IAuditHttpClient, AuditHttpClient>()
.AddAuditHandler(audit =>
{
audit.IncludeRequestBody();
audit.IncludeResponseBody();
audit.IncludeOptions();
}); Additionally, a new GetRequestMessage() method has been introduced in HttpAction to retrieve the current request message from the audit event. This enables you to achieve the same result with a custom action, such as Audit.Core.Configuration.AddOnCreatedAction(scope =>
{
var originalMessage = scope.GetHttpAction().GetRequestMessage();
originalMessage.Options.TryGetValue(new HttpRequestOptionsKey<int>("ContextId"), out int contextId);
scope.SetCustomField("ContextId", contextId);
}); |
Beta Was this translation helpful? Give feedback.
The
IncludeOptions
configuration was added in version 25.0.7.For example:
Additionally, a new GetRequestMessage() method has been introduced in HttpAction to retrieve the current request message from the audit event. This enables you to achieve the same result with a custom action, such as