Audit.DynamicProxy 25.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Audit.DynamicProxy --version 25.0.4                
NuGet\Install-Package Audit.DynamicProxy -Version 25.0.4                
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.DynamicProxy" Version="25.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Audit.DynamicProxy --version 25.0.4                
#r "nuget: Audit.DynamicProxy, 25.0.4"                
#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.DynamicProxy as a Cake Addin
#addin nuget:?package=Audit.DynamicProxy&version=25.0.4

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

Audit.DynamicProxy

Dynamic Proxy Extension for Audit.NET library.

Generate Audit Logs by intercepting operations on virtually any class.

Audit.DynamicProxy provides the infrastructure to create audit logs for a class without changing its code. It relies on Castle DynamicProxy library to intercept and record the operation calls (methods and properties) including caller info and arguments.

Install

NuGet Package

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

PM> Install-Package Audit.DynamicProxy

NuGet Status NuGet Count

Usage

To enable the audit log for an instance of a class, create a proxy for the class by calling the AuditProxy.Create<>() method.

This will return a proxied audit-enabled instance that you should use instead of the real instance. Each operation on the proxy (access to a property or method call) will generate an Audit Event.

Suppose you have a MyRepository instance that you want to audit, like this:

public class MyDataAccess
{
    IMyRepository _repository = new MyRepository(); // <- Audit this object

    public async Task<int> InsertUserAsync(string userName)
    {
        return await _repository.InsertUserAsync(userName);
    }
    // ...
}

To enable the audit on the _repository object, intercept its assignation by calling AuditProxy.Create<>():

public class MyDataAccess
{
    IMyRepository _repository = AuditProxy.Create<IMyRepository>(new MyRepository()); // Audited!

    public async Task<int> InsertUserAsync(string userName)
    {
        return await _repository.InsertUserAsync(userName);
    }
    // ...
}

You can also intercept conditionally, for example to avoid auditing when a debugger is attached:

public class MyDataAccess
{
    IMyRepository _repository = new MyRepository();

    public MyDataAccess()
    {
        if (!Debugger.IsAttached)
        {
            // Audit only when no debugger is attached
            _repository = AuditProxy.Create<IMyRepository>(_repository);
        }
    }
}

Creating proxies

The AuditProxy.Create<>() method returns an auditable proxy object that inherits from the proxied class/implements proxied interface and forwards calls to the real object.

This is the method signature:

T AuditProxy.Create<T>(T instance, InterceptionSettings settings = null)

Give special attention to the generic type argument T, it can be:

  • An interface: Will generate an interface proxy to log all the interface member calls. (Recommended)
  • A class type: Will generate a class proxy to log virtual member calls. Non-virtual methods can't be automatically audited.

When using an interface proxy, the interception is limited to the members of the interface. And when using a class proxy, the interception is limited to its virtual members.

The instance argument is an instance of the object to be audited.

The settings argument allows you to change the default settings. See settings section for more information.

Settings

The InterceptionSettings class include the following settings:

  • EventType: A string that identifies the event type. Default is "{class}.{method}". Can contain the following placeholders:
    • {class}: Replaced by the class name
    • {method}: Replaced by the method name
  • IgnoreProperties: A boolean indicating whether the audit should ignore the property getters and setters. If true, the property accesses will not be logged. Default is false
  • IgnoreEvents: A boolean indicating whether the audit should ignore the event attach and detach operations. If true, the event accesses will not be logged. Default is false
  • MethodFilter: A function that takes a MethodInfo and returns a boolean indicating whether the method should be taken into account for the logging. Use this setting to have fine grained control over the methods that should be audited. By default all methods are included.
  • AuditDataProvider: Allows to set a specific audit data provider for this instance. By default the globally configured data provider is used. See Audit.NET Data Providers section for more information.

AuditIgnore Attribute

You can exclude specific members, arguments or return values from the audit, by decorating them with the AuditIgnore attribute. For example:

public class MyRepository : IMyRepository
{
    //Ignore a method (no events will be generated for this method)
    [AuditIgnore] 
    public User GetUser(string userName)
    {
       ...
    }

