Search code examples
azureazure-functionsazure-logic-appsazureservicebusasp.net-core-6.0

Logging not working in .NET 6 Azure Functions with the default code provided by Visual Studio 2022


Code Snippets:

host.json

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

Function Code:

public class SBFuncTrigger
    {
        private readonly ILogger<Function1> _logger;

        public Function1(ILogger<Function1> log)
        {
            _logger = log;
        }

        [FunctionName("SBFuncTrigger")]
        public void Run([ServiceBusTrigger("sampletopic", "sbtopicsubscription", Connection = "ServiceBusConnStr")]string mySbMsg)
        {
            _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }
    }

Output:

For detailed output, run func with --verbose flag.
 Host lock lease acquired by instance ID '<Some_Id>'.
 Executing 'SBFuncTrigger' (Reason='(null)', Id=
<Some_Id>)
 Trigger Details: MessageId: 
<Some_Id>, SequenceNumber: 8, DeliveryCount: 1, EnqueuedTimeUtc: , LockedUntilUtc: , SessionId: (null)
 Executed 'Function1' (Succeeded, Id=
<Some_Id>, Duration=xxxms)

I'm not getting the msg in output:

C# ServiceBus topic trigger function processed message: {mySbMsg}

Details/Implementation:

  1. Created logic app for sending message to topic subscription with content "Hello to Azure Functions Service Bus" enter image description here

  2. Azure Functions Service Bus Topic Trigger - created with a topic and subscription in the Azure Portal Service Bus Namespace.

  3. Logic App Workflow connected to Service Bus Topic Subscription for message transfer.

  4. Connection string of service bus ServiceBusConnStr is defined in local.settings.json


Solution

  • You’re missing the ILogger Class in the Function Declaration of the Service Bus Trigger you have provided.

    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    
    namespace KrishSBNet6FunctionApp
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
            public Function1(ILogger<Function1> log)
            {
                _logger = log;
            }
    
            [FunctionName("Function1")]
            public void Run([ServiceBusTrigger("messagetopic", "topicsubsn", Connection = "SBConnection")] string mySbMsg, ILogger _logger)
            {
                _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
            }
        }
    }
    
    

    enter image description here