
关于
Azure Application Insights .NET SDK。应用性能监控和可观测性资源管理。
name: azure-mgmt-applicationinsights-dotnet description: Azure Application Insights SDK for .NET。应用程序性能监控和可观测性资源管理。 risk: unknown source: community date_added: '2026-02-27'
Azure.ResourceManager.ApplicationInsights (.NET)
用于管理 Application Insights 资源的 Azure Resource Manager SDK,用于应用程序性能监控。
安装
dotnet add package Azure.ResourceManager.ApplicationInsights
dotnet add package Azure.Identity
当前版本: v1.0.0 (GA) API 版本: 2022-06-15
环境变量
AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>
AZURE_APPINSIGHTS_NAME=<your-appinsights-component>
身份验证
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.ApplicationInsights;
ArmClient client = new ArmClient(new DefaultAzureCredential());
资源层次结构
Subscription
└── ResourceGroup
└── ApplicationInsightsComponent # App Insights 资源
├── ApplicationInsightsComponentApiKey # 用于编程访问的 API 密钥
├── ComponentLinkedStorageAccount # 用于数据导出的链接存储
└── (via component ID)
├── WebTest # 可用性测试
├── Workbook # 分析工作簿
├── WorkbookTemplate # 工作簿模板
└── MyWorkbook # 私有工作簿
核心工作流
1. 创建 Application Insights 组件(基于工作区)
using Azure.ResourceManager.ApplicationInsights;
using Azure.ResourceManager.ApplicationInsights.Models;
ResourceGroupResource resourceGroup = await client
.GetDefaultSubscriptionAsync()
.Result
.GetResourceGroupAsync("my-resource-group");
ApplicationInsightsComponentCollection components = resourceGroup.GetApplicationInsightsComponents();
// Workspace-based Application Insights (recommended)
ApplicationInsightsComponentData data = new ApplicationInsightsComponentData(
AzureLocation.EastUS,
ApplicationInsightsApplicationType.Web)
{
Kind = "web",
WorkspaceResourceId = new ResourceIdentifier(
"/subscriptions/<sub-id>/resourceGroups/<rg>/providers/Microsoft.OperationalInsights/workspaces/<workspace-name>"),
IngestionMode = IngestionMode.LogAnalytics,
PublicNetworkAccessForIngestion = PublicNetworkAccessType.Enabled,
PublicNetworkAccessForQuery = PublicNetworkAccessType.Enabled,
RetentionInDays = 90,
SamplingPercentage = 100,
DisableIPMasking = false,
ImmediatePurgeDataOn30Days = false,
Tags =
{
{ "environment", "production" },
{ "application", "mywebapp" }
}
};
ArmOperation<ApplicationInsightsComponentResource> operation = await components
.CreateOrUpdateAsync(WaitUntil.Completed, "my-appinsights", data);
ApplicationInsightsComponentResource component = operation.Value;
Console.WriteLine($"Component created: {component.Data.Name}");
Console.WriteLine($"Instrumentation Key: {component.Data.InstrumentationKey}");
Console.WriteLine($"Connection String: {component.Data.ConnectionString}");
2. 获取连接字符串和密钥
ApplicationInsightsComponentResource component = await resourceGroup
.GetApplicationInsightsComponentAsync("my-appinsights");
// Get connection string for SDK configuration
string connectionString = component.Data.ConnectionString;
string instrumentationKey = component.Data.InstrumentationKey;
string appId = component.Data.AppId;
Console.WriteLine($"Connection String: {connectionString}");
Console.WriteLine($"Instrumentation Key: {instrumentationKey}");
Console.WriteLine($"App ID: {appId}");
3. 创建 API 密钥
ApplicationInsightsComponentResource component = await resourceGroup
.GetApplicationInsightsComponentAsync("my-appinsights");
ApplicationInsightsComponentApiKeyCollection apiKeys = component.GetApplicationInsightsComponentApiKeys();
// API key for reading telemetry
ApplicationInsightsApiKeyContent keyContent = new ApplicationInsightsApiKeyContent
{
Name = "ReadTelemetryKey",
LinkedReadProperties =
{
$"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/components/{component.Data.Name}/api",
$"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/components/{component.Data.Name}/agentconfig"
}
};
ApplicationInsightsComponentApiKeyResource apiKey = await apiKeys
.CreateOrUpdateAsync(WaitUntil.Completed, keyContent);
Console.WriteLine($"API Key Name: {apiKey.Data.Name}");
Console.WriteLine($"API Key: {apiKey.Data.ApiKey}"); // Only shown once!
4. 创建 Web 测试(可用性测试)
WebTestCollection webTests = resourceGroup.GetWebTests();
// URL Ping Test configuration
// See Azure documentation for full WebTestData setup
兼容工具
Claude CodeCursor
标签
AI与机器学习