    //Ignore an argument (argument value will not be included in the output)
    public User FindUser(int type, [AuditIgnore] Filter filter)
    {
        ...
    }

    //Exclude the return value (result will not be included in the output)
    [return:AuditIgnore] 
    public List<User> SearchUsers(string text)
    {
        ...
    }
}

Customization

You can access the current audit scope from an audited member by getting the static AuditProxy.CurrentScope property.

The static property AuditProxy.CurrentScope returns the scope for the current running thread and should be accessed from the same thread as the executing audited operation. Calling this from a different thread will lead to an unexpected result. On async methods, you should only access this propery before any await ocurrence.

For example:

public class MyRepository : IMyRepository
{
    public async Task<int> InsertUserAsync(string userName)
    {
        var auditScope = AuditProxy.CurrentScope;   // Get the current scope
        auditScope.SetCustomField("TestField", Guid.NewGuid()); // Set a custom field
        
        //... existing code to insert user ...

        if (pleaseDoNotLog)
        {
            auditScope.Discard(); // Discard the event
        }

        return await _repository.InsertUserAsync(userName);
    }
}

Output

Audit.DynamicProxy output includes:

  • Execution time and duration (async-aware)
  • Environment information such as user, machine, domain and locale.
  • Method parameters (input and output)
  • Return object
  • Exception details
  • Comments and Custom Fields provided

With this information you can know who did the operation, and also measure performance, observe exceptions thrown and get statistics about usage of your classes.

Async calls are logged when the asynchronous call ends; as a continuation task, so the Audit Event includes the actual duration and result.

Output Details

The following table describes the Audit.DynamicProxy output fields:

AuditInterceptEvent

Describes an operation call event

Field Name Type Description
ClassName string Name of class where the operation is defined
MethodName string Name of the audited method
IsAsync boolean A boolean indicating whether the audited method is async
AsyncStatus string If the method is async, this will contain the final Task status (Canceled, Faulted, RanToCompletion)
InstanceQualifiedName string Full qualified name of the class
MethodSignature string The complete method signature
PropertyName string Name of the property modified (if any)
EventName string Name of the event modified (if any)
Arguments argument array The operation arguments (input and output parameters)
Success boolean Indicates if the operation completed succesfully
Exception string The exception details when an exception is thrown
Result argument object The result of the operation

AuditInterceptArgument

Describes an operation argument

Field Name Type Description
Index string Argument index
Name string Argument name
Type string Argument type
Value object Input argument value
OutputValue object Output argument value (Only for ref or out parameters)

Output Samples

