阻止 Audit.Net 将 BodyContent 值格式化为字符串?
Prevent Audit.Net from formatting BodyContent values as strings?
我越来越熟悉 audit.net,发现序列化工作不太正常。我想知道是否需要添加更多配置。我目前的配置如下:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// ... configuration here
Audit.Core.Configuration.Setup().UseFileLogProvider("./logs"); // issue also happens when using SqlServer
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next();
});
app.UseAuditMiddleware(_ => _
.IncludeHeaders()
.IncludeResponseHeaders()
.IncludeRequestBody()
.IncludeResponseBody(ctx => ctx.Response.StatusCode == 200));
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我得到的结果 (带转义字符的字符串):
"ResponseBody": {
"Type": "application/json; charset=utf-8",
"Value": "[{\"date\":\"2021-04-22T12:39:31.173043-06:00\",\"temperatureC\":4,\"temperatureF\":39,\"summary\":\"Balmy\"},{\"date\":\"2021-04-23T12:39:31.1734617-06:00\",\"temperatureC\":34,\"temperatureF\":93,\"summary\":\"Balmy\"},{\"date\":\"2021-04-24T12:39:31.1734642-06:00\",\"temperatureC\":-8,\"temperatureF\":18,\"summary\":\"Cool\"},{\"date\":\"2021-04-25T12:39:31.1734646-06:00\",\"temperatureC\":32,\"temperatureF\":89,\"summary\":\"Mild\"},{\"date\":\"2021-04-26T12:39:31.1734647-06:00\",\"temperatureC\":-2,\"temperatureF\":29,\"summary\":\"Mild\"}]"
},
我希望得到什么(对象):
"ResponseBody": {
"Type": "application/json; charset=utf-8",
"Value": [
{
"date": "2021-04-22T12:39:31.173043-06:00",
"temperatureC": 4,
"temperatureF": 39,
"summary": "Balmy"
},
{
// more temperatures
},
...,
]
因为中间件只能访问已经序列化为字节数组的响应体,所以只能解码为字符串。
另一方面,action filter可以在对象写入响应流之前访问该对象,因此如果您也 将其添加到管道中,请求到达操作方法的审计事件将如您所期望的那样。
例如,在您的 Startup
class 上:
using Audit.WebApi;
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc(mvcOptions =>
{
mvcOptions.AddAuditFilter(a => a
.LogAllActions()
.IncludeResponseHeaders()
.IncludeRequestBody()
.IncludeResponseBody());
});
}
查看 this 了解有关同时配置中间件和操作过滤器的更多信息。
如果您不想或不能添加操作过滤器,您可以在使用自定义操作保存之前修改审核事件。例如,同样在您的 Startup
class:
using Newtonsoft.Json;
Audit.Core.Configuration.AddOnSavingAction(scope =>
{
var audit = scope.GetWebApiAuditAction();
if (audit?.ResponseBody?.Type.Contains("application/json") == true)
{
audit.ResponseBody.Value = JsonConvert.DeserializeObject(audit.ResponseBody.Value.ToString());
}
});
我越来越熟悉 audit.net,发现序列化工作不太正常。我想知道是否需要添加更多配置。我目前的配置如下:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// ... configuration here
Audit.Core.Configuration.Setup().UseFileLogProvider("./logs"); // issue also happens when using SqlServer
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next();
});
app.UseAuditMiddleware(_ => _
.IncludeHeaders()
.IncludeResponseHeaders()
.IncludeRequestBody()
.IncludeResponseBody(ctx => ctx.Response.StatusCode == 200));
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我得到的结果 (带转义字符的字符串):
"ResponseBody": {
"Type": "application/json; charset=utf-8",
"Value": "[{\"date\":\"2021-04-22T12:39:31.173043-06:00\",\"temperatureC\":4,\"temperatureF\":39,\"summary\":\"Balmy\"},{\"date\":\"2021-04-23T12:39:31.1734617-06:00\",\"temperatureC\":34,\"temperatureF\":93,\"summary\":\"Balmy\"},{\"date\":\"2021-04-24T12:39:31.1734642-06:00\",\"temperatureC\":-8,\"temperatureF\":18,\"summary\":\"Cool\"},{\"date\":\"2021-04-25T12:39:31.1734646-06:00\",\"temperatureC\":32,\"temperatureF\":89,\"summary\":\"Mild\"},{\"date\":\"2021-04-26T12:39:31.1734647-06:00\",\"temperatureC\":-2,\"temperatureF\":29,\"summary\":\"Mild\"}]"
},
我希望得到什么(对象):
"ResponseBody": {
"Type": "application/json; charset=utf-8",
"Value": [
{
"date": "2021-04-22T12:39:31.173043-06:00",
"temperatureC": 4,
"temperatureF": 39,
"summary": "Balmy"
},
{
// more temperatures
},
...,
]
因为中间件只能访问已经序列化为字节数组的响应体,所以只能解码为字符串。
另一方面,action filter可以在对象写入响应流之前访问该对象,因此如果您也 将其添加到管道中,请求到达操作方法的审计事件将如您所期望的那样。
例如,在您的 Startup
class 上:
using Audit.WebApi;
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc(mvcOptions =>
{
mvcOptions.AddAuditFilter(a => a
.LogAllActions()
.IncludeResponseHeaders()
.IncludeRequestBody()
.IncludeResponseBody());
});
}
查看 this 了解有关同时配置中间件和操作过滤器的更多信息。
如果您不想或不能添加操作过滤器,您可以在使用自定义操作保存之前修改审核事件。例如,同样在您的 Startup
class:
using Newtonsoft.Json;
Audit.Core.Configuration.AddOnSavingAction(scope =>
{
var audit = scope.GetWebApiAuditAction();
if (audit?.ResponseBody?.Type.Contains("application/json") == true)
{
audit.ResponseBody.Value = JsonConvert.DeserializeObject(audit.ResponseBody.Value.ToString());
}
});