Search code examples
audit.net

How to convert StartDate and EndDate to DateTimeZone in Event Audit .Net?


{ "EventType": "Order:Update", "Environment": { "UserName": "Federico", "MachineName": "HP", "DomainName": "HP", "CallingMethodName": "Audit.UnitTest.AuditTests.TestUpdate()", "Exception": null, "Culture": "en-GB" }, "Target": { "Type": "Order", "Old": { "OrderId": "39dc0d86-d5fc-4d2e-b918-fb1a97710c99", "Status": 2,

    },
    "New": {
        "OrderId": "39dc0d86-d5fc-4d2e-b918-fb1a97710c99",
        "Status": -1,
        
    }
},
"ReferenceId": "39dc0d86-d5fc-4d2e-b918-fb1a97710c99",           // <-- Custom Field
"Comments": ["Status Updated to Cancelled"],                     // <-- Comments
"StartDate": "2016-08-23T11:34:44.656101-05:00",
"EndDate": "2016-08-23T11:34:55.1810821-05:00",
"Duration": 8531

}


Solution

  • You can't change the type of the AuditEvent properties, but you can add custom fields to the audit event with custom actions.

    For example:

    Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
    {
        scope.SetCustomField("StartDateTimeZone", GetCurrentDate());
    });
    
    Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, scope =>
    {
        scope.SetCustomField("EndDateTimeZone", GetCurrentDate());
    });