How to Retrieve Azure Function App Logs for a Custom Date Range from Application Insights?

CS001 11 Reputation points
2024-07-29T09:40:06.91+00:00

Hi everyone,

I am working with an Azure Function App and want to retrieve logs from Application Insights for a specific date range. Specifically, I am trying to extract logs in the same format and detail as seen in the "Log Stream" of the Azure Function App in the Azure Portal.

Here are the steps I've taken so far:

Checked Log Stream: I can see the logs live in the "Log Stream" section of the Azure Portal. However, it only shows logs for the current time, and I do not see an option to search or filter logs by a custom date range.

Explored Application Insights: I tried using the Azure Portal's "Logs" feature with a Kusto Query Language (KQL) query like traces | order by timestamp desc. While this query retrieves logs, it does not include certain parameters, such as the file ID, that I passed to the functions.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,694 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 7,441 Reputation points
    2024-07-29T17:52:08.5866667+00:00

    Hello CS001,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Problem

    I understand that you would like to retrieve logs from Application Insights for a specific date range.

    Solution

    That can be done using the following steps:

    Query and Scope selection

    When you run a log query in Log Analytics within the Azure portal, the data evaluated by the query depends on the scope and time range you select. The query scope defines the records that the query evaluates. You can set the scope to a specific Log Analytics workspace or an Application Insights application. To select the scope:

    • For Log Analytics workspace: Go to the Azure Monitor menu or the Log Analytics workspaces menu and choose "Logs."
    • For Application Insights application: Go to the Application Insights menu for the specific application and select "Logs."
    • You can also set the scope to other Azure resources (e.g., resource group, subscription) if needed.
    • To set the time range, use the time picker at the top of the Log Analytics window. You can select predefined periods or specify a custom time range.

    Using Kusto Query Language (KQL):

    Use KQL to query the logs. For example, you can use the traces table to retrieve ILogger messages and to filter by a specific date range, modify your query to include a where clause with the timestamp column. For an example:

     traces
         | where timestamp >= datetime(2024-07-01) and timestamp < datetime(2024-07-02)
         | order by timestamp desc
    

    Adjust the date range as needed to match your desired time frame.

    Workspace-based application in Application Insights

    But if you're using a workspace-based application in Application Insights, its data is stored in a Log Analytics workspace. You can still access this data by setting the scope to the workspace. Refine your queries by filtering ILogger data based on severity, specific message content, or other relevant parameters.

    References

    Log query scope and time range in Azure Monitor Log Analytics.

    Application Insights logging with .NET.

    Checking Logs in Application Insights.

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments