Audit.Mvc 25.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Audit.Mvc --version 25.0.6                
NuGet\Install-Package Audit.Mvc -Version 25.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Audit.Mvc" Version="25.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Audit.Mvc --version 25.0.6                
#r "nuget: Audit.Mvc, 25.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Audit.Mvc as a Cake Addin
#addin nuget:?package=Audit.Mvc&version=25.0.6

// Install Audit.Mvc as a Cake Tool
#tool nuget:?package=Audit.Mvc&version=25.0.6                

Audit.Mvc

MVC Actions Audit Extension for Audit.NET library. (An extensible framework to audit executing operations in .NET).

Generate Audit Trails for MVC actions. Supporting Asp NET Core Mvc.

Audit.Mvc / Audit.Mvc.Core provides the infrastructure to log interactions with MVC applications. It can record action methods calls to controllers and razor pages.

Install

NuGet Packages

NuGet Status NuGet Count

NuGet Status NuGet Count

To install the ASP.NET package run the following command on the Package Manager Console:

PM> Install-Package Audit.Mvc

To install the Asp Net Core package:

PM> Install-Package Audit.Mvc.Core

IMPORTANT NOTE

Previously, it was possible to employ the Audit.Mvc package for ASP.NET Core MVC or vice versa.

However, starting from version 23, the Audit.Mvc package is now exclusively designed for ASP.NET Framework MVC, whereas the Audit.Mvc.Core package is exclusively tailored for ASP.NET Core MVC.

Please upgrade your references accordingly.

Usage

MVC Controllers

Decorate the MVC Actions or Controllers you want to audit with [Audit] action filter.

For example:

public class HomeController : Controller
{
    [Audit]
    public ActionResult Index(int id, string name)
    {
      //...
      return View(new SomeViewModel() { Id = id, Name = name });
    }

    [Audit(EventType = "InsertOrderAction", IncludeHeaders = true, IncludeModel = true)]
    [HttpPost]
    public ActionResult TestPost(SomeViewModel model)
    {
      //...
    }
}

The [Audit] attribute cannot be used on razor pages, because action filters are not supported on razor pages.

Razor pages

To audit razor pages, include the AuditPageFilter on the filters collection on your startup code, for example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages()
        .AddMvcOptions(options =>
        {
            options.Filters.Add(new AuditPageFilter()
            {
                IncludeHeaders = true
            });
        });
}

Or you can apply the filter only to certain pages, for example for pages under /Movies path:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages(options =>
    {
        options.Conventions.AddFolderApplicationModelConvention("/Movies", model => model.Filters.Add(new AuditPageFilter()
        {
            IncludeResponseBody = true
        }));
    });
}

Alternatively, if you want to setup the audit on a particular page and/or don't want to add the filter as a global filter, you can override the OnPageHandlerExecutionAsync on your page model and manually call the same method on an AuditPageFilter instance:

public class YourPageModel : PageModel
{
    private readonly AuditPageFilter _pageFilter = new AuditPageFilter() { IncludeHeaders = true };

    public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
    {
        await _pageFilter.OnPageHandlerExecutionAsync(context, next);
    }
    // ...
}

Configuration

Output

The MVC 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.

Settings

The AuditAttribute can be configured with the following properties:

  • EventType: A string that identifies the event type. Can contain the following placeholders:
    • {controller}: replaced with the controller name (only for MVC).
    • {action}: replaced with the action method name (or the display name for razor pages).
    • {verb}: replaced with the HTTP verb used (GET, POST, etc).
    • {area}: replaced with the area name (only for razor pages).
    • {path}: replaced with the view path (only for razor pages).
  • IncludeHeaders: Boolean to indicate whether to include the Http Request Headers or not.
  • IncludeModel: Boolean to indicate whether to include the View Model or not.
  • IncludeRequestBody: Boolean to indicate whether to include or exclude the request body from the logs. Default is false. (Check following note)
  • IncludeResponseBody: Boolean to indicate whether to include response body or not. Default is false.
  • SerializeActionParameters: Boolean to indicate whether the action arguments should be pre-serialized to the audit event. Default is false.

To configure the output persistence mechanism please see Event Output Configuration.

NOTE

When IncludeRequestBody is set to true you may need to enable rewind on the request body stream, otherwise the controller won't be able to read the request body more than once (by default it's a forwand-only stream that can be read only once). You can enable rewind on your startup logic with the following startup code:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Use(async (context, next) => {
        context.Request.EnableBuffering();
        await next();
    });
}

Audit Ignore attribute

