-1

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

Igavshne
  • 661
  • 6
  • 29
  • The error indicates that `this.GetCurrentAuditScope()` is null. The method `GetCurrentAuditScope()` is returning null and that's what's causing the `NullReferenceException`.The problem is there. – Nicolás de Ory Mar 30 '20 at 12:23
  • 1
    Please add code and errors as text to the question, instead of using images of text. – phuzi Mar 30 '20 at 13:25
  • @phuzi, The code is not on my machine. These are the screenprints my colleague sent me. I can't get access to the environment due to red-tape and my country's current lockdown. – Igavshne Mar 30 '20 at 20:23
  • @NicodeOry yes, but why is the AuditScope (that should be returned by GetCurrentAuditScope) null? I thought the AuditScope is created automatically in Audit.MVC? – Igavshne Mar 30 '20 at 20:25
  • 1
    Share at least the stack trace, otherwise we can only guess... Is that method `UpdateAssesmentRequest` the action method on the controller? I don't think so. You have to decorate the MVC Action Method (or the controller) with the `[Audit]` attribute. – thepirat000 Mar 31 '20 at 03:18
  • 1
    @thepirat000, Aha! Yes that must be it! I'll ask my colleague to put it on the action methods. UpdateAssessmentRequest seems like a auxiliary method. – Igavshne Mar 31 '20 at 07:00
  • If your colleague can send you screen shots, can't they send you the text?! – phuzi Mar 31 '20 at 07:13
  • @phuzi, sure, but they were probably asleep. I work as a contractor for the company, so often I work late at night or early in the morning, - whenever it suits me. I would have asked him for the text this morning, but then I saw thepirat000's comment and could conclude what the problem was. – Igavshne Mar 31 '20 at 09:48

1 Answers1

0

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

thepirat000
  • 10,774
  • 4
  • 38
  • 63