0
votes

we are not getting list shown under diagnostics settings in azure portal, with using azure rest API

i tried using azure rest API for diagnostics setting list

i want list of resources under diagnostics settings from azure portal using azure rest API.

so the attached snapshot is what we want to get

1
Any update now? If it helps you, please accept it as answer.(click on the check mark beside the answer to toggle it from greyed out to filled in.)Joey Cai

1 Answers

0
votes

It seems that there is no single API call to retrieve all the diagnostic settings, you could gets the active diagnostic settings list for the specified resource.

Refer to the link https://docs.microsoft.com/en-us/rest/api/monitor/diagnosticsettings/list

Update:

1.First, go to your sql server>Access control(IAM)>Add>Add Role Assignment and assign a role to your service principal.

enter image description here

2.Use the following rest api C# code to get sql database diagnostics settings info.

public static void getDiaSettings()
{
    var appId = "xxxxxxxx";
    var secretKey = "xxxxxxxx";
    var tenantId = "xxxxxxxx";
    var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
    ClientCredential clientCredential = new ClientCredential(appId, secretKey);
    var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
    var accessToken = tokenResponse.AccessToken;
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
        var baseUrl = new Uri($"https://management.azure.com/");
        var request2=  baseUrl +
                     @"/subscriptions/xxxxxxxx/resourceGroups/yourResourceGroup/providers/Microsoft.Sql/servers/yourSqlServer/databases/yourSqlDatabase/providers/microsoft.insights/diagnosticSettings?api-version=2017-05-01-preview";
        var response = client.GetAsync(request2).Result.Content.ReadAsStringAsync().Result;
    }
}