Successful async method call:
{
  "EventType": "MyRepository.InsertUserAsync",
  "Environment": {
    "UserName": "Federico",
    "MachineName": "HP",
    "DomainName": "HP",
    "CallingMethodName": "Audit.DynamicProxy.AuditInterceptor.Intercept()",
    "AssemblyName": "Audit.DynamicProxy, Version=4.5.2.0, Culture=neutral, PublicKeyToken=null",
    "Culture": "en-GB"
  },
  "StartDate": "2016-09-30T12:00:35.7073819-05:00",
  "EndDate": "2016-09-30T12:00:36.7168197-05:00",
  "Duration": 1009,
  "InterceptEvent": {
    "ClassName": "MyRepository",
    "MethodName": "InsertUserAsync",
    "IsAsync": true,
    "AsyncStatus": "RanToCompletion",
    "InstanceQualifiedName": "Audit.DynamicProxy.UnitTest.MyRepository, Audit.DynamicProxy.UnitTest, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null",
    "MethodSignature": "System.Threading.Tasks.Task`1[System.Int32] InsertUserAsync(System.String)",
    "Arguments": [
      {
        "Index": 0,
        "Name": "userName",
        "Type": "String",
        "Value": "thepirat000"
      }
    ],
    "Success": true,
    "Result": {
      "Type": "Task<Int32>",
      "Value": 142857
    }
  }
}
Failed async method call:
{
  "EventType": "MyRepository.InsertUserAsync",
  "Environment": {
    "UserName": "Federico",
    "MachineName": "HP",
    "DomainName": "HP",
    "CallingMethodName": "Audit.DynamicProxy.AuditInterceptor.Intercept()",
    "AssemblyName": "Audit.DynamicProxy, Version=4.5.2.0, Culture=neutral, PublicKeyToken=null",
    "Exception": "COMException: Exception from HRESULT: 0xE0434352",
    "Culture": "en-GB"
  },
  "StartDate": "2016-09-30T12:18:34.5093824-05:00",
  "EndDate": "2016-09-30T12:18:35.5388113-05:00",
  "Duration": 1029,
  "InterceptEvent": {
    "ClassName": "MyRepository",
    "MethodName": "InsertUserAsync",
    "IsAsync": true,
    "AsyncStatus": "Faulted",
    "InstanceQualifiedName": "Audit.DynamicProxy.UnitTest.MyRepository, Audit.DynamicProxy.UnitTest, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null",
    "MethodSignature": "System.Threading.Tasks.Task`1[System.Int32] InsertUserAsync(System.String)",
    "Arguments": [
      {
        "Index": 0,
        "Name": "userName",
        "Type": "String",
        "Value": null
      }
    ],
    "Success": false,
    "Exception": "(ArgumentNullException) UserName cannot be null",
    "Result": null
  }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Audit.DynamicProxy:

Repository Stars
thepirat000/Audit.NET
An extensible framework to audit executing operations in .NET and .NET Core.
Version Downloads Last updated
27.0.3 85 9/25/2024
27.0.2 88 9/19/2024
27.0.1 217 9/4/2024
27.0.0 86 9/3/2024
26.0.1 195 8/22/2024
26.0.0 133 7/19/2024
25.0.7 147 7/4/2024
25.0.6 130 6/24/2024
25.0.5 110 6/18/2024
25.0.4 461 3/24/2024
25.0.3 201 3/13/2024
25.0.2 124 3/12/2024
25.0.1 167 2/28/2024
25.0.0 163 2/16/2024
24.0.1 159 2/12/2024
24.0.0 112 2/12/2024
23.0.0 609 12/14/2023
22.1.0 152 12/9/2023
22.0.2 193 12/1/2023
22.0.1 228 11/16/2023
22.0.0 139 11/14/2023
21.1.0 2,766 10/9/2023
21.0.4 4,882 9/15/2023
21.0.3 1,676 7/9/2023
21.0.2 211 7/6/2023
21.0.1 547 5/27/2023
21.0.0 7,462 4/15/2023
20.2.4 1,329 3/27/2023
20.2.3 392 3/17/2023
20.2.2 319 3/14/2023
20.2.1 328 3/11/2023
20.2.0 352 3/7/2023
20.1.6 684 2/23/2023
20.1.5 3,157 2/9/2023
20.1.4 13,929 1/28/2023
20.1.3 403 12/21/2022
20.1.2 430 12/14/2022
20.1.1 444 12/12/2022
20.1.0 488 12/4/2022
20.0.4 430 11/30/2022
20.0.3 797 10/28/2022
20.0.2 539 10/26/2022
20.0.1 576 10/21/2022
20.0.0 656 10/1/2022
19.4.1 668 9/10/2022
19.4.0 600 9/2/2022
19.3.0 594 8/23/2022
19.2.2 639 8/11/2022
19.2.1 650 8/6/2022
19.2.0 738 7/24/2022
19.1.4 1,202 5/23/2022
19.1.3 593 5/22/2022
19.1.2 603 5/18/2022
19.1.1 752 4/28/2022
19.1.0 715 4/10/2022
19.0.7 771 3/13/2022
19.0.6 646 3/7/2022
19.0.5 723 1/28/2022
19.0.4 647 1/23/2022
19.0.3 685 12/14/2021
19.0.2 511 12/11/2021
19.0.1 946 11/20/2021
19.0.0 585 11/11/2021
19.0.0-rc.net60.2 180 9/26/2021
19.0.0-rc.net60.1 217 9/16/2021
18.1.6 1,029 9/26/2021
18.1.5 615 9/7/2021
18.1.4 538 9/6/2021
18.1.3 554 8/19/2021
18.1.2 649 8/8/2021
18.1.1 569 8/5/2021
18.1.0 611 8/1/2021
18.0.1 585 7/30/2021
18.0.0 599 7/26/2021
17.0.8 643 7/7/2021
17.0.7 1,800 6/16/2021
17.0.6 605 6/5/2021
17.0.5 640 5/28/2021
17.0.4 641 5/4/2021
17.0.3 604 5/1/2021
17.0.2 569 4/22/2021
17.0.1 598 4/18/2021
17.0.0 646 3/26/2021
16.5.6 638 3/25/2021
16.5.5 618 3/23/2021
16.5.4 627 3/9/2021
16.5.3 573 2/26/2021
16.5.2 581 2/23/2021
16.5.1 638 2/21/2021
16.5.0 558 2/17/2021
16.4.5 574 2/15/2021
16.4.4 542 2/5/2021
16.4.3 585 1/27/2021
16.4.2 652 1/22/2021
16.4.1 637 1/21/2021
16.4.0 640 1/11/2021
16.3.3 648 1/8/2021
16.3.2 656 1/3/2021
16.3.1 616 12/31/2020
16.3.0 566 12/30/2020
16.2.1 595 12/27/2020
16.2.0 842 10/13/2020
16.1.5 800 10/4/2020
16.1.4 997 9/17/2020
16.1.3 848 9/13/2020
16.1.2 702 9/9/2020
16.1.1 765 9/3/2020
16.1.0 733 8/19/2020
16.0.3 735 8/15/2020
16.0.2 755 8/9/2020
16.0.1 774 8/8/2020
16.0.0 662 8/7/2020
15.3.0 1,577 7/23/2020
15.2.3 735 7/14/2020
15.2.2 788 5/19/2020
15.2.1 767 5/12/2020
15.2.0 1,312 5/9/2020
15.1.1 787 5/4/2020
15.1.0 1,584 4/13/2020
15.0.5 3,049 3/18/2020
15.0.4 855 2/28/2020
15.0.3 761 2/26/2020
15.0.2 842 1/20/2020
15.0.1 869 1/10/2020
15.0.0 763 12/17/2019
14.9.1 820 11/30/2019
14.9.0 807 11/29/2019
14.8.1 819 11/26/2019
14.8.0 762 11/20/2019
14.7.0 800 10/9/2019
14.6.6 775 10/8/2019
14.6.5 780 9/27/2019
14.6.4 786 9/21/2019
14.6.3 926 8/12/2019
14.6.2 857 8/3/2019
14.6.1 753 8/3/2019
14.6.0 839 7/26/2019
14.5.7 843 7/18/2019
14.5.6 1,222 7/10/2019
14.5.5 827 7/1/2019
14.5.4 788 6/17/2019
14.5.3 855 6/5/2019
14.5.2 893 5/30/2019
14.5.1 873 5/28/2019
14.5.0 854 5/24/2019
14.4.0 1,550 5/22/2019
14.3.4 1,074 5/14/2019
14.3.3 849 5/9/2019
14.3.2 893 4/30/2019
14.3.1 890 4/27/2019
14.3.0 870 4/24/2019
14.2.3 874 4/17/2019
14.2.2 906 4/10/2019
14.2.1 923 4/5/2019
14.2.0 945 3/16/2019
14.1.1 890 3/8/2019
14.1.0 1,180 2/11/2019
14.0.4 2,413 1/31/2019
14.0.3 1,037 1/22/2019
14.0.2 963 12/15/2018
14.0.1 1,001 11/29/2018
14.0.0 1,044 11/19/2018
13.3.0 1,028 11/16/2018
13.2.2 1,032 11/15/2018
13.2.1 1,037 11/13/2018
13.2.0 1,092 10/31/2018
13.1.5 1,060 10/31/2018
13.1.4 1,049 10/25/2018
13.1.3 1,349 10/18/2018
13.1.2 1,171 9/12/2018
13.1.1 1,136 9/11/2018
13.1.0 1,106 9/11/2018
13.0.0 1,113 8/29/2018
12.3.6 1,150 8/29/2018
12.3.5 1,125 8/22/2018
12.3.4 1,185 8/21/2018
12.3.3 25,854 8/21/2018
12.3.2 1,110 8/20/2018
12.3.1 1,161 8/20/2018
12.3.0 1,153 8/20/2018
12.2.2 1,199 8/15/2018
12.2.1 1,189 8/9/2018
12.2.0 1,154 8/8/2018
12.1.11 1,117 7/30/2018
12.1.10 8,147 7/20/2018
12.1.9 1,239 7/10/2018
12.1.8 1,172 7/2/2018
12.1.7 1,348 6/7/2018
12.1.6 1,205 6/4/2018
12.1.5 1,221 6/2/2018
12.1.4 1,194 5/25/2018
12.1.3 1,440 5/16/2018
12.1.2 1,250 5/15/2018
12.1.1 1,307 5/14/2018
12.1.0 1,161 5/9/2018
12.0.7 1,372 5/5/2018
12.0.6 1,358 5/4/2018
12.0.5 1,345 5/3/2018
12.0.4 1,347 4/30/2018
12.0.3 1,181 4/30/2018
12.0.2 1,270 4/27/2018
12.0.1 1,151 4/25/2018
12.0.0 1,121 4/22/2018
11.2.0 1,321 4/11/2018
11.1.0 1,265 4/8/2018
11.0.8 1,296 3/26/2018
11.0.7 1,265 3/20/2018
11.0.6 1,219 3/7/2018
11.0.5 1,225 2/22/2018
11.0.4 1,271 2/14/2018
11.0.3 1,326 2/12/2018
11.0.2 1,198 2/9/2018
11.0.1 1,294 1/29/2018
11.0.0 1,640 1/15/2018
10.0.3 1,263 12/29/2017
10.0.2 1,291 12/26/2017
10.0.1 1,342 12/18/2017
10.0.0 1,198 12/18/2017
9.3.0 1,338 12/17/2017
9.2.0 1,324 12/17/2017
9.1.3 1,294 12/5/2017
9.1.2 1,292 11/27/2017
9.1.1 1,249 11/21/2017
9.1.0 1,237 11/21/2017
9.0.1 1,234 11/11/2017
9.0.0 1,275 11/10/2017
8.7.0 1,278 11/9/2017
8.6.0 1,248 11/9/2017
8.5.0 1,270 10/3/2017
8.4.0 1,267 10/3/2017
8.3.1 1,533 9/8/2017
8.3.0 1,311 9/8/2017
8.2.0 1,337 9/4/2017
8.1.0 1,340 8/22/2017
8.0.0 1,460 8/19/2017
7.1.3 1,311 8/14/2017
7.1.2 1,251 8/2/2017
7.1.1 1,240 7/26/2017
7.1.0 1,313 7/5/2017
7.0.9 1,333 6/28/2017
7.0.8 1,365 6/19/2017
7.0.6 1,384 4/7/2017
7.0.5 1,342 3/21/2017
7.0.4 1,303 3/21/2017
7.0.3 1,330 3/20/2017
7.0.2 1,313 3/13/2017
7.0.0 1,321 3/1/2017
6.2.0 1,414 2/25/2017
6.1.0 1,336 2/14/2017
6.0.0 1,464 2/9/2017
5.3.0 1,363 2/5/2017
5.2.0 1,315 1/26/2017
5.1.0 1,315 1/19/2017
5.0.0 1,315 1/7/2017
4.11.0 1,293 1/5/2017
4.10.0 1,316 12/31/2016
4.9.0 1,268 12/26/2016
4.8.0 1,278 12/17/2016
4.7.0 1,322 12/8/2016
4.6.5 1,334 12/4/2016
4.6.4 1,301 11/25/2016
4.6.2 1,299 11/18/2016
4.6.1 1,283 11/15/2016
4.6.0 1,297 11/11/2016
4.5.9 1,481 11/2/2016
4.5.8 1,354 11/2/2016
4.5.7 1,282 10/26/2016
4.5.6 1,247 10/6/2016
4.5.5 1,279 10/3/2016
4.5.4 1,260 10/2/2016
4.5.3 1,287 9/30/2016
4.5.2 1,326 9/28/2016
4.5.1 1,279 9/28/2016
4.5.0 1,302 9/28/2016