Skip to content

Commit 408d26c

Browse files
committed
Update example code for Snapshot Debugger
1 parent f320d42 commit 408d26c

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

articles/azure-monitor/snapshot-debugger/snapshot-debugger-vm.md

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,75 @@ builder.Services.AddSnapshotCollector(config => config.IsEnabledInDeveloperMode
124124
Snapshots are collected only on exceptions that are reported to Application Insights. For ASP.NET and ASP.NET Core applications, the Application Insights SDK automatically reports unhandled exceptions that escape a controller method or endpoint route handler. For other applications, you might need to modify your code to report them. The exception handling code depends on the structure of your application. Here's an example:
125125

126126
```csharp
127-
TelemetryClient _telemetryClient = new TelemetryClient();
128-
void ExampleRequest()
127+
using Microsoft.ApplicationInsights;
128+
using Microsoft.ApplicationInsights.DataContracts;
129+
using Microsoft.ApplicationInsights.Extensibility;
130+
131+
namespace AppInsightsWorkerServiceExample;
132+
133+
internal class ExampleService
134+
{
135+
private readonly TelemetryClient _telemetryClient;
136+
137+
public ExampleService(TelemetryClient telemetryClient)
138+
{
139+
// Obtain the TelemetryClient via dependency injection.
140+
_telemetryClient = telemetryClient;
141+
}
142+
143+
public void HandleExampleRequest()
144+
{
145+
using IOperationHolder<RequestTelemetry> operation =
146+
_telemetryClient.StartOperation<RequestTelemetry>("Example");
147+
try
148+
{
149+
// TODO: Handle the request.
150+
operation.Telemetry.Success = true;
151+
}
152+
catch (Exception ex)
153+
{
154+
// Report the exception to Application Insights.
155+
operation.Telemetry.Success = false;
156+
_telemetryClient.TrackException(ex);
157+
// TODO: Rethrow the exception if desired.
158+
}
159+
}
160+
}
161+
```
162+
163+
Below, is another example using `ILogger`. In this case, when handling an exception, be sure to pass the exception as the first parameter to `LogError`.
164+
165+
```csharp
166+
using Microsoft.Extensions.Logging;
167+
168+
internal class LoggerExample
129169
{
170+
private readonly ILogger _logger;
171+
172+
public LoggerExample(ILogger<LoggerExample> logger)
173+
{
174+
_logger = logger;
175+
}
176+
177+
public void HandleExampleRequest()
178+
{
179+
using IDisposable scope = _logger.BeginScope("Example");
130180
try
131181
{
132-
// TODO: Handle the request.
182+
// TODO: Handle the request
133183
}
134184
catch (Exception ex)
135185
{
136-
// Report the exception to Application Insights.
137-
_telemetryClient.TrackException(ex);
138-
// TODO: Rethrow the exception if desired.
186+
// Use the LogError overload with an Exception as the first parameter.
187+
_logger.LogError(ex, "An error occurred.");
139188
}
189+
}
140190
}
141191
```
142192

193+
> [!NOTE]
194+
> By default, the Application Insights Logger (`ApplicationInsightsLoggerProvider`) forwards exceptions to the Snapshot Debugger via `TelemetryClient.TrackException`. This behavior is controlled via the `TrackExceptionsAsExceptionTelemetry` property on the `ApplicationInsightsLoggerOptions` class. If you set `TrackExceptionsAsExceptionTelemetry` to `false` when configuring the Application Insights Logger, then the example above will not trigger the Snapshot Debugger. In this case, modify your code to call `TrackException` manually.
195+
143196
[!INCLUDE [azure-monitor-log-analytics-rebrand](../../../includes/azure-monitor-instrumentation-key-deprecation.md)]
144197

145198
## Next steps

0 commit comments

Comments
 (0)