Azure应用程序:如何将POSTed JSON记录到我的应用程序?

时间:2017-09-25 16:27:21

标签: azure azure-web-sites

我去了我的App服务>我的应用>监控>诊断日志

我同时启用了Application Logging (Blob)Web server logging (Storage)

以上设置开始记录.log文件,其中每一行都是对我的应用程序的HTTP请求,包含以下标题:

#Fields: date time s-sitename cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken

我有兴趣记录服务器响应HTTP 500的请求的POSTed JSON。如何从Azure门户实现此目的?

2 个答案:

答案 0 :(得分:0)

不清楚您使用什么来编写应用程序,但如果您使用.net,则需要使用System.Diagnostics.Trace命名空间。

在这种情况下,因为你得到一个5XX的http错误,我认为有一些异常被抛出,缺少一个try / catch。

一旦找到,就会添加如下内容:

System.Diagnostics.Trace.TraceError("如果你看到这个,发生了一些不好的事情" + postedJSON);

这将导致json被添加到应用程序日志中。

您可以在此处获取有关App Service日志记录的更多信息:https://docs.microsoft.com/en-us/azure/app-service/web-sites-enable-diagnostic-log

答案 1 :(得分:0)

  

如何从Azure门户网站实现这一目标?

Azure Web App不提供此功能的默认设置。我建议你为应用程序级异常添加一个事件处理程序。在此处理程序中,您可以记录请求正文或异常信息。以下代码供您参考。

protected void Application_Error(object sender, EventArgs e)
{
    string req_JSON;
    using (StreamReader reader = new StreamReader(Request.InputStream))
    {
        req_JSON = reader.ReadToEnd();
    }
    //you could log the json to anywhere you want 

    Exception excpt = Server.GetLastError().GetBaseException();
    //you also could log the exception which encounted at server
}