Try for free Book a demo

Search Logic Apps Run or Trigger History

Microsoft Azure

6 Mins Read | Last modified on December 2nd, 2024

Search Logic Apps Run or Trigger History

Introduction

Logic Apps in real-time will define the business workflow. Searching for a specific Logic App run by its identifier or by start and end time is a critical requirement. This article would guide a developer implement searching Logic Apps run and trigger history by leveraging the Logic App SDK. If you are looking for out-of-the-box capability to access your Azure Logic Apps run and trigger history, then Turbo360 is the best solution.

What is Azure Logic App?

Azure Logic App is a cloud service which can connect business-critical apps and services to facilitate automating business workflows without writing a single line of code.

Every Logic App workflow starts with a trigger, which would fall under either of the following:

  • Polling triggers: Regularly checks a service endpoint for new data
  • Push triggers: Listens for data at a service endpoint and waits until a specific event happens 

The trigger defines the calls those instantiate and start the workflow to execute the actions defined.

Below are the key characteristics of Azure Logic Apps:

  • Follows workflow model
  • API as custom connectors
  • Simple and scalable

Accessing the trigger and run histories of Azure Logic Apps is the pre-requisite to search for a specific run. Let us now focus on retrieving the records consuming the exposed SDK’s.

Required Packages

The following packages need to be installed in order to get the Logic App runs or triggers:

  • IdentityModel.Clients.ActiveDirectory
  • Rest.ClientRuntime
  • Azure.Management.Logic

Microsoft.IdentityModel.Clients.ActiveDirectory

This package contains the binaries of the Active Directory Authentication Library (ADAL). It provides an easy way to use authentication functionality for your .NET based client by taking advantage of Windows Server Active Directory and Azure Active Directory.

Microsoft.Rest.ClientRuntime

 Provides common Error Handling, Tracing, and HTTP/REST-based pipeline manipulation.

Microsoft.Azure.Management.Logic

 This SDK helps in managing Logic App. At this context, this SDK is used for retrieving the runs.

How to Access the Logic App?

In order to access, Logic App management client needs to be generated. The following credentials are required to get the Logic App management client.

  • Azure Subscription Id
  • Tenant Id
  • Azure Client Id
  • Azure Client Secret

Code Snippet to Generate Logic App Management Client

private static async Task<LogicManagementClient> GetLogicManagementClient(string azureSubscriptionId, string tenantId, string azureClientId, string azureClientSecret)
        {
            var RequestToken = new AzureTokenRequest()
            {
                tenant_id = tenantId,
                client_id = azureClientId,
                client_secret = azureClientSecret
            };

            var authContext = new         AuthenticationContext(string.Format("https://login.windows.net/{0}", RequestToken.tenant_id));
            ClientCredential clientCred = new ClientCredential(RequestToken.client_id,     RequestToken.client_secret);
            var result = await authContext.AcquireTokenAsync("https://management.azure.com/", clientCred);
            var accessToken = result.AccessToken;
            var token = new TokenCredentials(accessToken);
            return new LogicManagementClient(token) { SubscriptionId = azureSubscriptionId };
        }

Tenant Id:  A Tenant is a representative of an organization within the Azure Active Directory. It is a dedicated instance of the Azure AD service.

Azure Client Id: Azure Active Directory application ID which provides access to the Azure resources.

Azure Client Secret: The Client Secret is a secret known only to the OAuthClient and the Authorization Server. Client Secret must be sufficiently random to not be guessable.

To access the resource, we need to provide contributor level of access to the Active Directory application.

In the above code, the Active Directory application acts as an authentication server which provides the access token to access the Logic App when we pole the Active Directory application with the Client ID and Client Secret.

How to Get Logic App Runs?

The Logic App management client should be provided with Logic App name and Resource Group name to get its runs.

odataQuery can be used to form query to retrieve runs based on the below parameters:

  • status
  • start time

var runs = await logicManagementClient.WorkflowRuns.ListAsync("Megahertz", "webshop", odataQuery);

“odataQuery.Filter” parameter can be used to perform a filtered search based on the query. Now to retrieve the Logic App runs. We need to pass on the Resource Group name, Logic App name and Search Query. This will return the run history based on the status and the start time provided.

Entire Code Snippet to Get the Logic App Runs

using Microsoft.Azure.Management.Logic;
using Microsoft.Azure.Management.Logic.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Microsoft.Rest.Azure.OData;
using System;
using System.Threading.Tasks;

namespace SearchInLogicApp
{
    class Program
    {

        static async System.Threading.Tasks.Task Main(string[] args)
        {

            
//frame ‘ODataQuery ‘ based on the requirement and   pass as a parameter to LogicApp  Management Client
ODataQuery<WorkflowRunFilter> odataQuery = new ODataQuery<WorkflowRunFilter>();
            odataQuery.OrderBy = "startTime desc";
            odataQuery.Top = 10;
            var logicManagementClient = await GetLogicManagementClient(<azureSubscriptionId>, < tenantId>, < azureClientId>, < azureClientSecret>);
            var runs = await logicManagementClient.WorkflowRuns.ListAsync("Megahertz", "webshop", odataQuery);
            foreach (var run in runs)
            {
                Console.WriteLine(run.Name + " " + run.Status);
            }
            Console.ReadLine();
        }
// function to Get LogicApp  Management Client
        private static async Task<LogicManagementClient> GetLogicManagementClient(string azureSubscriptionId, string tenantId, string azureClientId, string azureClientSecret)
        {
            var RequestToken = new AzureTokenRequest()
            {
                tenant_id = tenantId,
                client_id = azureClientId,
                client_secret = azureClientSecret
            };

            var authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", RequestToken.tenant_id));
            ClientCredential clientCred = new ClientCredential(RequestToken.client_id, RequestToken.client_secret);
            var result = await authContext.AcquireTokenAsync("https://management.azure.com/", clientCred);
            var accessToken = result.AccessToken;
            var token = new TokenCredentials(accessToken);
            return new LogicManagementClient(token) { SubscriptionId = azureSubscriptionId };
        }
    }
} 

Output

This image depicts the output on running the above code sucessfully.

Azure Logic App Runs

The Filter Runs Based on the Status

To filter the runs based on the status “odataQuery.Filter” value should be provided.

Query filter should look something like this to retrieve succeeded runs:

odataQuery.Filter = "status eq ‘succeeded’"

There are 13 workflow status, some of them are:

  • Aborted
  • Canceled
  • Failed
  • Ignored
  • Succeeded
  • Running

Filter Runs Based on the Start Time

odataQuery.Filter =“ startTime ge <yyyy-MM-ddTHH:mm:ss.fffZ>”

Filter Runs Within a Time Frame

odataQuery.Filter = “ startTime ge <yyyy-MM-ddTHH:mm:ss.fffZ> and startTime le <yyyy-MM-ddTHH:mm:ss.fffZ>”

We can formulate queries based on the requirements.

Trigger History

We can also retrieve trigger history of the Logic Apps with this same SDK “Microsoft.Azure.Management.Logic” as below:

var runs = await logicManagementClient.WorkflowTriggerHistories.ListAsync(“<ResourceGroup>”,”<LogicAppName>”, “<triggerName>”, odataQuery);

Conclusion

In this blog, we saw how to retrieve run/trigger history of Azure Logic Apps and how to filter the workflow based on our requirements with various search queries.

This article was originally published on Aug 19, 2019. It was most recently updated on Dec 2, 2024.

Azure Logic Apps - Request Demo CTA

Related Articles