To selectively exclude certain controllers, action methods, action parameters or return values, you can decorate them with AuditIgnore attribute.

For example:

[Audit(IncludeHeaders = true, IncludeModel = true)]
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(string user)
    {
        // the response body of this action will not be audited
    }
}

You can also decorate the razor pages classes, methods or arguments to be ignored on the audits:

public class IndexModel : PageModel
{
    [return:AuditIgnore]
    public IActionResult OnGet(string user)
    {
        // the response of this action will not be audited
    }

    [AuditIgnore]
    public void OnDelete(string user)
    {
        // this action will not be audited
    }

    public async Task<IActionResult> OnPostAsync([AuditIgnore]string password)
    {
        // password argument will not be audited
    }
}

Output details

The following table describes the Audit.Mvc 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 (or the area name for razor pages)
ActionName string The action name (or the display name for razor pages)
ViewName string The view name (if any)
ViewPath string View physical path (if any)
FormVariables Object Form-data input variables passed to the action
ActionParameters Object The action parameters passed
RequestBody BodyContent The request body (optional)
ResponseBody BodyContent The response body (optional)
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
Headers Object HTTP Headers (optional)
Model Object The model object returned by the controller (if any) (optional)
ModelStateValid boolean Boolean to indicate if the model is valid
ModelStateErrors string Error description when the model is invalid
RedirectLocation string The redirect location (if any)
Exception string The exception thrown details (if any)

BodyContent

Field Name Type Description
Type string The body type reported
Length long? The length of the body if reported
Value Object The body content

Customization

You can access the Audit Scope from the controller action by calling the Controller extension method GetCurrentAuditScope().

For example:

