Reputation: 73
I am new to the use of audit.net. I need to audit a series of custom values from the parameters received in the action method as follows.
[AuditField("User","reqLogin.User")]
[AuditCustom(EventTypeName = "AccesoAction")]
public async Task<IActionResult> Acceso(LoginRequest reqLogin)
{
...
}
I have to create a CustomField with FieldName "User" and value, the value of the User attribute of parameter reqLogin (reqLogin.User).
I'm extending the AuditAttribute class, to overwrite the OnActionExecutionAsync method and add the CustomField.
public class AuditCustomAttribute : AuditAttribute
{
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
lock (context.ActionDescriptor.Parameters)
{
foreach (AuditFieldAttribute apa in context.ActionDescriptor.Parameters.Cast<ControllerParameterDescriptor>().First().ParameterInfo.Member.GetCustomAttributes<AuditFieldAttribute>())
{
// create CustomField and add to context
}
}
return base.OnActionExecutionAsync(context, next);
}
}
Finally, in my custom AuditDataProvider class, I would audit the customFields that arrived in the auditevent field of method:
public override object InsertEvent(AuditEvent auditEvent)
How would it be possible? Thanks
Upvotes: 1
Views: 623
Reputation: 13114
It's not very clear what are you asking, but I guess you're using the Audit.MVC extension.
Why do you need to create a CustomField with information you already have on the ActionParameters?
Also I don't think you need to subclass the AuditAttribute
being that there is already a CustomAction mechanism you can use, for example:
Audit.Core.Configuration.AddOnCreatedAction(scope =>
{
var action = scope.GetMvcAuditAction();
var login = action.ActionParameters.FirstOrDefault(p => p.Key == "reqLogin").Value as LoginRequest;
scope.SetCustomField("User", login.User);
});
Upvotes: 1