C# Web API错误日志记录

C# Web API错误日志记录,c#,error-handling,asp.net-web-api2,C#,Error Handling,Asp.net Web Api2,我有一个Web API项目,并添加了一个自定义异常过滤器。在这个异常过滤器中,我想记录客户端发布到服务器的JSON数据。不过,我似乎无法访问它。有人能帮我找到我可以访问的对象吗 ActionExecuteContext.ActionContext.Request.Content似乎为空,但在我的模拟场景中,我知道发布了有效数据 public class MobileAppExceptionFilterAttribute : ExceptionFilterAttribute { publi

我有一个Web API项目,并添加了一个自定义异常过滤器。在这个异常过滤器中,我想记录客户端发布到服务器的JSON数据。不过,我似乎无法访问它。有人能帮我找到我可以访问的对象吗

ActionExecuteContext.ActionContext.Request.Content似乎为空,但在我的模拟场景中,我知道发布了有效数据

public class MobileAppExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override async void OnException( HttpActionExecutedContext actionExecutedContext )
    {
        var sb = new StringBuilder();

        sb.Append( $"Url:{actionExecutedContext.Request.RequestUri}" );

        foreach(var header in actionExecutedContext.Request.Headers)
        {
            sb.Append( $"Header:{header.Key} - {String.Join( ", ", header.Value )}" );
        }...
   }
}

在尝试访问之前,您应该检查是否确实存在要读取的内容

public class MobileAppExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override async void OnException( HttpActionExecutedContext actionExecutedContext )
    {
        var sb = new StringBuilder();
        var request = actionExecutedContext.Request;
        sb.Append( $"Url:{request.RequestUri}" );

        foreach(var header in request.Headers)
        {
            sb.Append( $"Header:{header.Key} - {String.Join( ", ", header.Value )}" );
        }

        if(request.Method == HttpMethod.Post && request.Headers.ContentLength > 0) {
            var content = await request.Content.ReadAsStringAsync();
            sb.Append( $"Content:{content}"); 
        } 
   }
}

在尝试访问之前,您应该检查是否确实存在要读取的内容

public class MobileAppExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override async void OnException( HttpActionExecutedContext actionExecutedContext )
    {
        var sb = new StringBuilder();
        var request = actionExecutedContext.Request;
        sb.Append( $"Url:{request.RequestUri}" );

        foreach(var header in request.Headers)
        {
            sb.Append( $"Header:{header.Key} - {String.Join( ", ", header.Value )}" );
        }

        if(request.Method == HttpMethod.Post && request.Headers.ContentLength > 0) {
            var content = await request.Content.ReadAsStringAsync();
            sb.Append( $"Content:{content}"); 
        } 
   }
}

您可以这样尝试,看看是否可以获得json:

   string RequestContent = "";

  RequestContent = Extract_Json_From_Request(actionContext.Request.Content);

  private string Extract_Json_From_Request(HttpContent Requestcontent) 
        {
            string RequestBody = "";

            try
            {
                var content = Requestcontent.ReadAsStringAsync().Result;
                RequestBody = content.ToString();
            }
            catch (Exception ex)
            {
                RequestBody = ex.Message;

            }
            return RequestBody;

        }

您可以这样尝试,看看是否可以获得json:

   string RequestContent = "";

  RequestContent = Extract_Json_From_Request(actionContext.Request.Content);

  private string Extract_Json_From_Request(HttpContent Requestcontent) 
        {
            string RequestBody = "";

            try
            {
                var content = Requestcontent.ReadAsStringAsync().Result;
                RequestBody = content.ToString();
            }
            catch (Exception ex)
            {
                RequestBody = ex.Message;

            }
            return RequestBody;

        }

检查POST的请求
request.Method
,然后检查
request.Content
,当你说
似乎是空的时候。
这是什么意思?属性是
null
?当我进行“快速观察”时,虽然它说它有:{Content Length:3156 Content Type:application/json}这是它的实际内容:actionExecutedContext.Request.Content.ReadAsStringAsync()Id=382,Status=RanToCompletion,Method=“{null}”,Result=“”System.Threading.Tasks.Task
actionExecutedContext.Request.Content.ReadAsStringAsync()
不带
等待
直到返回一个
任务
,这就是您在上一条评论中描述的内容。等待将内容转换为可读字符串。如果您不打算使用
await
,请使用
actionExecutedContext.Request.Content.ReadAsStringAsync().Result
我尝试了Result和await,它们都返回空字符串。列出的结果是来自quick watch的,它显示了它是如何空的。检查POST的请求
request.Method
,然后检查
请求。当你说
时,内容似乎是空的。
这是什么意思?属性是
null
?当我进行“快速观察”时,虽然它说它有:{Content Length:3156 Content Type:application/json}这是它的实际内容:actionExecutedContext.Request.Content.ReadAsStringAsync()Id=382,Status=RanToCompletion,Method=“{null}”,Result=“”System.Threading.Tasks.Task
actionExecutedContext.Request.Content.ReadAsStringAsync()
不带
等待
直到返回一个
任务
,这就是您在上一条评论中描述的内容。等待将内容转换为可读字符串。如果您不打算使用
await
,请使用
actionExecutedContext.Request.Content.ReadAsStringAsync().Result
我尝试了Result和await,它们都返回空字符串。所列出的结果来自于快速观察,它显示了它是如何空的。哇,这就像一个符咒,它与所有其他似乎带有空内容的方法有何不同?哇,这就像一个符咒,它与所有其他似乎带有空内容的方法有何不同?