gpt4 book ai didi

asp.net-web-api - 从 ExceptionLogger 引用操作参数

转载 作者:行者123 更新时间:2023-12-04 06:19:06 25 4
gpt4 key购买 nike

我想使用全局记录错误的新方法。我写了一个继承ExceptionLogger的类并覆盖 Log()方法。然后将其注册为替代品。

public class TraceExceptionLogger : ExceptionLogger
{
public async override void Log(ExceptionLoggerContext context)
{
// This is always empty string
var content = await context.Request.Content.ReadAsStringAsync();

// This is almost always null
var actionContext = context.ExceptionContext.ActionContext;
}
}

我可以挖掘 ExceptionLoggerContext对象的属性来获得几乎所有我需要的东西,除了 Action 参数。确实有 ActionContext属性,但我只看到它 null 和 this wiki page声明 ActionContextControllerContext几乎总是为空。

此外,我无法获取内容流,因为它的流在到达我的记录器之前已经被读取。所以我无法从请求的内容中获取任何已发布的 json。

有没有办法从 HttpContext.Current 获取发布的数据?或以其他方式?

最佳答案

好的,看起来我可以通过读取 Request 对象上的 InputStream 来从 HttpContext 获取正文文本,如下所示:

string bodyText = string.Empty;

using (var sr = new StreamReader(HttpContext.Current.Request.InputStream))
{
sr.BaseStream.Seek(0, SeekOrigin.Begin);
bodyText = sr.ReadToEnd();
}

到目前为止,此代码已成功获取我发布的 json 数据。

关于asp.net-web-api - 从 ExceptionLogger 引用操作参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22971395/

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