如何跟踪和记录性能并调用我的 Web API 请求?
How can I trace and log performance and call on my Web API requests?
我刚刚继承了一个非常糟糕的 C# Web API 代码。我需要改进它,但我不知道从哪里开始。有很多 类 代码是一场噩梦。我还认为部分代码从未使用过。
我想统计每个请求被调用的次数以及每个请求最后调用的时间。我想将所有结果记录在 table 中。在一两天内,我可以清楚地了解我最常使用的内容以及需要时间的内容。客户对此表示抱怨。
我使用 Nlog 作为日志,使用简单的秒表来计算性能。你有什么建议?我知道最好对跟踪使用过滤器和属性,但如何记录和跟踪这样的性能?
// GET api/Item/5
public Item GetItem(string id)
{
// I don't use string.Format in my real code. This is just for this example.
logger.Trace(string.Format("Request: GET api/Item/{0}", id));
Stopwatch sw = new Stopwatch();
sw.Start();
Item item = context.Items.Single(i => i.Code == id);
sw.Stop();
logger.Trace(string.Format("Response: GET api/values/{0}\r\nElpased={1}\r\rn{2}", id, sw.Elapsed, response));
我在这里找到了以前的信息
感谢
http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi
Exact time measurement for performance testing
当然,所有这些跟踪和性能作业都将从我的配置文件中激活或停用。对于 NLog,它是内置的。对于 Stopwatch,我计划构建一些东西。我不打算在生产中一直保持这种状态
看看分析器。这种更通用的方法。我们使用 Ants performance profiler。
您可以使用 DelegatingHandler.
A base type for HTTP handlers that delegate the processing of HTTP response messages to another handler, called the inner handler.
在管道中注入允许捕获控制器中请求处理前后的位置。
public class MonitoringDelegate : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var watcher = Stopwatch.StartNew();
var response = await base.SendAsync(request, cancellationToken);
watcher.Stop();
//log/store duration using watcher.ElapsedMilliseconds
return response;
}
}
这是记录性能和操作的更好方法:http://arcware.net/logging-web-api-requests/
请求时间和响应时间的时间差就是经过的时间。
我刚刚继承了一个非常糟糕的 C# Web API 代码。我需要改进它,但我不知道从哪里开始。有很多 类 代码是一场噩梦。我还认为部分代码从未使用过。
我想统计每个请求被调用的次数以及每个请求最后调用的时间。我想将所有结果记录在 table 中。在一两天内,我可以清楚地了解我最常使用的内容以及需要时间的内容。客户对此表示抱怨。
我使用 Nlog 作为日志,使用简单的秒表来计算性能。你有什么建议?我知道最好对跟踪使用过滤器和属性,但如何记录和跟踪这样的性能?
// GET api/Item/5
public Item GetItem(string id)
{
// I don't use string.Format in my real code. This is just for this example.
logger.Trace(string.Format("Request: GET api/Item/{0}", id));
Stopwatch sw = new Stopwatch();
sw.Start();
Item item = context.Items.Single(i => i.Code == id);
sw.Stop();
logger.Trace(string.Format("Response: GET api/values/{0}\r\nElpased={1}\r\rn{2}", id, sw.Elapsed, response));
我在这里找到了以前的信息
感谢
http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi
Exact time measurement for performance testing
当然,所有这些跟踪和性能作业都将从我的配置文件中激活或停用。对于 NLog,它是内置的。对于 Stopwatch,我计划构建一些东西。我不打算在生产中一直保持这种状态
看看分析器。这种更通用的方法。我们使用 Ants performance profiler。
您可以使用 DelegatingHandler.
A base type for HTTP handlers that delegate the processing of HTTP response messages to another handler, called the inner handler.
在管道中注入允许捕获控制器中请求处理前后的位置。
public class MonitoringDelegate : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var watcher = Stopwatch.StartNew();
var response = await base.SendAsync(request, cancellationToken);
watcher.Stop();
//log/store duration using watcher.ElapsedMilliseconds
return response;
}
}
这是记录性能和操作的更好方法:http://arcware.net/logging-web-api-requests/
请求时间和响应时间的时间差就是经过的时间。