public class HomeController : Controller
{
    [Audit]
    public ActionResult Index(int id, string name)
    {
       //...
       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.

Output Sample for Get operation

HomeController.Index (GET) with params: id=1234567&name=test

{
    "EventType": "Home/Index (GET)",
    "Environment": {
        ...
    },
    "StartDate": "2016-08-22T18:31:14.6550924-05:00",
    "EndDate": "2016-08-22T18:31:23.1834012-05:00",
    "Duration": 8529,
    "Action": {
        "TraceId": "0HLFLQP4HGFAG_00000001",
        "HttpMethod": "GET",
        "ControllerName": "Home",
        "ActionName": "Index",
        "ViewName": "Index",
        "ViewPath": "~/Views/Home/Index.cshtml",
        "FormVariables": {},
        "ActionParameters": {
            "id": 1234567,
            "name": "test",
        },
        "UserName": "federico@mycompany.com",
        "RequestUrl": "/",
        "IpAddress": "127.0.0.1",
        "ResponseStatus": "200 OK",
        "ResponseStatusCode": 200,
        "ModelStateValid": true,
        "RedirectLocation": null
    }
}

Output Sample for Post operation

HomeController.TestPost (POST) with body: id=1234567&name=test

{
    "EventType": "InsertOrderAction",
    "Environment": {
        ...
    },
    "StartDate": "2016-08-22T18:31:00.0020036-05:00",
    "EndDate": "2016-08-22T18:31:15.1705128-05:00",
    "Duration": 15000,
    "Action": {
        "TraceId": "0HLFLQP4HGFAG_00000002",
        "HttpMethod": "POST",
        "ControllerName": "Home",
        "ActionName": "TestPost",
        "FormVariables": {
            "id": "1234567",
            "name": "test"
        },
        "ActionParameters": {
            "model": {
                "id": 1234567,
                "name": "test"
            }
        },
        "UserName": "federico@mycompany.com",
        "RequestUrl": "/Home/TestPost",
        "IpAddress": "::1",
        "ResponseStatus": "200 OK",
        "ResponseStatusCode": 200,
        "Headers": {
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Content-Length": "24",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "es-419,es;q=0.8",
            "Host": "localhost:37341",
            "Referer": "http://localhost:37341/",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743",
            "Origin": "http://localhost:37341",
            "Upgrade-Insecure-Requests": "1"
        },
        "ModelStateValid": false,
        "ModelStateErrors": {
            "Id": "The field Id must be between 0 and 9999."
        },
        "RedirectLocation": null
    }
}

MVC template (dotnet new)

If you are creating an ASP.NET Core MVC project from scratch, you can use the dotnet new template provided on the library Audit.Mvc.Template. This allows to quickly generate an audit-enabled MVC 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.Mvc.Template

Once you install the template, you should see it on the dotnet new templates list with the name mvcaudit as follows:

capture

You can now create a new project on the current folder by running:

dotnet new mvcaudit

This will create a new Asp.NET Core 2.1 project.

To get help about the options:

dotnet new mvcaudit -h

Contribute

If you like this project please contribute in any of the following ways:

  • Star this project on GitHub.
  • Request a new feature or expose any bug you found by creating a new issue.
  • Ask any questions about the library on StackOverflow.
  • Subscribe to and use the Gitter Audit.NET channel.
  • Support the project by becoming a Backer: Backer    
  • Spread the word by blogging about it, or sharing it on social networks: <p class="share-buttons"> <a href="https://www.facebook.com/sharer/sharer.php?u=https://nuget.org/packages/Audit.NET/&t=Check+out+Audit.NET" target="_blank"> <img width="24" height="24" alt="Share this package on Facebook" src="https://nuget.org/Content/gallery/img/facebook.svg" / > </a> <a href="https://twitter.com/intent/tweet?url=https://nuget.org/packages/Audit.NET/&text=Check+out+Audit.NET" target="_blank"> <img width="24" height="24" alt="Tweet this package" src="https://nuget.org/Content/gallery/img/twitter.svg" /> </a> </p>
  • Buy me a coffee via ko-fi: <br/>ko-fi
Product Compatible and additional computed target framework versions.
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Audit.Mvc:

Package Downloads
BuildingBlocks.Logging

Componente para geração de logs.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
27.3.2 86 12/11/2024
27.3.1 74 12/10/2024
27.3.0 87 12/8/2024
27.2.0 105 11/23/2024
27.1.1 103 10/28/2024
27.1.0 96 10/24/2024
27.0.3 123 9/25/2024
27.0.2 100 9/19/2024
27.0.1 146 9/4/2024
27.0.0 116 9/3/2024
26.0.1 147 8/22/2024
26.0.0 143 7/19/2024
25.0.7 127 7/4/2024
25.0.6 121 6/24/2024
25.0.5 983 6/18/2024
25.0.4 197 3/24/2024
25.0.3 604 3/13/2024
25.0.2 121 3/12/2024
25.0.1 139 2/28/2024
25.0.0 128 2/16/2024
24.0.1 162 2/12/2024
24.0.0 142 2/12/2024
23.0.0 1,759 12/14/2023
22.1.0 26,603 12/9/2023
22.0.2 11,984 12/1/2023
22.0.1 243 11/16/2023
22.0.0 139 11/14/2023
21.1.0 622 10/9/2023
21.0.4 22,259 9/15/2023
21.0.3 13,318 7/9/2023
21.0.2 230 7/6/2023
21.0.1 23,339 5/27/2023
21.0.0 1,269 4/15/2023
20.2.4 396 3/27/2023
20.2.3 407 3/17/2023
20.2.2 386 3/14/2023
20.2.1 311 3/11/2023
20.2.0 329 3/7/2023
20.1.6 414 2/23/2023
20.1.5 18,820 2/9/2023
20.1.4 413 1/28/2023
20.1.3 1,660 12/21/2022
20.1.2 4,130 12/14/2022
20.1.1 404 12/12/2022
20.1.0 405 12/4/2022
20.0.4 408 11/30/2022
20.0.3 2,440 10/28/2022
20.0.2 514 10/26/2022
20.0.1 537 10/21/2022
20.0.0 11,274 10/1/2022
19.4.1 5,828 9/10/2022
19.4.0 2,127 9/2/2022
19.3.0 3,130 8/23/2022
19.2.2 1,450 8/11/2022
19.2.1 890 8/6/2022
19.2.0 1,103 7/24/2022
19.1.4 1,413 5/23/2022
19.1.3 611 5/22/2022
19.1.2 703 5/18/2022
19.1.1 1,139 4/28/2022
19.1.0 736 4/10/2022
19.0.7 1,653 3/13/2022
19.0.6 1,663 3/7/2022
19.0.5 1,167 1/28/2022
19.0.4 867 1/23/2022
19.0.3 5,870 12/14/2021
19.0.2 517 12/11/2021
19.0.1 952 11/20/2021
19.0.0 597 11/11/2021
19.0.0-rc.net60.2 181 9/26/2021
19.0.0-rc.net60.1 219 9/16/2021
18.1.6 13,597 9/26/2021
18.1.5 1,290 9/7/2021
18.1.4 656 9/6/2021
18.1.3 1,155 8/19/2021
18.1.2 744 8/8/2021
18.1.1 627 8/5/2021
18.1.0 708 8/1/2021
18.0.1 717 7/30/2021
18.0.0 803 7/26/2021
17.0.8 838 7/7/2021
17.0.7 1,261 6/16/2021
17.0.6 798 6/5/2021
17.0.5 2,925 5/28/2021
17.0.4 3,067 5/4/2021
17.0.3 647 5/1/2021
17.0.2 21,611 4/22/2021
17.0.1 649 4/18/2021
17.0.0 741 3/26/2021
16.5.6 687 3/25/2021
16.5.5 675 3/23/2021
16.5.4 819 3/9/2021
16.5.3 723 2/26/2021
16.5.2 662 2/23/2021
16.5.1 652 2/21/2021
16.5.0 3,083 2/17/2021
16.4.5 638 2/15/2021
16.4.4 1,000 2/5/2021
16.4.3 1,124 1/27/2021
16.4.2 746 1/22/2021
16.4.1 763 1/21/2021
16.4.0 2,172 1/11/2021
16.3.3 690 1/8/2021
16.3.2 681 1/3/2021
16.3.1 675 12/31/2020
16.3.0 730 12/30/2020
16.2.1 746 12/27/2020
16.2.0 1,618 10/13/2020
16.1.5 825 10/4/2020
16.1.4 898 9/17/2020
16.1.3 855 9/13/2020
16.1.2 2,178 9/9/2020
16.1.1 810 9/3/2020
16.1.0 1,558 8/19/2020
16.0.3 863 8/15/2020
16.0.2 807 8/9/2020
16.0.1 905 8/8/2020
16.0.0 762 8/7/2020
15.3.0 63,206 7/23/2020
15.2.3 1,438 7/14/2020
15.2.2 3,392 5/19/2020
15.2.1 6,968 5/12/2020
15.2.0 862 5/9/2020
15.1.1 1,706 5/4/2020
15.1.0 972 4/13/2020
15.0.5 1,407 3/18/2020
15.0.4 1,079 2/28/2020
15.0.3 845 2/26/2020
15.0.2 3,427 1/20/2020
15.0.1 1,014 1/10/2020
15.0.0 1,346 12/17/2019
14.9.1 1,047 11/30/2019
14.9.0 850 11/29/2019
14.8.1 800 11/26/2019
14.8.0 902 11/20/2019
14.7.0 41,991 10/9/2019
14.6.6 853 10/8/2019
14.6.5 922 9/27/2019
14.6.4 972 9/21/2019
14.6.3 1,985 8/12/2019
14.6.2 984 8/3/2019
14.6.1 857 8/3/2019
14.6.0 917 7/26/2019
14.5.7 2,335 7/18/2019
14.5.6 1,527 7/10/2019
14.5.5 1,223 7/1/2019
14.5.4 1,122 6/17/2019
14.5.3 1,227 6/5/2019
14.5.2 887 5/30/2019
14.5.1 853 5/28/2019
14.5.0 975 5/24/2019
14.4.0 938 5/22/2019
14.3.4 5,188 5/14/2019
14.3.3 889 5/9/2019
14.3.2 1,134 4/30/2019
14.3.1 1,578 4/27/2019
14.3.0 5,800 4/24/2019
14.2.3 1,125 4/17/2019
14.2.2 1,149 4/10/2019
14.2.1 973 4/5/2019
14.2.0 1,403 3/16/2019
14.1.1 1,180 3/8/2019
14.1.0 1,462 2/11/2019
14.0.4 1,145 1/31/2019
14.0.3 1,128 1/22/2019
14.0.2 2,030 12/15/2018
14.0.1 1,224 11/29/2018
14.0.0 1,152 11/19/2018
13.3.0 1,250 11/16/2018
13.2.2 1,194 11/15/2018
13.2.1 1,833 11/13/2018
13.2.0 1,156 10/31/2018
13.1.5 1,156 10/31/2018
13.1.4 1,334 10/25/2018
13.1.3 1,218 10/18/2018
13.1.2 8,291 9/12/2018
13.1.1 1,236 9/11/2018
13.1.0 1,221 9/11/2018
13.0.0 8,739 8/29/2018
12.3.6 1,283 8/29/2018
12.3.5 1,243 8/22/2018
12.3.4 1,181 8/21/2018
12.3.3 26,009 8/21/2018
12.3.2 1,220 8/20/2018
12.3.1 1,276 8/20/2018
12.3.0 1,245 8/20/2018
12.2.2 1,402 8/15/2018
12.2.1 1,318 8/9/2018
12.2.0 1,289 8/8/2018
12.1.11 2,372 7/30/2018
12.1.10 1,252 7/20/2018
12.1.9 1,398 7/10/2018
12.1.8 1,290 7/2/2018
12.1.7 7,964 6/7/2018
12.1.6 2,794 6/4/2018
12.1.5 1,386 6/2/2018
12.1.4 1,573 5/25/2018
12.1.3 2,973 5/16/2018
12.1.2 1,392 5/15/2018
12.1.1 1,448 5/14/2018
12.1.0 1,406 5/9/2018
12.0.7 1,421 5/5/2018
12.0.6 1,518 5/4/2018
12.0.5 1,402 5/3/2018
12.0.4 1,466 4/30/2018
12.0.3 1,491 4/30/2018
12.0.2 1,308 4/27/2018
12.0.1 1,398 4/25/2018
12.0.0 1,349 4/22/2018
11.2.0 1,413 4/11/2018
11.1.0 2,001 4/8/2018
11.0.8 1,509 3/26/2018
11.0.7 1,399 3/20/2018
11.0.6 1,471 3/7/2018
11.0.5 8,554 2/22/2018
11.0.4 2,664 2/14/2018
11.0.3 1,496 2/12/2018
11.0.2 1,391 2/9/2018
11.0.1 1,458 1/29/2018
11.0.0 1,541 1/15/2018
10.0.3 1,561 12/29/2017
10.0.2 1,360 12/26/2017
10.0.1 1,313 12/18/2017
10.0.0 1,302 12/18/2017
9.3.0 1,431 12/17/2017
9.2.0 1,353 12/17/2017
9.1.3 1,354 12/5/2017
9.1.2 1,337 11/27/2017
9.1.1 1,357 11/21/2017
9.1.0 1,304 11/21/2017
9.0.1 1,290 11/11/2017
9.0.0 1,268 11/10/2017
8.7.0 1,446 11/9/2017
8.6.0 1,338 11/9/2017
8.5.0 4,221 10/3/2017
8.4.0 1,295 10/3/2017
8.3.1 1,463 9/8/2017
8.3.0 1,343 9/8/2017
8.2.0 1,311 9/4/2017
8.1.0 1,374 8/22/2017
8.0.0 1,459 8/19/2017
7.1.3 1,501 8/14/2017
7.1.2 1,372 8/2/2017
7.1.1 1,410 7/26/2017
7.1.0 3,816 7/5/2017
7.0.9 1,391 6/28/2017
7.0.8 1,421 6/19/2017
7.0.6 4,278 4/7/2017
7.0.5 1,608 3/21/2017
7.0.4 1,383 3/21/2017
7.0.3 1,397 3/20/2017
7.0.2 1,475 3/13/2017
7.0.0 1,492 3/1/2017
6.2.0 3,547 2/25/2017
6.1.0 1,413 2/14/2017
6.0.0 1,468 2/9/2017
5.3.0 1,291 2/5/2017
5.2.0 1,270 1/26/2017
5.1.0 1,416 1/19/2017
5.0.0 1,435 1/7/2017
4.11.0 1,371 1/5/2017
4.10.0 1,272 12/31/2016
4.9.0 1,269 12/26/2016
4.8.0 1,333 12/17/2016
4.7.0 1,359 12/8/2016
4.6.5 1,284 12/4/2016
4.6.4 1,322 11/25/2016
4.6.2 1,322 11/18/2016
4.6.1 1,281 11/15/2016
4.6.0 1,285 11/11/2016
4.5.9 1,523 11/2/2016
4.5.8 1,326 11/2/2016
4.5.7 1,316 10/26/2016
4.5.6 1,571 10/6/2016
4.5.5 1,303 10/3/2016
4.5.4 1,252 10/2/2016
4.5.3 1,273 9/30/2016
4.5.2 1,310 9/28/2016
4.5.1 1,291 9/28/2016
4.5.0 1,347 9/28/2016
4.4.0 1,446 9/23/2016
4.3.0 1,337 9/22/2016
4.2.0 1,516 9/19/2016
4.1.0 1,260 9/13/2016
4.0.1 1,267 9/9/2016
4.0.0 1,296 9/9/2016
3.6.0 1,295 9/7/2016
3.4.0 1,302 9/7/2016
3.3.0 1,241 9/4/2016
3.2.0 1,266 9/3/2016
3.1.0 1,266 9/2/2016
3.0.0 1,593 8/31/2016
2.5.0 1,299 8/27/2016
2.4.0 1,866 8/26/2016
2.3.0 1,278 8/23/2016
2.1.0 1,742 8/22/2016