gbbosmiya
gbbosmiya

Reputation: 509

Azure monitormanagementclient .net

Using the preview package for Microsoft.Azure.Management.Monitor, I am trying to get ActivityLogs from Azure into a .NET application, but I am uncertain about what to input as "odataquery".

var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var monitorClient = new MonitorManagementClient(serviceCreds);
monitorClient.SubscriptionId = subscriptionId;

var odataquery = "";
var activityLogs = await monitorClient.ActivityLogs.ListAsync(odataQuery: odataquery, cancellationToken: CancellationToken.None);

What should I insert in the odataQuery variable, and where do I get this odataQuery from in Azure? A lot of things are great about Azure, but not documentation for this

Upvotes: 0

Views: 460

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

You should use it like below:

    //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 odataquery = new ODataQuery<EventData>(p => p.EventTimestamp >= startTime && p.EventTimestamp <= endTime);

    var events = client.ActivityLogs.List(odataquery);

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 odataquery, it will throw errors if you're using > or < operator. But it's ok to use >= or <= operator.

Upvotes: 1

Related Questions