WCF client Audit Extension for Audit.NET library.
Generate Audit Logs for Windows Communication Foundation (WCF) service calls on client-side.
Audit.Wcf.Client
provides the client-side infrastructure to log interactions with WCF services.
It records detailed information of the service method calls by using an IClientMessageInspector
to intercept the request and response messages.
If you are looking for server-side audit, please check the Audit.WCF
library.
NuGet Package
To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.Wcf.Client
A custom endpoint behavior is provided to enable auditing on the requests and responses of the WCF service calls.
There are two ways to configure the endpoint behavior:
-
Adding the audit behavior to the client endpoint on the app.config / web.config of your client application.
For example:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IService" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost/Service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="MyServiceReference.IService" name="BasicHttpBinding_IService" behaviorConfiguration="auditBehavior" /> </client> <behaviors> <endpointBehaviors> <behavior name="auditBehavior"> <auditBehavior includeResponseHeaders="true" eventType="Catalog:{action}" /> </behavior> </endpointBehaviors> </behaviors> <extensions> <behaviorExtensions> <add name="auditBehavior" type="Audit.Wcf.Client.AuditBehavior, Audit.Wcf.Client, PublicKeyToken=571d6b80b242c87e"/> </behaviorExtensions> </extensions> </system.serviceModel> </configuration>
Note you have to include the
behaviorConfiguration
property on the client endpoint, the endpoint behaviorauditBehavior
, and the behavior extension pointing the provided typeAudit.Wcf.Client.AuditBehavior
from the assemblyAudit.Wcf.Client
-
Adding the audit behavior on code when creating the channel.
For example:
public static IService GetServiceProxy() { var channelFactory = new ChannelFactory<IService>(new BasicHttpBinding(), new EndpointAddress(URL)); channelFactory.Endpoint.EndpointBehaviors.Add(new AuditEndpointBehavior() { EventType = "Catalog:{action}", IncludeResponseHeaders = true }); return channelFactory.CreateChannel(); }
The Audit Behavior can be configured with the following properties:
- EventType: A string that identifies the event type. Default is "{action}".
- Can contain the following placeholders:
- {action}: Replaced with the action URL
- IncludeRequestHeaders: Boolean value that indicates whether the output should include the request headers. Default is false.
- IncludeResponseHeaders: Boolean value that indicates whether the output should include the response headers. Default is false.
- AuditDataProvider: Allows to set a specific audit data provider. By default the globally configured data provider is used. See Audit.NET Data Providers section for more information.
- AuditScopeFactory: Allows to set a specific audit scope factory. By default the globally configured
AuditScopeFactory
is used.
To globally configure the output mechanism, use the Audit.Core.Configuration
class to set a Data Provider.
For more details please see Event Output Configuration.
For example:
Audit.Core.Configuration.Setup()
.UseFileLogProvider(config => config.Directory(@"C:\Logs"));
This should be done prior to the AuditScope creation, i.e. during application startup.
Audit.Wcf.Client
output includes:
- Execution time and duration
- Environment information such as user, machine, domain and locale.
- Request body
- Response body
- Http Status Code
- Request headers (optional)
- Response headers (optional)
The following table describes the Audit.Wcf.Client
output fields:
Describes an audited WCF client event
Field Name | Type | Description |
---|---|---|
Action | string | The requested action URL |
RequestBody | string | The request body (XML) |
RequestHeaders | Dictionary<string, string> | The request HTTP headers (optional) |
HttpMethod | string | The HTTP method used (POST, PUT) |
ResponseAction | string | The response action (if any) |
MessageId | string | The message ID (if any) |
ResponseStatusCode | HttpStatusCode | The response HTTP status code |
ResponseBody | string | The response body (XML) |
ResponseHeaders | Dictionary<string, string> | The response HTTP headers (optional) |
IsFault | string | Value that indicates whether the message generated any SOAP faults. |
{
"EventType": "Catalog:http://tempuri.org/IService/GetProductDetails",
"WcfClientEvent": {
"Action": "http://tempuri.org/IService/GetProductDetails",
"RequestBody": "<s:Envelope> ... </s:Envelope>",
"HttpMethod": "POST",
"ResponseAction": null,
"MessageId": null,
"ResponseStatuscode": 200,
"ResponseBody": "<s:Envelope> ... </s:Envelope>",
"ResponseHeaders": {
"Content-Length": "431",
"Cache-Control": "private",
"Content-Type": "text/xml; charset=utf-8",
"Date": "Thu, 29 Jul 2021 23",
"Server": "Microsoft-IIS/10.0",
"X-AspNet-Version": "4.0.30319",
"X-Powered-By": "ASP.NET"
},
"IsFault": false
},
"Environment": {
"UserName": "Federico",
"MachineName": "DESKTOP",
"DomainName": "DESKTOP",
"CallingMethodName": "WcfClient.MyServiceReference.ServiceClient.GetProductDetails()",
"AssemblyName": "WcfClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"Culture": "en-US"
},
"StartDate": "2021-07-29T23:31:10.9631278Z",
"EndDate": "2021-07-29T23:31:11.0641269Z",
"Duration": 101
}