Skip to content

Commit 73e96ec

Browse files
authored
Merge pull request #231538 from hhunter-ms/hh-69857
[Profiler/Snapshot Debugger] Update .NET examples
2 parents 6cc8381 + ae7a5a3 commit 73e96ec

File tree

2 files changed

+133
-167
lines changed

2 files changed

+133
-167
lines changed

articles/azure-monitor/profiler/profiler-aspnetcore-linux.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ In this guide, you'll:
2222

2323
- Install the [latest and greatest .NET Core SDK](https://dotnet.microsoft.com/download/dotnet).
2424
- Install Git by following the instructions at [Getting Started - Installing Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git).
25+
- Review the following samples for context:
26+
- [Enable Service Profiler for containerized ASP.NET Core Application (.NET 6)](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/tree/main/examples/EnableServiceProfilerForContainerAppNet6)
27+
- [Application Insights Profiler for Worker Service Example](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/tree/main/examples/ServiceProfilerInWorkerNet6)
2528

2629
## Set up the project locally
2730

@@ -41,30 +44,31 @@ In this guide, you'll:
4144
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
4245
```
4346

44-
1. In your preferred code editor, enable Application Insights and Profiler in `Program.cs`:
47+
1. In your preferred code editor, enable Application Insights and Profiler in `Program.cs`. [Add custom Profiler settings, if applicable](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/blob/main/Configurations.md).
48+
49+
For `WebAPI`:
4550

4651
```csharp
47-
public void ConfigureServices(IServiceCollection services)
48-
{
49-
services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights.
50-
services.AddServiceProfiler(); // Add this line of code to Enable Profiler
51-
services.AddControllersWithViews();
52-
}
52+
// Add services to the container.
53+
builder.Services.AddApplicationInsightsTelemetry();
54+
builder.Services.AddServiceProfiler();
5355
```
5456

55-
1. Add a line of code in the **HomeController.cs** section to randomly delay a few seconds:
57+
For `Worker`:
5658

5759
```csharp
58-
using System.Threading;
59-
...
60-
61-
public IActionResult About()
60+
IHost host = Host.CreateDefaultBuilder(args)
61+
.ConfigureServices(services =>
6262
{
63-
Random r = new Random();
64-
int delay = r.Next(5000, 10000);
65-
Thread.Sleep(delay);
66-
return View();
67-
}
63+
services.AddApplicationInsightsTelemetryWorkerService();
64+
services.AddServiceProfiler();
65+
66+
// Assuming Worker is your background service class.
67+
services.AddHostedService<Worker>();
68+
})
69+
.Build();
70+
71+
await host.RunAsync();
6872
```
6973

7074
1. Save and commit your changes to the local repository:

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

Lines changed: 112 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: hhunter-ms
66
ms.reviewer: charles.weininger
77
reviewer: cweining
88
ms.topic: conceptual
9-
ms.date: 08/18/2022
9+
ms.date: 03/21/2023
1010
ms.custom: devdivchpfy22
1111
---
1212

@@ -16,170 +16,132 @@ If your ASP.NET or ASP.NET Core application runs in App Service and requires a c
1616

1717
If your application runs in Azure Service Fabric, Cloud Service, Virtual Machines, or on-premises machines, you can skip enabling Snapshot Debugger on App Services and jump into following this guide.
1818

19+
## Before you begin
20+
21+
- [Enable Application Insights in your web app](../app/asp-net.md).
22+
23+
- Include the [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package version 1.3.5 or above in your app.
24+
1925
## Configure snapshot collection for ASP.NET applications
2026

21-
### Prerequisite
22-
23-
[Enable Application Insights in your web app](../app/asp-net.md).
24-
25-
1. Include the [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package in your app.
26-
27-
1. If needed, customize the Snapshot Debugger configuration added to [ApplicationInsights.config](../app/configuration-with-applicationinsights-config.md).
28-
29-
The default Snapshot Debugger configuration is mostly empty and all settings are optional. Here's an example showing a configuration equivalent to the default configuration:
30-
31-
```xml
32-
<TelemetryProcessors>
33-
<Add Type="Microsoft.ApplicationInsights.SnapshotCollector.SnapshotCollectorTelemetryProcessor, Microsoft.ApplicationInsights.SnapshotCollector">
34-
<!-- The default is true, but you can disable Snapshot Debugging by setting it to false -->
35-
<IsEnabled>true</IsEnabled>
36-
<!-- Snapshot Debugging is usually disabled in developer mode, but you can enable it by setting this to true. -->
37-
<!-- DeveloperMode is a property on the active TelemetryChannel. -->
38-
<IsEnabledInDeveloperMode>false</IsEnabledInDeveloperMode>
39-
<!-- How many times we need to see an exception before we ask for snapshots. -->
40-
<ThresholdForSnapshotting>1</ThresholdForSnapshotting>
41-
<!-- The maximum number of examples we create for a single problem. -->
42-
<MaximumSnapshotsRequired>3</MaximumSnapshotsRequired>
43-
<!-- The maximum number of problems that we can be tracking at any time. -->
44-
<MaximumCollectionPlanSize>50</MaximumCollectionPlanSize>
45-
<!-- How often we reconnect to the stamp. The default value is 15 minutes.-->
46-
<ReconnectInterval>00:15:00</ReconnectInterval>
47-
<!-- How often to reset problem counters. -->
48-
<ProblemCounterResetInterval>1.00:00:00</ProblemCounterResetInterval>
49-
<!-- The maximum number of snapshots allowed in ten minutes.The default value is 1. -->
50-
<SnapshotsPerTenMinutesLimit>3</SnapshotsPerTenMinutesLimit>
51-
<!-- The maximum number of snapshots allowed per day. -->
52-
<SnapshotsPerDayLimit>30</SnapshotsPerDayLimit>
53-
<!-- Whether or not to collect snapshot in low IO priority thread. The default value is true. -->
54-
<SnapshotInLowPriorityThread>true</SnapshotInLowPriorityThread>
55-
<!-- Agree to send anonymous data to Microsoft to make this product better. -->
56-
<ProvideAnonymousTelemetry>true</ProvideAnonymousTelemetry>
57-
<!-- The limit on the number of failed requests to request snapshots before the telemetry processor is disabled. -->
58-
<FailedRequestLimit>3</FailedRequestLimit>
59-
</Add>
60-
</TelemetryProcessors>
61-
```
62-
63-
1. Snapshots are collected only on exceptions that are reported to Application Insights. In some cases (for example, older versions of the .NET platform), you might need to [configure exception collection](../app/asp-net-exceptions.md#exceptions) to see exceptions with snapshots in the portal.
27+
The default Snapshot Debugger configuration is mostly empty and all settings are optional. You can customize the Snapshot Debugger configuration added to [ApplicationInsights.config](../app/configuration-with-applicationinsights-config.md).
28+
29+
The following example shows a configuration equivalent to the default configuration:
30+
31+
```xml
32+
<TelemetryProcessors>
33+
<Add Type="Microsoft.ApplicationInsights.SnapshotCollector.SnapshotCollectorTelemetryProcessor, Microsoft.ApplicationInsights.SnapshotCollector">
34+
<!-- The default is true, but you can disable Snapshot Debugging by setting it to false -->
35+
<IsEnabled>true</IsEnabled>
36+
<!-- Snapshot Debugging is usually disabled in developer mode, but you can enable it by setting this to true. -->
37+
<!-- DeveloperMode is a property on the active TelemetryChannel. -->
38+
<IsEnabledInDeveloperMode>false</IsEnabledInDeveloperMode>
39+
<!-- How many times we need to see an exception before we ask for snapshots. -->
40+
<ThresholdForSnapshotting>1</ThresholdForSnapshotting>
41+
<!-- The maximum number of examples we create for a single problem. -->
42+
<MaximumSnapshotsRequired>3</MaximumSnapshotsRequired>
43+
<!-- The maximum number of problems that we can be tracking at any time. -->
44+
<MaximumCollectionPlanSize>50</MaximumCollectionPlanSize>
45+
<!-- How often we reconnect to the stamp. The default value is 15 minutes.-->
46+
<ReconnectInterval>00:15:00</ReconnectInterval>
47+
<!-- How often to reset problem counters. -->
48+
<ProblemCounterResetInterval>1.00:00:00</ProblemCounterResetInterval>
49+
<!-- The maximum number of snapshots allowed in ten minutes.The default value is 1. -->
50+
<SnapshotsPerTenMinutesLimit>3</SnapshotsPerTenMinutesLimit>
51+
<!-- The maximum number of snapshots allowed per day. -->
52+
<SnapshotsPerDayLimit>30</SnapshotsPerDayLimit>
53+
<!-- Whether or not to collect snapshot in low IO priority thread. The default value is true. -->
54+
<SnapshotInLowPriorityThread>true</SnapshotInLowPriorityThread>
55+
<!-- Agree to send anonymous data to Microsoft to make this product better. -->
56+
<ProvideAnonymousTelemetry>true</ProvideAnonymousTelemetry>
57+
<!-- The limit on the number of failed requests to request snapshots before the telemetry processor is disabled. -->
58+
<FailedRequestLimit>3</FailedRequestLimit>
59+
</Add>
60+
</TelemetryProcessors>
61+
```
62+
63+
Snapshots are collected _only_ on exceptions reported to Application Insights. In some cases (for example, older versions of the .NET platform), you may need to [configure exception collection](../app/asp-net-exceptions.md#exceptions) to see exceptions with snapshots in the portal.
6464

6565
## Configure snapshot collection for applications using ASP.NET Core LTS or above
6666

6767
### Prerequisites
6868

69-
[Enable Application Insights in your ASP.NET Core web app](../app/asp-net-core.md), if you haven't done it yet.
70-
> [!NOTE]
71-
> Be sure that your application references version 2.1.1, or newer, of the Microsoft.ApplicationInsights.AspNetCore package.
72-
73-
1. Include the [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package in your app.
74-
75-
1. Modify your application's `Startup` class to add and configure the Snapshot Collector's telemetry processor.
76-
1. If [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package version 1.3.5 or above is used, then add the following using statements to *Startup.cs*:
77-
78-
```csharp
79-
using Microsoft.ApplicationInsights.SnapshotCollector;
80-
```
81-
82-
Add the following at the end of the ConfigureServices method in the `Startup` class in *Startup.cs*:
83-
84-
```csharp
85-
services.AddSnapshotCollector((configuration) => Configuration.Bind(nameof(SnapshotCollectorConfiguration), configuration));
86-
```
87-
88-
1. If [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package version 1.3.4 or below is used, then add the following using statements to *Startup.cs*.
89-
90-
```csharp
91-
using Microsoft.ApplicationInsights.SnapshotCollector;
92-
using Microsoft.Extensions.Options;
93-
using Microsoft.ApplicationInsights.AspNetCore;
94-
using Microsoft.ApplicationInsights.Extensibility;
95-
```
96-
97-
Add the following `SnapshotCollectorTelemetryProcessorFactory` class to `Startup` class:
98-
99-
```csharp
100-
class Startup
101-
{
102-
private class SnapshotCollectorTelemetryProcessorFactory : ITelemetryProcessorFactory
103-
{
104-
private readonly IServiceProvider _serviceProvider;
105-
106-
public SnapshotCollectorTelemetryProcessorFactory(IServiceProvider serviceProvider) =>
107-
_serviceProvider = serviceProvider;
108-
109-
public ITelemetryProcessor Create(ITelemetryProcessor next)
110-
{
111-
var snapshotConfigurationOptions = _serviceProvider.GetService<IOptions<SnapshotCollectorConfiguration>>();
112-
return new SnapshotCollectorTelemetryProcessor(next, configuration: snapshotConfigurationOptions.Value);
113-
}
114-
}
115-
...
116-
```
117-
118-
Add the `SnapshotCollectorConfiguration` and `SnapshotCollectorTelemetryProcessorFactory` services to the startup pipeline:
119-
120-
```csharp
121-
// This method gets called by the runtime. Use this method to add services to the container.
122-
public void ConfigureServices(IServiceCollection services)
123-
{
124-
// Configure SnapshotCollector from application settings
125-
services.Configure<SnapshotCollectorConfiguration>(Configuration.GetSection(nameof(SnapshotCollectorConfiguration)));
126-
127-
// Add SnapshotCollector telemetry processor.
128-
services.AddSingleton<ITelemetryProcessorFactory>(sp => new SnapshotCollectorTelemetryProcessorFactory(sp));
129-
130-
// TODO: Add other services your application needs here.
131-
}
132-
}
133-
```
134-
135-
1. If needed, customize the Snapshot Debugger configuration by adding a `SnapshotCollectorConfiguration` section to *appsettings.json*.
136-
137-
All settings in the Snapshot Debugger configuration are optional. Here's an example showing a configuration equivalent to the default configuration:
138-
139-
```json
140-
{
141-
"SnapshotCollectorConfiguration": {
142-
"IsEnabledInDeveloperMode": false,
143-
"ThresholdForSnapshotting": 1,
144-
"MaximumSnapshotsRequired": 3,
145-
"MaximumCollectionPlanSize": 50,
146-
"ReconnectInterval": "00:15:00",
147-
"ProblemCounterResetInterval":"1.00:00:00",
148-
"SnapshotsPerTenMinutesLimit": 1,
149-
"SnapshotsPerDayLimit": 30,
150-
"SnapshotInLowPriorityThread": true,
151-
"ProvideAnonymousTelemetry": true,
152-
"FailedRequestLimit": 3
153-
}
154-
}
155-
```
69+
Create a new class called `SnapshotCollectorTelemetryProcessorFactory` to add and configure the Snapshot Collector's telemetry processor.
15670

157-
## Configure snapshot collection for other .NET applications
71+
```csharp
72+
using Microsoft.ApplicationInsights.AspNetCore;
73+
using Microsoft.ApplicationInsights.Extensibility;
74+
using Microsoft.ApplicationInsights.SnapshotCollector;
75+
using Microsoft.Extensions.Options;
76+
77+
internal class SnapshotCollectorTelemetryProcessorFactory : ITelemetryProcessorFactory
78+
{
79+
private readonly IServiceProvider _serviceProvider;
15880

159-
1. If your application isn't already instrumented with Application Insights, get started by [enabling Application Insights and setting the instrumentation key](https://github.com/Microsoft/appcenter).
81+
public SnapshotCollectorTelemetryProcessorFactory(IServiceProvider serviceProvider) =>
82+
_serviceProvider = serviceProvider;
16083

161-
1. Add the [Microsoft.ApplicationInsights.SnapshotCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.SnapshotCollector) NuGet package in your app.
84+
public ITelemetryProcessor Create(ITelemetryProcessor next)
85+
{
86+
IOptions<SnapshotCollectorConfiguration> snapshotConfigurationOptions = _serviceProvider.GetRequiredService<IOptions<SnapshotCollectorConfiguration>>();
87+
return new SnapshotCollectorTelemetryProcessor(next, configuration: snapshotConfigurationOptions.Value);
88+
}
89+
}
90+
```
91+
92+
Add the `SnapshotCollectorConfiguration` and `SnapshotCollectorTelemetryProcessorFactory` services to `Program.cs`:
93+
94+
```csharp
95+
using Microsoft.ApplicationInsights.AspNetCore;
96+
using Microsoft.ApplicationInsights.SnapshotCollector;
97+
98+
var builder = WebApplication.CreateBuilder(args);
99+
100+
builder.Services.AddApplicationInsightsTelemetry();
101+
builder.Services.AddSnapshotCollector(config => builder.Configuration.Bind(nameof(SnapshotCollectorConfiguration), config));
102+
builder.Services.AddSingleton<ITelemetryProcessorFactory>(sp => new SnapshotCollectorTelemetryProcessorFactory(sp));
103+
```
104+
105+
If needed, customize the Snapshot Debugger configuration by adding a `SnapshotCollectorConfiguration` section to *appsettings.json*. The following example shows a configuration equivalent to the default configuration:
106+
107+
```json
108+
{
109+
"SnapshotCollectorConfiguration": {
110+
"IsEnabledInDeveloperMode": false,
111+
"ThresholdForSnapshotting": 1,
112+
"MaximumSnapshotsRequired": 3,
113+
"MaximumCollectionPlanSize": 50,
114+
"ReconnectInterval": "00:15:00",
115+
"ProblemCounterResetInterval":"1.00:00:00",
116+
"SnapshotsPerTenMinutesLimit": 1,
117+
"SnapshotsPerDayLimit": 30,
118+
"SnapshotInLowPriorityThread": true,
119+
"ProvideAnonymousTelemetry": true,
120+
"FailedRequestLimit": 3
121+
}
122+
}
123+
```
162124

163-
1. Snapshots are collected only on exceptions that are reported to Application Insights. You may need to modify your code to report them. The exception handling code depends on the structure of your application, but an example is below:
125+
## Configure snapshot collection for other .NET applications
164126

165-
```csharp
166-
TelemetryClient _telemetryClient = new TelemetryClient();
127+
Snapshots are collected only on exceptions that are reported to Application Insights. You may need to modify your code to report them. The exception handling code depends on the structure of your application, but an example is below:
167128

168-
void ExampleRequest()
129+
```csharp
130+
TelemetryClient _telemetryClient = new TelemetryClient();
131+
void ExampleRequest()
132+
{
133+
try
134+
{
135+
// TODO: Handle the request.
136+
}
137+
catch (Exception ex)
169138
{
170-
try
171-
{
172-
// TODO: Handle the request.
173-
}
174-
catch (Exception ex)
175-
{
176-
// Report the exception to Application Insights.
177-
_telemetryClient.TrackException(ex);
178-
179-
// TODO: Rethrow the exception if desired.
180-
}
139+
// Report the exception to Application Insights.
140+
_telemetryClient.TrackException(ex);
141+
// TODO: Rethrow the exception if desired.
181142
}
182-
```
143+
}
144+
```
183145

184146
[!INCLUDE [azure-monitor-log-analytics-rebrand](../../../includes/azure-monitor-instrumentation-key-deprecation.md)]
185147

0 commit comments

Comments
 (0)