问题
I am trying to get custom events from Azure via the Microsoft.Azure.Management.Monitor API (preview), but I get a "Bad Request" response when doing so.
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
var credential = new ClientCredential(clientId: clientId, clientSecret: secret);
var result = context.AcquireTokenAsync("https://management.core.windows.net/", credential);
var accesstoken = result.GetAwaiter().GetResult().AccessToken;
var tokenProvider = new StringTokenProvider(accesstoken, "Bearer");
var id = Guid.NewGuid().ToString();
var token = new TokenCredentials(tokenProvider: tokenProvider, tenantId: tenantId, callerId: id);
var client = new MonitorManagementClient(token)
{
SubscriptionId = subscriptionId
};
token.InitializeServiceClient(client);
var startTime = DateTime.Parse("2020-02-01T01:00:00.00Z");
var endTime = DateTime.Parse("2020-02-28T01:00:00.00Z");
var filterString = new ODataQuery<EventData>(p => p.EventTimestamp >= startTime && p.EventTimestamp <= endTime);
var events = client.ActivityLogs.List(filterString);
foreach (var ev in events)
{
Console.WriteLine(ev.EventName);
}
The last line returns this error. I have followed the code provided in the unit test for this: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/monitor/Microsoft.Azure.Management.Monitor/tests/BasicTests/EventDataTests.cs
回答1:
After some research, I can let it work. And also I find some bugs in this package, and the test method is not good which actually cannot work.
The "Unauthorized" means that you didn't use the proper token.
The package Microsoft.IdentityModel.Clients.ActiveDirectory is needed here, please install it.
Here is the sample code which can work well:
static void Main(string[] args)
{
var tenantId = "xxx";
var subscriptionId = "xxx";
var clientId = "xxx";
var secret = "xxx";
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
var credential = new ClientCredential(clientId: clientId, clientSecret: secret);
var result = context.AcquireTokenAsync("https://management.core.windows.net/", credential);
var accesstoken = result.GetAwaiter().GetResult().AccessToken;
var tokenProvider = new StringTokenProvider(accesstoken, "Bearer");
var id = Guid.NewGuid().ToString();
var token = new TokenCredentials(tokenProvider: tokenProvider, tenantId: tenantId, callerId: id);
var client = new MonitorManagementClient(token)
{
SubscriptionId = subscriptionId
};
token.InitializeServiceClient(client);
//DO NOT USE DateTimeOffset to parse datetime.
//var startTime = DateTimeOffset.Parse("2019-03-11T01:00:00.00Z");
//var endTime = DateTimeOffset.Parse("2020-03-11T02:00:00.00Z");
//use DateTime to parse datetime.
var startTime = DateTime.Parse("2020-03-05T01:00:00.00Z");
var endTime = DateTime.Parse("2020-03-28T01:00:00.00Z");
//it will throw errors if use the operator > or <, but works if using >= or <=
var filterString = new ODataQuery<EventData>(p => p.EventTimestamp >= startTime && p.EventTimestamp <= endTime);
var events = client.ActivityLogs.List(filterString);
Console.WriteLine("**Completed**");
Console.ReadLine();
}
And here are some notes you need to take care of:
1.When define startTime
and endTime
, you should use DateTime.Parse()
method instead of DateTimeOffset.Parse()
.
2.The startTime
should not be more than 90 days ago.
3.When define filterString
, it will throw errors if you're using >
or <
operator. But it's ok to use >=
or <=
operator.
Here is the test result for the simple sample code:
来源:https://stackoverflow.com/questions/61007517/custom-events-in-azure-monitor-for-net-core