Azure Functions Logger using DI - examples in docs dont work

Avi Stokar 6 Reputation points
2021-07-15T14:22:17.59+00:00

I am unable to get my own logger to log when using DI in Azure Functions. I have attached a very simple project which does not work. I have tried using Serilog (as in the attached example) or without it. Neither logs anything.

Additionally, I would like to use a non-generic ILogger, but that causes the DI container to crash with a unable to create an ILogger instance. (Unable to resolve ILogger from Microsoft.Extensions.Logging)

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

1 answer

Sort by: Most helpful
  1. Avi Stokar 6 Reputation points
    2021-07-18T16:06:36.887+00:00

    I found the problem - I had copied the host.json from the example at docs https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
    {
    "version": "2.0",
    "logging": {
    "applicationInsights": {
    "samplingSettings": {
    "isEnabled": true,
    "excludedTypes": "Request"
    }
    },
    "logLevel": {
    "MyNamespace.HttpTrigger": "Information"
    }
    }
    }
    removing the "excludedTypes": "Request" allows the logged messages to appear in the application insights log; however, theu still do not appear in the log stream, even when setting the loglevel using the nameapce (as instructed above in the docs) or adding a default in the loglevel.

    0 comments No comments

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.