ELMS: how to save module logs to blob storage?

Mistyron 25 Reputation points
2024-11-27T15:36:30.1+00:00

Hi,

first off, this is a double post. I have started a post on Stack overflow already, the URL is here: https://stackoverflow.com/questions/79227238/elms-how-to-save-module-logs-to-blob-storage?noredirect=1#comment139710644_79227238 I'll make sure to keep answers synchronized between the posts! After I posted on Stackoverflow, I thought that it might be better to sk for assistance here directly.

I want to save my module logs to blob as it's described here: https://learn.microsoft.com/en-us/azure/iot-edge/how-to-retrieve-iot-edge-logs?view=iotedge-1.5&viewFallbackFrom=iotedge-2018-06#upload-module-logs Only, I'm wondering how I can provide the sasURL to the method, can I do this through the Azure portal? How? We are currently using ELMS and I can see invocations of the following functions:

  • InvokeUploadModuleLogs
  • ScheduleUploadModuleLogs

in the portal.

Currently, I retrieve the module logs through iot-hub but only if the device is online. It would be great to have access to logs even if the device is offline hence I ant to upload them to blob.

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,990 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Keshavulu Dasari 2,250 Reputation points Microsoft Vendor
    2024-12-03T02:08:56.4633333+00:00

    Hi Mistyron,
    Since the InvokeUploadModuleLogs function is being triggered every 15 minutes, the next step is to ensure that the logs are correctly uploaded to your Blob Storage. Verify SAS URL Configuration:
    Ensure that the sasUrl parameter is correctly set in the payload of the InvokeUploadModuleLogs function. This URL should have the necessary permissions to write to the Blob Storage container.
    Check Environment Variables:
    Confirm that all required environment variables are correctly set in your Function App. These typically include the IoT Hub connection string, device ID, module ID, and the SAS URL for the Blob Storage container.
    Ensure that the function code for InvokeUploadModuleLogs is correctly invoking the direct method with the appropriate payload
    Monitor Logs and Storage:
    Check the logs of your Function App to ensure that the InvokeUploadModuleLogs function is executing without errors,Verify that the logs are being uploaded to the elmsstoragecontainer in your Blob Storage account.
    Permissions and Access:
    Ensure that the SAS URL has the correct permissions (write access) to the Blob Storage container,Confirm that the IoT Edge device has the necessary permissions to upload logs to the specified Blob Storage container.
    you can refer similar thread for more information: https://learn.microsoft.com/en-us/answers/questions/2123684/(duplicate)-elms-how-to-save-module-logs-to-blob-s


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members. 
    User's image

    If you have any other questions or are still running into more issues, let me know in the "comments" and I would be happy to help you.

    1 person found this answer helpful.
    0 comments No comments

  2. Sander van de Velde | MVP 33,716 Reputation points MVP
    2024-11-28T22:41:31.48+00:00

    Hello @Mistyron ,

    welcome to this moderated Azure community forum.

    The supportbundle call is basically just a way to execute a number of direct methods on the edge agent module.

    As seen in this post, you first need to call 'UploadSupportBundle', follow by one or more 'GetTaskStatus' calls until you get the response with status 'completed'.

    I'm not sure what ELMS is but you can use code to trigger direct methods using the IoT Hub Service SDK.

    This could be done using eg. an Azure Function.

    Based on this sample, I created this (untested) C# code:

    private static async Task InvokeMethodAsync(string deviceId, ServiceClient serviceClient)
    {
        var methodInvocation = new CloudToDeviceMethod("UploadSupportBundle")
        {
            ResponseTimeout = TimeSpan.FromSeconds(30),
        };
        methodInvocation.SetPayloadJson("[THE DEFAULT PAYLOAD OF THE UploadSupportBundle CALL]");
    
        Console.WriteLine($"Invoking direct method for EdgeAgent on device: {deviceId}");
    
        // Invoke the direct method asynchronously and get the response from the simulated device.
        CloudToDeviceMethodResult response = await serviceClient.InvokeDeviceMethodAsync(deviceId, "edgeAgent", methodInvocation);
    
        Console.WriteLine($"Response status: {response.Status}, payload:\n\t{response.GetPayloadAsJson()}");
    }
    
    

    You make use of this Nuget package (or a more recent one):

        <PackageReference Include="Microsoft.Azure.Devices" Version="1.39.1" />
    

    Make use of the connection string of the IoT Hub, not the device (and keep it secure).

    Notice that you still need to fill in the right payload for this call.

    Notice also that next to the device name, the module name can be passed on (great for modules on edge devices).

    I'm also not sure if the 'edgeAgent' is correct, please try '$edgeAgent' too.

    This should give you a good starting point.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


  3. Keshavulu Dasari 2,250 Reputation points Microsoft Vendor
    2024-11-29T17:52:27.41+00:00

    Hi Mistyron,
    To automate the execution of the UploadModuleLogs method every 15 minutes, you can use Azure Functions with a timer trigger
    you can set it up through the Azure portal:
    Create a Function App:

    • In the Azure portal, navigate to Create a resource > Compute > Function App.
    • Fill in the required details like subscription, resource group, and function app name.
    • Choose the runtime stack (e.g., JavaScript, PowerShell, C#) and the hosting plan (Consumption plan is usually sufficient).

    Create a Timer Trigger Function:

    Once the Function App is created, go to the Functions section and click + Add to create a new function.

    Select Timer trigger as the template.

    Set the schedule to run every 15 minutes using a CRON expression like 0 */15 * * * *.

    Write the Function Code:

    In the function editor, write the code to call the UploadModuleLogs method.
    Deploy and Test:

    • Save and deploy your function.
    • The function will now run every 15 minutes and trigger the UploadModuleLogs method.

    For more details :
    https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-scheduled-function

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members. 


    If you have any other questions or are still running into more issues, let me know in the "comments" and I would be happy to help you


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.