简体   繁体   English

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

[英]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:嘿,这是我第一次使用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();
}

Which outputs to a .json file and looks like this:输出到 .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"
}

And that function above outputs a .json file into the IIS Express directory.上面的函数将 .json 文件输出到 IIS Express 目录中。 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.但是,我想要做的不是输出到文件,而是输出到代码中的字符串(或 json 对象),并允许我自己手动将该信息放在另一个区域(或数据库)中。

Would anyone know how to go about doing this?有谁知道如何去做这件事?

You can create your own AuditDataProvider:您可以创建自己的 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.您可以将它存储在内存缓存中或任何适合您的缓存中,而不是返回 auditEvent.ToJson()。

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.您将此行放在 Start_Application 事件中的 Global.asax 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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