TrackTrace无法写入Application Insight求助:代码无追踪日志输出
Hey, let's figure out why your TrackTrace isn't showing up in Application Insights— I've dealt with this exact issue a few times, so here are the most common fixes to check:
1. Fix That Instrumentation Key Typo
Looking at your code, you're setting the Instrumentation Key in two places:
TelemetryConfiguration.Active.InstrumentationKey = "XXX-XXX-XXX";TelemetryClient TelePositive = new TelemetryClient { InstrumentationKey = "XXX-XXX" };
The second key is way too short! Application Insights Instrumentation Keys are full 36-character GUIDs, so "XXX-XXX" is definitely invalid. Pick one place to set the key (either the global configuration or the client) and make sure it's the exact key from your Azure Application Insights resource.
2. Add a Flush() Call (Critical for Short-Lived Processes)
By default, the TelemetryClient batches up telemetry and sends it in the background. If your app exits right after calling SAPLogger, that batch might never get sent. Add a Flush() to force the telemetry out immediately, and if it's a console app or short-lived process, add a small delay to let the send complete:
public static void SAPLogger(string Message) { // Use your full, valid Instrumentation Key here TelemetryConfiguration.Active.InstrumentationKey = "YOUR-VALID-GUID-KEY"; TelemetryClient TelePositive = new TelemetryClient(); TelePositive.TrackTrace(Message, SeverityLevel.Verbose, new Dictionary<string, string> { { "Information", "SAP" } }); TelePositive.Flush(); // This pushes the telemetry to Application Insights System.Threading.Thread.Sleep(1000); // Give it time to send (adjust if needed) }
3. Reuse Your TelemetryClient (Don't Create a New One Every Time)
Creating a new TelemetryClient in every call can cause unexpected behavior. It's better to initialize it once at app startup and reuse it:
// Initialize once at startup private static readonly TelemetryClient _telemetryClient = new TelemetryClient { InstrumentationKey = "YOUR-VALID-GUID-KEY" }; public static void SAPLogger(string Message) { _telemetryClient.TrackTrace(Message, SeverityLevel.Verbose, new Dictionary<string, string> { { "Information", "SAP" } }); _telemetryClient.Flush(); }
For web apps, even better—configure the key in appsettings.json and let the SDK handle initialization automatically:
{ "ApplicationInsights": { "InstrumentationKey": "YOUR-VALID-GUID-KEY" } }
4. Check if Application Insights is Filtering Verbose Traces
Even if you send Verbose traces, they might be filtered out in the portal. To verify:
- Go to your Application Insights resource in Azure
- Open the Logs section
- Run this Kusto query to look for your SAP traces:
traces | where customDimensions.Information == "SAP" | order by timestamp desc
If you don't see results, check your sampling settings—sometimes Verbose traces are sampled out by default. You can adjust this in your telemetry configuration to ensure all traces are captured.
5. Network/Firewall Blocking
Make sure your app has outbound access to https://dc.services.visualstudio.com/—this is the endpoint Application Insights uses to receive telemetry. If you're behind a corporate firewall, confirm this URL is allowed.
内容的提问来源于stack exchange,提问作者lokanath das

