0

I am facing issues in logging messages from the service class of Azure Function app. There are no issues in logging from the function class. I followed the solution mentioned on Azure Functions - ILogger Logging across classes but cant figure out the issue.

host.json

{
    "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "FunctionApp.Services.RestService": "Information"
    }
  }
}

RestService.cs

public class RestService : IRestService
    {
        private readonly ILogger<IRestService> _logger;
        public RestService(ILogger<IRestService> logger)
        {
            _logger = logger;
        }
        public async Task<RestResponse> SampleMethod()
        {
            _logger.LogInformation("************************************TEST THIS WORK************************************","1212121212");
        }
    }

IRestService.cs

public interface IRestService
    {
        Task<RestResponse> SampleMethod(string url, string requestBody, string soapAction);
    }

I am calling the RestService class from the function app.

Startup.cs

public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton<IRestService, RestService>();
        }
    }

As mentioned earlier, the log messages from the function class appears in App Insight but not from the service class. What am I missing?

Tarun Bhatt
  • 727
  • 2
  • 8
  • 28

1 Answers1

0

I have reproduced in my environment and got expected results as below:

You need to change Host.json file.

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "logLevel": {
      "Function": "Information"
    }
  }
}

enter image description here

Taken Code from (for Class, Interface class, Startup and main function) reference (Link)

I have written similar code as you but changed Host.json code. Try to change hots.json and you will get output as I have got.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7