Search code examples
c#audit.net

Audit.Net MVC - How to solve the NullReferenceException?


We have been using the Audit.Net package in our system. It is very useful! However, we have run into an error when trying to implement it for a different environment/client. The auditing does work in a different Controller class, but in this one we get the error.

I think we are just missing something simple. enter image description here

Can anyone help us in the right direction?

This is the full method:

enter image description here


Solution

  • I guess the method UpdateAssesmentRequest is not the action method on the controller, but an auxiliary method.

    Audit.MVC needs the action method (or the controller class) to be decorated with [Audit] action filter attribute.

    So you have to decorate your MVC Action Method with the [Audit] attribute, not the auxiliary method. Then you should be able to get the AuditScope from any controller method.

    Note the GetCurrentAuditScope method tries to get the current audit scope from the HttpContext's items collection, like this:

    httpContext?.Items["__private_AuditScope__"] as AuditScope
    

    source code