简体   繁体   English

如何向ASP.NET MVC3错误处理程序添加请求持续时间

[英]How to add request duration to ASP.NET MVC3 error handler

Code below is used in Global.asax to log error is ASP.NET MVC3 application. 下面的代码用于Global.asax来记录错误是ASP.NET MVC3应用程序。

Some errors are realated to timeouts reading data from PostgreSql server. 一些错误与从PostgreSql服务器读取数据的超时有关。

How to add request duration logging to this ? 如何将请求持续时间记录添加到此? Is there some propaerty which provides request start time in MVC3 ? 是否有一些propaerty在MVC3中提供请求开始时间?

I looked into samples in 我看了样品

http://www.codeproject.com/Articles/550510/Exception-Handling-and-NET http://www.codeproject.com/Articles/550510/Exception-Handling-and-NET

http://msdn.microsoft.com/en-us/library/24395wz3(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/24395wz3(v=vs.100).aspx

Global.asax - Application_Error - How can I get Page data? Global.asax - Application_Error - 如何获取页面数据?

http://www.velocityreviews.com/forums/t99686-global-asax-application_error-how-to-get-informations-about-the-exception.html http://www.velocityreviews.com/forums/t99686-global-asax-application_error-how-to-get-informations-about-the-exception.html

but havent found such sample. 但还没有找到这样的样本。

    void Application_Error(object sender, EventArgs e)
    {
        var sb = new StringBuilder();
        var url = HttpContext.Current.Request.Url;
        sb.AppendLine("Url " + url.ToString());
        foreach (var d in Request.Form.AllKeys)
                sb.AppendLine(d.ToString() + ":" + Request.Form[d].ToString());
        sb.AppendLine();
        foreach (var d in Request.Headers.AllKeys)
                sb.AppendLine(d.ToString() + "\t" + Request.Headers[d].ToString());
        Exception exception = Server.GetLastError();
        MyLogger.WriteException(exception, "Application_Error", sb.ToString());
    }

You can add request duration for the paths like this in config file 您可以在配置文件中为这样的路径添加请求持续时间

<location path="your path here">
<system.web>
    <httpRuntime executionTimeout="600" />
</system.web>

or you can set it into your controller also 或者你也可以将它设置到你的控制器中

HttpContext.Server.ScriptTimeout = timeout value here;

If by request, you are reffering to HTTP request, you can use Application_BeginRequest and Application_EndRequest in combination with ThreadStatic Stopwatch like this: 如果通过请求,您正在响应HTTP请求,您可以将Application_BeginRequest和Application_EndRequest与ThreadStatic Stopwatch结合使用,如下所示:

public class MvcApplication : System.Web.HttpApplication
{
    [ThreadStatic]
    private static Stopwatch stopwatch;

    protected void Application_Start()
    {
        //...
    }

    protected void Application_BeginRequest()
    {
        stopwatch = new Stopwatch();
        stopwatch.Start();
    }

    protected void Application_EndRequest()
    {
        stopwatch.Stop();
        var elapsedTime = stopwatch.ElapsedMilliseconds;
        //log time...
        stopwatch.Reset();
    }
}

And in your case, instead of Application_EndRequest, you can use 在您的情况下,您可以使用而不是Application_EndRequest

protected void Application_Error(object sender, EventArgs e)
{
    //...
    stopwatch.Stop();
    var time = stopwatch.ElapsedMilliseconds;
    stopwatch.Reset();
    //...
}

You should be aware of some problems when ThreadStatic : look here . 当ThreadStatic 时,你应该知道一些问题。 If you don't want to use ThreadStatic, you can use HttpContext.Current.Items like this: 如果您不想使用ThreadStatic,可以像这样使用HttpContext.Current.Items:

protected void Application_BeginRequest()
{
    var stopwatch = new Stopwatch();
    stopwatch = new Stopwatch();
    stopwatch.Start();
    HttpContext.Current.Items.Add("RequestStopwatch", stopwatch);
}

protected void Application_Error(object sender, EventArgs e)
{
    //...
    if (HttpContext.Current.Items["RequestStopwatch"] != null)
    {
        var stopwatch = (Stopwatch)HttpContext.Current.Items["RequestStopwatch"];
        stopwatch.Stop();
        var time = stopwatch.ElapsedMilliseconds;
        //log time...
        HttpContext.Current.Items.Remove("RequestStopwatch");
    }
    //...
}

You time it yourself. 你自己计时。 In a BeginRequest handler start a stopwatch then save it to HttpContext.Items. 在BeginRequest处理程序中,启动秒表,然后将其保存到HttpContext.Items。 Then in the error handler, get it back and stop it. 然后在错误处理程序中,将其取回并停止它。 Same approach for timeing the whole request, only you do the stopping part in EndRequest. 定时整个请求的方法相同,只有你在EndRequest中执行停止部分。

Take a look at this example , it's a http module which times the request then adds it to repsonse. 看看这个例子 ,它是一个http模块,它会将请求添加到repsonse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM