简体   繁体   中英

Audit.net Audit.MVC output to json and not json file

Hey all this is the first time I am using an thepirat000 Audit.MVC package and I have the base down and working just fine:

[Audit(EventTypeName = "InsertOrderAction", IncludeHeaders = true, IncludeModel = true)]
public ActionResult Index()
{
     var auditScope = this.GetCurrentAuditScope();

     auditScope.Comment("New comment from controller");
     auditScope.SetCustomField("TestField", Guid.NewGuid());

     return View();
}

Which outputs to a .json file and looks like this:

{
  "EventType": "InsertOrderAction",
  "Environment": {
    "UserName": "XXXXXXX",
    "MachineName": "XXXXXXX",
    "DomainName": "XXXXX",
    "CallingMethodName": "auditing.Controllers.HomeController.Index()",
    "AssemblyName": "auditing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
    "Culture": "en-US"
  },
  "Comments": [
    "New comment from controller"
  ],
  "StartDate": "2018-02-20T10:32:16.7258207-05:00",
  "EndDate": "2018-02-20T10:33:29.1468164-05:00",
  "Duration": 72421,
  "Action": {
    "HttpMethod": "GET",
    "ControllerName": "Home",
    "ActionName": "Index",
    "ViewName": "Index",
    "ViewPath": "~/Views/Home/Index.cshtml",
    "FormVariables": {},
    "ActionParameters": {},
    "UserName": "XXXX\\XXXXXXX",
    "RequestUrl": "/",
    "IpAddress": "127.0.0.1",
    "ResponseStatus": "200 OK",
    "ResponseStatusCode": 200,
    "Headers": {
      "Connection": "Keep-Alive",
      "Accept": "text/html, application/xhtml+xml, image/jxr, */*",
      "Accept-Encoding": "gzip, deflate",
      "Accept-Language": "en-US",
      "Authorization": "Negotiate oXcwdaADCgEBoloEWE5UTE1TU1AAAwAABBBBBBBBBAAAAAAAFgAAAAAAAAAWAAAAAAAAABYAAAAAAAAAFgAAAAAAAAAWAAAABXCiOIKADk4AAAA23456MS6IHLJk6i4sy2WPK09876QAAANlIQpc8OcBaAAAAAA==",
      "Cookie": "ai_user=PLRK2|2018-01-30T21:02:46.862Z",
      "Host": "localhost:29772",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
    },
    "ModelStateValid": true
  },
  "TestField": "c457b3a2-ff2f-460e-a6ed-c2d83904da07"
}

And that function above outputs a .json file into the IIS Express directory. However, what I am looking to do is not output to a file and just output to a string (or json object) within the code and allow me to place that information in another area (or database) manually myself.

Would anyone know how to go about doing this?

You can create your own AuditDataProvider:

public class MyCustomDataProvider : AuditDataProvider
{
    public override object InsertEvent(AuditEvent auditEvent)
    {
        return auditEvent.ToJson();
    }
}

Instead of returning the auditEvent.ToJson() you can store it in some in memory cache or whatever suits you.

Set this provider in the configuration:

Audit.Core.Configuration.DataProvider = new MyCustomDataProvider();

You put this line in your Global.asax within the Start_Application event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM