简体   繁体   中英

Create Application Insights using Azure Fluent API

Is there a way to specifically create an Application Insights using Azure Fluent API? I see there is a Monitor code sample, but this is not specific to Application Insights.

EDIT: After trying the Azure SDK API from here , I got an error not identified in their documentation .

在此处输入图片说明

Currently Fluent API does not support for Application Insights .

It appears the .NET Fluent Azure libraries do not support Application Insight provisioning while the Java SDK does

Same with C# too.

According to Azure SDK API , there is no Application Insight management API currently.

But we could create the Application Insights with following API. For more information, please refer to this .

You could test it directly with [ try it ].

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

在此处输入图片说明

This is an old topic, but apparently Application Insights still isn't part of the Azure Management API yet. I created a class for this, which works a bit like the Fluent API would.

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);

    }

There's more context on this github page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM