0

I have an azure function app(v4), which is logging to App Insights.

The function app I have is n tier architecture, so it contains a function app project, domain and data layer.

The domain and data projects are .net 6 class libraries.

The logging seem to work fine with the function app project. There is instrumentation key setup in the Configuration tab in Azure Portal. I do not have any setup for app insights in the startup.cs file.

But the logging from the class libraries are not working. The class Library classes are injecting ILogger in the constructor to log the information/error. Is there any special setup needed for the logging to work?

Newbie
  • 563
  • 1
  • 5
  • 16

2 Answers2

0

One of the workarounds to use logging from Class Library Azure Functions Project is:

Step 1: Add this Package Reference Microsoft.Extensions.Logging.Abstractions* in the Class Library Project.

Step 2: Add a parameter to you class constructor that accepts and ILogger or ILogger<YourClass> if you intend on using DI). Store the logger in a field and use it as needed.

public class MyClass {
   private readonly ILogger _logger;
   public MyClass(ILogger logger) =>
      _logger = logger ?? throw new ArgumentNullException(nameof(logger));

   public void MyMethod(string name) {
      _logger.LogInformation("Hello {Person}", name);
   }
}

Step 3: Pass the logger to your constructor:

FunctionName("clFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestMessage req, ILogger log) {
   log.LogInformation("HTTP trigger function start");
   var class1 = new MyClass(log);
   class1.MyMethod("krishazure");
}

Refer this MSFT Doc for more information.

0

You need to add loglevel on your host.json

{
  "version": "2.0",
  "logging": {
  "applicationInsights": {
   "samplingSettings": {
     "isEnabled": true
   }
   },
  "logLevel": {
   "Yournamespace**": "Information"
  }
 }  
}
ramdev
  • 137
  • 1
  • 9