Generate detailed Audit Logs for Web API Controller calls.
$ dotnet add package Audit.WebApiASP.NET MVC Web API Audit Extension for Audit.NET library (An extensible framework to audit executing operations in .NET).
Generate Audit Trails for ASP.NET MVC Web API calls. This library provides a configurable infrastructure to log interactions with your Asp.NET (or Asp.NET Core) Web API.
NuGet Package
To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.WebApi
If your project targets the full .NET framework, but you are using AspNet Core (Microsoft.AspNetCore.Mvc.*)
you should install and reference the Audit.WebApi.Core package instead, otherwise it will assume you are targeting
the old generation of ASP.NET:
PM> Install-Package Audit.WebApi.Core
If your project targets the NET Core framework (NetStandard >= 1.6), there is no difference between using Audit.WebApi or Audit.WebApi.Core
since both assumes AspNet Core.
This library is implemented as an action filter that intercepts the execution of action methods to generate a detailed audit trail.
For Asp.NET Core, it is also implemented as a Middleware class that can be configured to log requests that does not reach the action filter (i.e. unsolved routes, parsing errors, etc).
The audit can be enabled in different ways:
AuditApi action filter attribute.AuditApiGlobalFilter action filter as a global filter. This method allows more dynamic configuration of the audit settings.AuditMiddleware to the pipeline. This method allow to audit request that doesn't get to the action filter.Decorate your controller with AuditApiAttribute:
using Audit.WebApi;
public class UsersController : ApiController
{
[AuditApi]
public IEnumerable<ApplicationUser> Get()
{
//...
}
[AuditApi(EventTypeName = "GetUser",
IncludeHeaders = true, IncludeResponseHeaders = true, IncludeResponseBody = true, IncludeRequestBody = true, IncludeModelState = true)]
public IHttpActionResult Get(string id)
{
//...
}
}
You can also decorate the controller class with the AuditApi attribute so it will apply to all the actions, for example:
using Audit.WebApi;
[AuditApi(EventTypeName = "{controller}/{action} ({verb})", IncludeResponseBody = true, IncludeRequestBody = true, IncludeModelState = true)]
public class UsersController : ApiController
{
public IEnumerable<ApplicationUser> Get()
{
//...
}
public IHttpActionResult Get(string id)
{
//...
}
}
You can also add the AuditApiAttribute as a global filter, for example for Asp.NET Core:
using Audit.WebApi;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(_ => _
.Filters.Add(new AuditApiAttribute()));
}
}
Note
For custom configuration it is recommended to use the
AuditApiGlobalFilteras a global filter. See next section.
Alternatively, you can add one or more AuditApiGlobalFilter as global action filters.
This method allows to dynamically change the audit settings as functions of the context, via a fluent API.
Note this action filter cannot be used to statically decorate the controllers.
using Audit.WebApi;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(mvc =>
{
mvc.AddAuditFilter(config => config
.LogActionIf(d => d.ControllerName == "Orders" && d.ActionName != "GetOrder")
.WithEventType("{verb}.{controller}.{action}")
.IncludeHeaders(ctx => !ctx.ModelState.IsValid)
.IncludeRequestBody()
.IncludeModelState()
.IncludeResponseBody(ctx => ctx.HttpContext.Response.StatusCode == 200));
});
}
For Asp.NET Core, you can additionally (or alternatively) configure a middleware to be able to log requests that doesn't get into an action filter (i.e. request that cannot be routed, etc).
On your startup Configure method, call the UseAuditMiddleware() extension method:
using Audit.WebApi;
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseAuditMiddleware(_ => _
.FilterByRequest(rq => !rq.Path.Value.EndsWith("favicon.ico"))
.WithEventType("{verb}:{url}")
.IncludeHeaders()
.IncludeResponseHeaders()
.IncludeRequestBody()
.IncludeResponseBody());
app.UseMvc();
}
}
Warning
You should call
UseAuditMiddleware()beforeUseMvc(), otherwise the middleware will not be able to process MVC actions.
If you only configure the middleware (no audit action filters) but want to ignore actions via [AuditIgnoreAttribute], you must
add an action filter to discard the AuditScope. This is needed because the middleware cannot inspect the
MVC action attributes. You can use the AuditIgnoreActionFilter for this purpose, adding it to the MVC pipeline like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(mvc =>
{
mvc.Filters.Add(new AuditIgnoreActionFilter());
});
}
You can mix the Audit Middleware together with the Global Action Filter (and/or Local Action Filters). Take into account that:
AuditIgnore attribute is handled by the Action Filters, there is no need to add the AuditIgnoreActionFilter to the MVC filters when using a mixed approach.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.
You can setup the data provider to use by registering an instance of an AuditDataProdiver
to the IServiceCollection on your start-up code, for example:
var dataProvider = new FileDataProvider(cfg => cfg.Directory(@"C:\Logs"));
services.AddSingleton<AuditDataProvider>(dataProvider);
Or, alternatively, you can setup the data provider globally with the static configuration:
Audit.Core.Configuration.DataProvider = new FileDataProvider(cfg => cfg.Directory(@"C:\Logs"));
Or using the fluent API:
Audit.Core.Configuration.Setup()
.UseFileLogProvider(cfg => cfg.Directory(@"C:\Logs"));
The AuditApiAttribute can be configured with the following properties:
The AuditApiGlobalFilter can be configured with the following methods:
ContollerActionDescriptor / HttpRequest to determine whether the action should be logged or not.To configure the output persistence mechanism please see Event Output Configuration.
When IncludeRequestBody is set to true (or when using IncludeRequestBodyFor/ExcludeRequestBodyFor), you must enable rewind on the request body stream, otherwise, the controller won't be able to read the request body since by default, it's a forward-only stream that can be read only once. You can enable rewind on your startup logic with the following code:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) => { // <----
context.Request.EnableBuffering(); // or .EnableRewind();
await next();
});
app.UseMvc();
}
HttpRequest to determine whether the request should be logged or not, by default all requests are logged.To selectively exclude certain controllers, actions, action parameters or action responses, you can decorate them with AuditIgnore attribute.
For example:
[Route("api/[controller]")]
[AuditApi(EventTypeName = "{controller}/{action}")]
public class AccountController : Controller
{
[HttpGet]
[AuditIgnore]
public IEnumerable<string> GetAccounts()
{
// this action will not be audited
}
[HttpPost]
public IEnumerable<string> PostAccount(string user, [AuditIgnore]string password)
{
// password argument will not be audited
}
[HttpGet]
[return:AuditIgnore]
public IEnumerable<string> GetSecrets()
{
// the return value of this action will not be audited
}
}
The following table describes the Audit.WebApi output fields:
| Field Name | Type | Description |
|---|---|---|
| TraceId | string | A unique identifier per request |
| HttpMethod | string | HTTP method (GET, POST, etc) |
| ControllerName | string | The controller name |
| ActionName | string | The action name |
| FormVariables | Object | Form-data input variables passed to the action |
| ActionParameters | Object | The action parameters passed |
| UserName | string | Username on the HttpContext Identity |
| RequestUrl | string | URL of the request |
| IpAddress | string | Client IP address |
| ResponseStatusCode | integer | HTTP response status code |
| ResponseStatus | string | Response status description |
| RequestBody | BodyContent | The request body (optional) |
| ResponseBody | BodyContent | The response body (optional) |
| Headers | Object | HTTP Request Headers (optional) |
| ResponseHeaders | Object | HTTP Response Headers (optional) |
| ModelStateValid | boolean | Boolean to indicate if the model is valid |
| ModelStateErrors | string | Error description when the model is invalid |
| Exception | string | The exception thrown details (if any) |
| Field Name | Type | Description |
|---|---|---|
| Type | string | The body type reported |
| Length | long? | The length of the body if reported |
| Value | Object | The body content |
You can access the Audit Scope object for customization from the API controller action by calling the ApiController extension method GetCurrentAuditScope().
For example:
[AuditApi]
public class UsersController : ApiController
{
public IHttpActionResult Get(string id)
{
//...
var auditScope = this.GetCurrentAuditScope();
auditScope.Comment("New comment from controller");
auditScope.SetCustomField("TestField", Guid.NewGuid());
//...
}
}
See Audit.NET documentation about Custom Field and Comments for more information.
{
"EventType":"POST Values/Post",
"Environment":{
"UserName":"Federico",
"MachineName":"HP",
"DomainName":"HP",
"CallingMethodName":"WebApiTest.Controllers.ValuesController.Post()",
"AssemblyName":"WebApiTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"Culture":"en-US"
},
"StartDate":"2017-03-09T18:03:05.5287603-06:00",
"EndDate":"2017-03-09T18:03:05.5307604-06:00",
"Duration":2,
"Action":{
"TraceId": "0HLFLQP4HGFAF_00000001",
"HttpMethod":"POST",
"ControllerName":"Values",
"ActionName":"Post",
"ActionParameters":{
"value":{
"Id":100,
"Text":"Test"
}
},
"FormVariables":{
},
"RequestUrl":"http://localhost:65080/api/values",
"IpAddress":"127.0.0.1",
"ResponseStatus":"OK",
"ResponseStatusCode":200,
"RequestBody":{
"Type":"application/json",
"Length":27,
"Value":"{ Id: 100, Text: \"Test\" }"
},
"ResponseBody":{
"Type":"SomeObject",
"Value":{
"Id":1795824380,
"Text":"Test"
}
},
"Headers": {
"Connection": "Keep-Alive",
"Accept": "text/html, application/xhtml+xml, image/jxr, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-GB",
"Host": "localhost:37341",
"User-Agent": "Mozilla/5.0, (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0), like, Gecko"
}
}
}
If you are creating an ASP.NET Core Web API project from scratch, you can use the dotnet new template provided on the library Audit.WebApi.Template. This allows to quickly generate an audit-enabled Web API project that can be used as a starting point for your project or as a working example.
To install the template on your system, just type:
dotnet new -i Audit.WebApi.Template
Once you install the template, you should see it on the dotnet new templates list with the name webapiaudit as follows:

You can now create a new project on the current folder by running:
dotnet new webapiaudit
This will create a new Asp.NET Core project.
You can optionally include Entity Framework Core by adding the -E parameter
dotnet new webapiaudit -E
Also you can include a service interceptor (using Audit.DynamicProxy) for your service dependencies calls, by adding the -S parameter
dotnet new webapiaudit -S
To get help about the options:
dotnet new webapiaudit -h
If you like this project please contribute in any of the following ways: