嘿,这是我第一次使用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目录中。但是,我要执行的操作不是输出到文件中,而是输出到代码中的字符串(或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中。

10-01 09:23