从ExceptionLogger引用操作参数 [英] Referencing Action Parameters from ExceptionLogger

查看:531
本文介绍了从ExceptionLogger引用操作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用新方法的全球记录错误。我已经写了继承 ExceptionLogger 并覆盖日志()方法的类。然后把它注册为一个替代品。

I'm wanting to make use of the new method for globally logging errors. I've written a class that inherits ExceptionLogger and overrides the Log() method. Then registered it as a replacement.

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 对象的属性挖掘得到pretty很多我需要的一切,除了动作参数。的确存在 ActionContext中属性,但我只看到它和空的此Wiki网页指出 ActionContext中 ControllerContext 几乎总是空

I can dig through the ExceptionLoggerContext object's properties to get pretty much everything I need, EXCEPT for action parameters. There is indeed an ActionContext property but I've only seen it null and this wiki page states that ActionContext and ControllerContext will almost always be null.

另外,我不能因为它的流已经阅读它得到我的记录之前获得内容流。因此,有没有办法让我获得请求的任何内容张贴JSON。

Also, I can't get the content stream because its stream is already read before it gets to my logger. So there's no way for me to get any posted json from the request's content.

有可能的方式获得 HttpContext.Current 已发布数据,或以其他方式?

Is there maybe a way to get the posted data from HttpContext.Current or in some other way?

推荐答案

好吧,它看起来像我可以通过Request对象的InputStream读这样得到的HttpContext正文:

Ok it looks like I can get the body text from HttpContext by reading InputStream on the Request object like this:

string bodyText = string.Empty;

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

这code已经成功我至今为让我张贴的JSON数据。

This code has been successful me so far for getting my posted json data.

这篇关于从ExceptionLogger引用操作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