0

I have an Azure function and I can successfully write logs from the app function but I want to use logger in another class library. I have done in DI and also added the necessary code but it does not log anything.

Azure function code is

enter image description here

Business data service code is

enter image description here

start up class code is. AddLoggin() is optional but it did not work with or without.

enter image description here

host.json file

enter image description here

I can see the logs written form the function app (MaterDataTimerTrigger) but I can't see anything written form the MasterDataBusinessService

Update: I changed the host.json file as per comments and it worked.

{
"version": "2.0",
"logging": {
"logLevel": {
  "default": "Information"
}
}
}
Ali
  • 1,015
  • 14
  • 40
  • Where are you logging? `-Application Insights`? If yes, 1) you don't require `builder.Services.AddLogging.` 2) Please post your host.json file. It should be entry for loglevel. Check [this](https://github.com/amigup/CleanArchitecture-For-AzureFunctionV3/blob/master/CleanArchitecture.Functions/host.json) for reference. – user1672994 Mar 28 '22 at 10:22
  • yes application insight and function app also have its own logging as well. AddLogging() function was optional but even with or without that it did not make any difference. If it writing in the function app but not writing in the other class, I don't thing it is host.josn file issue but I will post it anyway. – Ali Mar 28 '22 at 11:11
  • Can you add the default loglevel entry? `"logging": { "logLevel": { "default": "Information", } }` – user1672994 Mar 28 '22 at 13:43
  • Adding "logging": { "logLevel": { "default": "Information", } } has fixed the issue. What is the reason for this? Why the default host.josn file does not work for logging – Ali Mar 29 '22 at 01:50
  • I've posted as a answer and replied to your question in comment in my answer. – user1672994 Mar 29 '22 at 03:23

1 Answers1

1

From this Microsoft documentation --- The default value for all functions and telemetry categories is set to Warning (including Microsoft and Worker categories) so, by default, all errors and warnings generated by both, the runtime and custom logging, are gathered.

So to have information level, you should define the logLevel in host.json file.

"logging": {
    "logLevel": {
      "default": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }

Also, I would suggest to set the samplingSettings to false so that all logs are logged at application insights instead of sampled one.

user1672994
  • 10,509
  • 1
  • 19
  • 32
  • I understand what you saying but if you look at my code, you would see I am using log.information function only in both Azure function and other class library. It works fine in the function app with old host.josn but does not work in the other class library but changing the host file made it worked in class library as well, so not sure what was the problem. – Ali Mar 29 '22 at 04:38
  • 2
    @Ali - there is a slight difference between ILogger, that is injected into Azure Functions, and ILogger which is injected into your service class - see e.g. https://stackoverflow.com/questions/61019859/azure-functions-injected-iloggert-logs-arent-appearing Setting default to Information will apply to all log sources, not just your code, so be careful of costs. Same for sampleSettings - would recommend to keep enabled. If your root namespace is "MyCompany", then you can configure logLevel with `"MyCompany.": "Information"` instead, so any logging from your code will come out. – AdaTheDev Mar 29 '22 at 14:32
  • I have recently created a 4.X Azure function and the same host.josn file is throwing error, are you able to provide some help? https://stackoverflow.com/questions/72965616/azure-function-4-x-host-josn-logging-breaks – Ali Jul 13 '22 at 11:39