使用Microsoft.ApplicationInsights 2.1.0.0时服务器响应时间未记录求助
Hey there! Let's break down why you're not seeing the Server Response Time metric in your Azure Portal, even though other request-related metrics are showing up. Based on your code and setup, here are the key areas to check:
1. Fix the TrackRequest Timestamp Parameter Type
Looking at your LogHttpRequest method, you’re passing a DateTime for the request start time—but the 2.1.0.0 version of TelemetryClient.TrackRequest expects a DateTimeOffset for this parameter.
DateTime lacks timezone information, which can cause Application Insights to misinterpret request timing and fail to calculate Server Response Time correctly. Update your method signature to use DateTimeOffset:
public void LogHttpRequest(string Name, DateTimeOffset CreatedDateTime, TimeSpan Duration, string ResponseCode, bool flag) { TelemetryClient.TrackRequest(Name, CreatedDateTime, Duration, ResponseCode, flag); }
Make sure you pass a valid DateTimeOffset (like DateTimeOffset.UtcNow for the request start time) to ensure accurate timing tracking.
2. Verify the Duration Value is Correct
Server Response Time is directly derived from the Duration parameter in TrackRequest. Double-check that this TimeSpan represents the full actual duration of request processing—from when the request was received to when the response was sent.
For example, in an ASP.NET context, calculate it like this:
var startTime = DateTimeOffset.UtcNow; // ... process the request ... var duration = DateTimeOffset.UtcNow - startTime; LogHttpRequest("MyRequest", startTime, duration, "200", true);
If Duration is set to an invalid value (like TimeSpan.Zero or an arbitrary number), the Server Response Time metric won’t populate.
3. Ensure Request Tracking Configuration is Enabled
Even if you’re using manual TrackRequest calls, enabling the built-in request tracking module ensures consistent metadata is attached to telemetry, which helps surface all related metrics.
Check your ApplicationInsights.config to confirm the module is present:
<TelemetryModules> <Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.ApplicationInsights.Web" /> <!-- Other modules --> </TelemetryModules>
If you’re initializing telemetry programmatically (without the config file), add the module explicitly:
var requestModule = new RequestTrackingTelemetryModule(); requestModule.Initialize(TelemetryConfiguration.Active);
4. Inspect Raw Request Telemetry to Confirm
To pinpoint the issue, go to your Application Insights resource in the Azure Portal:
- Navigate to Search under the Investigate section
- Filter for
Requesttelemetry items - Open a request item and check if the
Durationfield has a valid, non-zero value
If Duration is missing or zero, the problem is definitely in how you’re passing this parameter in your LogHttpRequest method.
5. Optional: Upgrade the SDK Version
While 2.1.0.0 is functional, it’s an older version of the SDK. Newer releases include bug fixes and improved metric handling. If the above steps don’t resolve the issue, upgrading to a more recent version (after testing for compatibility) might help.
内容的提问来源于stack exchange,提问作者user145610

