Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DelegatingHandler中记录响应时间?_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在DelegatingHandler中记录响应时间?

C# 如何在DelegatingHandler中记录响应时间?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我有一个叫PerformanceHandler的委托人。我想记录所有传入请求的响应时间 我创建了一个秒表,并在SendAsync中启动它,然后使用以下命令返回它: 返回base.SendAsyncrequest、cancellationToken.ContinueWith。但是,当我尝试此操作时,所有请求似乎都不会返回,并且根本不会记录时间 我做错了什么?如果这段代码没有意义,那么在DelegatingHandler中记录请求时间的正确方法是什么 My Global.asax.cs包含: 我的P

我有一个叫PerformanceHandler的委托人。我想记录所有传入请求的响应时间

我创建了一个秒表,并在SendAsync中启动它,然后使用以下命令返回它: 返回base.SendAsyncrequest、cancellationToken.ContinueWith。但是,当我尝试此操作时,所有请求似乎都不会返回,并且根本不会记录时间

我做错了什么?如果这段代码没有意义,那么在DelegatingHandler中记录请求时间的正确方法是什么

My Global.asax.cs包含:

我的PerformanceHandler.cs代码:


我认为下面的修改会起作用

public class PerformanceHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            request.Properties.Add(new KeyValuePair<string, object>("Stopwatch", stopwatch));

            var response = await base.SendAsync(request, cancellationToken);

            Log(request, response);

            return response;

        }
   }
    public class PerformanceHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            request.Properties.Add(new KeyValuePair<string, object>("Stopwatch", stopwatch));

            return base.SendAsync(request, cancellationToken).ContinueWith(t =>
            {
                Log(request, t.Result);
                return t.Result;
            });

        }

        private void Log(HttpRequestMessage request, HttpResponseMessage response)
        {
            var requestMethod = request.Method.Method;
            var requestUri = request.RequestUri.ToString();

            var stopwatch = (Stopwatch)request.Properties["Stopwatch"];
            stopwatch.Stop();

            var responseTimeInMilliseconds = 1000; //stopwatch.ElapsedMilliseconds;

            int userObjectId = UserSession.Current().UserObjectId;

            System.Diagnostics.Debug.WriteLine("End of Request");

            //LOG TIME HERE
        }

   }
public class PerformanceHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            request.Properties.Add(new KeyValuePair<string, object>("Stopwatch", stopwatch));

            var response = await base.SendAsync(request, cancellationToken);

            Log(request, response);

            return response;

        }
   }