gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:05:22 28 4
gpt4 key购买 nike

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

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

关于c# - Audit.net Audit.MVC 输出到 json 而不是 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48889198/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com