none
Unfied logging in Functions and Cloud Services? RRS feed

  • Question

  • I have an Azure Function and an Azure Cloud Service. Both are written in .NET.

    They share a few functions. The shared code is implemented as a shared project in Visual Studio. Inside these functions, I need to log information.

    For Cloud Services, the recommended way to log seems to be static call to System.Diagnostics.Trace.

    For Azure Functions, the recommended way to log seems to be dependency injection of Microsoft.Extensions.Logging.ILogger

    Is there a recommended way or best practice on how to handle logging from the shared code?

    Thursday, July 18, 2019 1:29 PM

Answers

All replies

  • At this point in time, I do not know of a way to handle logging for shared code between a function and a cloud service. This may be something that might come in the future however Cloud Services being our last legacy product is built on older technology so any changes in the product including ones around logging and cross feature integration with Functions would come in the future. 

    The closest thing I can think of might be integration of application insights

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/cloudservices

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#enable-application-insights-integration

    I believe you could enable App Insights for both your Cloud Service and your function app and send the data to the same Application Insights Workspace. Again, I have not personally tried this but might be worth a shot. 

    Thursday, July 18, 2019 5:31 PM
    Moderator
  • Thanks for your suggestion Micah. As far as I can see your first link suggests logging using System.Diagnostics.Trace and the second link suggests using Microsoft.Extensions.Logging.ILogger, exactly as I described in my question.

    It is indeed true that both these logging methods can be collected in Application Insights, but that's not was I am asking for. Sorry for being unclear.

    What I am looking for is a way to call a log function in the shared code, where that code works when the shared code is deployed on my Cloud service and when the same code is deployed in an Azure function.

    To my knowledge, Microsoft.Extensions.Logging.ILogger is not available from a Cloud Service, and System.Diagnostics.Trace is not available from an Azure function.

    Thursday, July 18, 2019 8:45 PM
  • Got it. Sorry for the confusion. 

    I don't believe this is possible. At least nothing I have seen or read about before. As you mention, both services have a different way for logging. Both are separate methods that are not interchangeable. 

    I do believe you could make this work if you were using for example, Azure Web Apps along with Functions. But not if running a Cloud Service and a Function. 

    Thursday, July 18, 2019 9:20 PM
    Moderator
  • Is there any update? Please 'Mark as answer' and 'Upvote' if the reply helped so that it can help other users in the community.
    Thursday, July 25, 2019 3:38 PM
    Moderator
  • Any update on this issue? Did it help?

    Karishma Tiwari - MSFT

    Thursday, August 1, 2019 11:12 PM
    Moderator
  • I decided to go with C# preprocessing directives. This allows me to use the one logging function call in the shared code. When the shared project is compiled for the different environments, appropriate parts of the code will be activated.

    Full details: https://mr-iot.com/2019/08/14/unified-logging-in-azure-functions-and-azure-cloud-services/

    Thursday, August 15, 2019 2:18 PM