使用Azure Fluent API创建应用程序见解

时间:2018-07-10 01:31:40

标签: azure azure-application-insights fluent

是否可以使用Azure Fluent API专门创建应用程序见解? 我看到有一个Monitor代码示例,但这不是特定于Application Insights的。

编辑: 在尝试Azure SDK API中的here之后,我在他们的documentation中未发现错误。

enter image description here

3 个答案:

答案 0 :(得分:1)

当前Fluent API不支持 Application Insights

  

.NET Fluent Azure库似乎不支持应用程序   Java SDK执行时提供洞察功能

也与C#相同。

答案 1 :(得分:0)

根据Azure SDK API,目前没有Application Insight管理API。

但是我们可以使用以下API创建Application Insights。有关更多信息,请参阅this

您可以使用[尝试]直接对其进行测试。

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/components/{resourceName}?api-version=2015-05-01

enter image description here

答案 2 :(得分:0)

这是一个古老的话题,但是Application Insights显然还不属于Azure Management API的一部分。我为此创建了一个类,该类的工作方式类似于Fluent API。

public class AppInsights
{
    public IAzureRestClient azureRestClient { get; private set; }

    public AppInsights(IAzureRestClient client)
    {
        this.azureRestClient = client;
    }

    public Dictionary<string, string> Tags { get; private set; }
    public AppInsights WithTags(Dictionary<string, string> tags)
    {
        this.Tags = tags;
        return this;
    }

    public string Name { get; private set; }
    public AppInsights WithName(string name)
    {
        this.Name = name;
        return this;
    }

    public string SubscriptionId { get; private set; }
    public AppInsights WithSubscriptionId(string id)
    {
        this.SubscriptionId = id;
        return this;
    }

    public IResourceGroup ResourceGroup { get; private set; }
    public AppInsights WithResourceGroup(IResourceGroup group)
    {
        this.ResourceGroup = group;
        return this;
    }

    public string AssociatedApp { get; private set; }
    public AppInsights WithAssociatedApp(string appName)
    {
        this.AssociatedApp = appName;
        return this;
    }

    public AppInsightsComponent Create()
    {
        // initialize tags
        var tags = new Dictionary<string, string>(Tags);
        if (!string.IsNullOrWhiteSpace(AssociatedApp))
        {
            // Add a tag to associate the App Insights instance with the app service
            // ARM syntax for this: "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', parameters('webSiteName'))]": "Resource",
            tags.Add($"hidden-link:{ResourceGroup.Id}/providers/Microsoft.Web/sites/{Name}-portal", "Resource");
        }

        // Create the resource

        var url = $"https://management.azure.com/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroup.Name}/providers/Microsoft.Insights/components/{Name}?api-version=2015-05-01";

        dynamic reqBody = new JObject();
        reqBody.Location = ResourceGroup.RegionName;
        reqBody.Kind = "web";
        reqBody.tags = JObject.FromObject(tags);
        dynamic properties = new JObject();
        properties.ApplicationType = "web";
        properties.FlowType = "Bluefield";
        properties.RequestSource = "rest";
        reqBody.Properties = properties;

        var response = azureRestClient.Put(url, reqBody);
        if (!response.IsSuccessStatusCode)
        {
            throw new HttpRequestException($"management.azure.com returned {response.StatusCode.ToString()}");
        }

        var responseBody = response.Content.ReadAsStringAsync().Result;
        return Newtonsoft.Json.JsonConvert.DeserializeObject<AppInsightsComponent>(responseBody);

    }

this github page上还有更多内容。