1

In the Environment property of Audit.NET's output none of the properties (DomainName, MachineName, etc) are really useful to us and take a lot of extra space in our audit database.

To remove them I've found I can either do this:

auditScope.Event.Environment = new AuditEventEnvironment()

...or I can put a custom ContractResolver on Audit.Core.Configuration.JsonSettings.ContractResolver that skips the Environment property.

But is there a better way to disable the Environment property?

x5657
  • 1,172
  • 2
  • 13
  • 26
  • 1
    The library does not provide a method to explicitly remove the Environment property. Those are the options you have, the first one can be done on a [custom action](https://github.com/thepirat000/Audit.NET#custom-actions). – thepirat000 Jan 08 '20 at 23:16

1 Answers1

1

Of the options mentioned in the question, this is the only one that completely removes Environment from the serialised audit entries. The other option serialises to an empty Environment object (and if Environment is set to null then you get a NullReferenceException).

In ConfigureServices set the ContractResolver to EnvironmentSkippingContractResolver:

Audit.Core.Configuration.JsonSettings.ContractResolver = EnvironmentSkippingContractResolver.Instance;

EnvironmentSkippingContractResolver looks like this:

public class EnvironmentSkippingContractResolver : DefaultContractResolver
{
    public static readonly EnvironmentSkippingContractResolver Instance = new EnvironmentSkippingContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);
        property.ShouldSerialize = instance => member.Name != "Environment";
        return property;
    }
}
x5657
  • 1,172
  • 2
  • 13
  • 26