Audit.net Audit.MVC 输出到 json 而不是 json 文件

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

嘿,这是我第一次使用 thepirat000 Audit.MVC 软件包,我已经放下底座并且工作正常:

[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();
}

输出到 .json 文件,如下所示:

{
  "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"
}

上面的函数将 .json 文件输出到 IIS Express 目录中。但是,我想要做的是 not 输出到文件,只是输出到代码中的字符串(或 json 对象),并允许我将该信息放在另一个区域(或数据库)我自己手动。

有人知道怎么做吗?

您可以创建自己的 AuditDataProvider:

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

而不是返回 auditEvent.ToJson() 你可以将它存储在一些内存缓存或任何适合你的地方。

在配置中设置此提供程序:

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

您将此行放在 Start_Application 事件中的 Global.asax 中。