【问题标题】:How to add the diagnostic settings to the azure service using .NET SDK?如何使用 .NET SDK 将诊断设置添加到 azure 服务?
【发布时间】:2020-09-06 22:17:18
【问题描述】:

如何使用 c# 从 asp.net 核心 Web 应用程序将诊断设置添加和配置到 Azure 数据工厂?

【问题讨论】:

    标签: c# .net azure asp.net-core-webapi azure-data-factory-2


    【解决方案1】:

    您可以使用 azure management sdk。

    1.安装以下nuget包:

    Microsoft.Azure.Management.Fluent, version 1.33.0.

    Microsoft.Azure.Management.Monitor.Fluent, version 1.33.0.

    2.创建凭据:

    如果你在本地安装了azure cli,也可以直接使用azure cli from azure portal。然后按照this article 创建凭据。简而言之,输入以下 azure cli 命令:

    az ad sp create-for-rbac --sdk-auth
    

    然后你从输出中得到clientIdclientSecrettenantId,请保存这些值。输出如下:

    3.然后使用下面的代码:

            string clientId = "xxx";
            string clientSecret = "xxx";
            string tenantId = "xxx";
    
            var credentials = SdkContext.AzureCredentialsFactory
                        .FromServicePrincipal(clientId,
                                              clientSecret,
                                              tenantId,
                                              AzureEnvironment.AzureGlobalCloud);
    
            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();
    
            azure.DiagnosticSettings
                .Define("test2")
                //the resource id of your ADF
                .WithResource("subscriptions/xxx/resourcegroups/xxx/providers/Microsoft.DataFactory/factories/your_ADF_name")
                //the resource id of your azure log analytics
                .WithLogAnalytics("subscriptions/xxx/resourcegroups/xxxx/providers/microsoft.operationalinsights/workspaces/your_azure_log_analytics_name")
                .WithLog("ActivityRuns", 7)
                .WithLog("PipelineRuns", 7)
                .WithMetric("AllMetrics", TimeSpan.FromMinutes(5), 0)
                .Create();
    

    这是来自 github 的 sample code

    你也可以用azure monitor rest api Diagnostic Settings - Create Or Update来设置它。

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2019-11-28
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      相关资源
      最近更新 更多