Skip to content

Commit dd5faef

Browse files
authored
Merge pull request #271288 from pharring/main
Update sample code for Snapshot Debugger
2 parents a9e9696 + e105b82 commit dd5faef

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ using Microsoft.ApplicationInsights.SnapshotCollector;
9494
builder.Services.Configure<SnapshotCollectorConfiguration>(builder.Configuration.GetSection("SnapshotCollector"));
9595
```
9696

97-
Next, add a `SnapshotCollector` section to *appsettings.json* where you can override the defaults. The following example shows a configuration equivalent to the default configuration:
97+
Next, add a `SnapshotCollector` section to _appsettings.json_ where you can override the defaults. The following example shows a configuration equivalent to the default configuration:
9898

9999
```json
100100
{
@@ -114,7 +114,7 @@ Next, add a `SnapshotCollector` section to *appsettings.json* where you can over
114114
}
115115
```
116116

117-
If you need to customize the Snapshot Collector's behavior manually, without using *appsettings.json*, use the overload of `AddSnapshotCollector` that takes a delegate. For example:
117+
If you need to customize the Snapshot Collector's behavior manually, without using _appsettings.json_, use the overload of `AddSnapshotCollector` that takes a delegate. For example:
118118
```csharp
119119
builder.Services.AddSnapshotCollector(config => config.IsEnabledInDeveloperMode = true);
120120
```
@@ -124,26 +124,77 @@ 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+
internal class ExampleService
129132
{
133+
private readonly TelemetryClient _telemetryClient;
134+
135+
public ExampleService(TelemetryClient telemetryClient)
136+
{
137+
// Obtain the TelemetryClient via dependency injection.
138+
_telemetryClient = telemetryClient;
139+
}
140+
141+
public void HandleExampleRequest()
142+
{
143+
using IOperationHolder<RequestTelemetry> operation =
144+
_telemetryClient.StartOperation<RequestTelemetry>("Example");
130145
try
131146
{
132-
// TODO: Handle the request.
147+
// TODO: Handle the request.
148+
operation.Telemetry.Success = true;
133149
}
134150
catch (Exception ex)
135151
{
136-
// Report the exception to Application Insights.
137-
_telemetryClient.TrackException(ex);
138-
// TODO: Rethrow the exception if desired.
152+
// Report the exception to Application Insights.
153+
operation.Telemetry.Success = false;
154+
_telemetryClient.TrackException(ex);
155+
// TODO: Rethrow the exception if desired.
139156
}
157+
}
140158
}
141159
```
142160

161+
The following example uses `ILogger` instead of `TelemetryClient`. This example assumes you're using the [Application Insights Logger Provider](../app/ilogger.md#console-application). As the example shows, when handling an exception, be sure to pass the exception as the first parameter to `LogError`.
162+
163+
```csharp
164+
using Microsoft.Extensions.Logging;
165+
166+
internal class LoggerExample
167+
{
168+
private readonly ILogger _logger;
169+
170+
public LoggerExample(ILogger<LoggerExample> logger)
171+
{
172+
_logger = logger;
173+
}
174+
175+
public void HandleExampleRequest()
176+
{
177+
using IDisposable scope = _logger.BeginScope("Example");
178+
try
179+
{
180+
// TODO: Handle the request
181+
}
182+
catch (Exception ex)
183+
{
184+
// Use the LogError overload with an Exception as the first parameter.
185+
_logger.LogError(ex, "An error occurred.");
186+
}
187+
}
188+
}
189+
```
190+
191+
> [!NOTE]
192+
> 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 preceding example will not trigger the Snapshot Debugger. In this case, modify your code to call `TrackException` manually.
193+
143194
[!INCLUDE [azure-monitor-log-analytics-rebrand](../../../includes/azure-monitor-instrumentation-key-deprecation.md)]
144195

145196
## Next steps
146197

147198
- Generate traffic to your application that can trigger an exception. Then wait 10 to 15 minutes for snapshots to be sent to the Application Insights instance.
148199
- See [snapshots](snapshot-debugger-data.md?toc=/azure/azure-monitor/toc.json#view-snapshots-in-the-portal) in the Azure portal.
149-
- For help with troubleshooting Snapshot Debugger issues, see [Snapshot Debugger troubleshooting](snapshot-debugger-troubleshoot.md).
200+
- [Troubleshoot](snapshot-debugger-troubleshoot.md) Snapshot Debugger problems.

0 commit comments

Comments
 (0)