Audit.net Audit.MVC 输出到 json 而不是 json 文件
Posted
技术标签:
【中文标题】Audit.net Audit.MVC 输出到 json 而不是 json 文件【英文标题】:Audit.net Audit.MVC output to json and not json file 【发布时间】:2018-07-31 01:08:38 【问题描述】:嘿,这是我第一次使用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 对象),并允许我手动将该信息放在另一个区域(或数据库)中我自己。
有人知道怎么做吗?
【问题讨论】:
【参考方案1】:您可以创建自己的 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 中。
【讨论】:
感谢@Postlagerkarte 的回复。您能否举例说明我需要在哪里放置该代码? @StealthRT 请阅读 Audit.NET 数据提供者文档here 工作得很好,@Postlagerkarte!谢谢你! @StealthRT 谢谢!随意将我的答案标记为解决方案。以上是关于Audit.net Audit.MVC 输出到 json 而不是 json 文件的主要内容,如果未能解决你的问题,请参考以下文章