通过C#定义日志分析数据源

时间:2019-07-11 22:04:26

标签: azure

如何通过C#应用程序添加下面的屏幕快照中所示的Windows性能计数器?

enter image description here

1 个答案:

答案 0 :(得分:1)

我发现可以通过OperationalInsightsManagementClient完成此操作。下面的代码将添加一个指标。

void AddWorkspaceDatasources(string resourceGroupName, string objectName, string counterName)
{
    var client = new OperationalInsightsManagementClient(GetCredentials()) {SubscriptionId = subscriptionId};
    var existing = client.DataSources.ListByWorkspace(
        new ODataQuery<DataSourceFilter> {Filter = "kind eq 'WindowsPerformanceCounter'"},
        resourceGroupName,
        resourceGroupName);

    if (!existing.Any(c => (c.Properties as JObject)["objectName"].ToString() == objectName && (c.Properties as JObject)["counterName"].ToString() == counterName))
    {
        var properties = new JObject();
        properties["counterName"] = counterName;
        properties["instanceName"] = "*";
        properties["intervalSeconds"] = 10;
        properties["objectName"] = objectName;
        properties["collectorType"] = "Default";

        client.DataSources.CreateOrUpdate(
            resourceGroupName,
            resourceGroupName,
            Regex.Replace(objectName, "[^a-zA-Z0-9]", "") + Regex.Replace(counterName, "[^a-zA-Z0-9]", ""),
            new DataSource
            {
                Kind = "WindowsPerformanceCounter",
                Properties = properties
            });
    }
}