Search code examples
azurehttpsazure-functionsazure-data-lake-gen2

how to save logs to adls for every https request of azure function


I have a Azure function with HTTP trigger. For every HTTP request I need to save the logs of details the of post request to ADLS like time of trigger and request params.

Can anyone help me how can i do this?


Solution

  • Here is the Workaround I did to store the Azure Function Logs in ADLS Gen 2 where the logs contains the Function Execution Time, Started Time, Execution Result, etc.

    • To Create the Function App in ADLS Gen 2, First, you need to create the Storage Account of type ADLS Gen 2.

    • When you're creating the Storage Account, Check the box of Enable hierarchical namespace to create an ADLS Gen 2 Account.

    enter image description here

    After Creating an ADLS Storage account, create a function app in that storage account using PowerShell Command: (For example DotNet Stack)

    New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount <STORAGE_NAME> -Runtime dotnet -FunctionsVersion 3 -Location '<REGION>'
    
    • Created Http Trigger Function App through Portal in the Function App and it is running successfully with the POST Request.
    • Enable the Application Insights Resource on your Function App in the Portal.
    • All the logs will be stored in the path:

    ADLS Storage Account > File Shares > Your Function App > LogFiles > Application > Functions > Function > Your Function Name > here you can see the logs.

    enter image description here enter image description here

    enter image description here

    Suppose, Function Key is also a one of the parameter in Azure Function URL.

    By default, this info will not be logged but we can customize the code to log this parameter and in the Traces table of Application insights, this information can be available.

    const  string key_Claim =  "http://schemas.microsoft.com/2017/07/functions/claims/keyid";
    var claim = req.HttpContext.User.Claims.FirstOrDefault(c => c.Type  == key_Claim);
    if(claim !=  null)
    {
    log.LogInformation(  "The call was made using {0} named key", claim.Value);
     }
    

    enter image description here

    In Application Insights [Logs]

    enter image description here

    The above Workaround I did in .Net Stack and here is an example code related to read parameters of Azure Function and store it in a blob files using NodeJS.