0

I have an aspnet core 2 web app which relies on a "Business" project to handle some logic. I am trying to set up the web app so that its ILogger logs are sent to App Insights. I can send the logs fine if I call the logger.Log method from within its Controller Actions. However, when making calls to classes in another project, which is part of the same solution, where I have an instance of ILogger and logging from there, it doesn't send any log data to App Insights. Am I missing something here? I would imagine if I have configured the logging and app insights in the web app correctly, I can call any other library from there and the logging data would be sent fine.

Riz
  • 6,486
  • 19
  • 66
  • 106
  • How are you acquiring the logger in the library? If you acquire it through DI like in the web app project, it should be a similarly configured logger. – juunas Aug 25 '21 at 05:47
  • have you crosschecked instrumentation key – Sarang Kulkarni Aug 25 '21 at 05:51
  • I'm afraid you also need to add configuration in that project to make that project also integrate azure app insights. In app insights documents, it doesn't introduce the scenario you mentioned. Or in other words, if your business project has appsettings.json and startup.cs as well, you may also need to set configuration in that project. – Tiny Wang Aug 25 '21 at 07:52
  • business project doesn't have startup.cs or appsettings because it's not a standalone project. It's just a library. – Riz Aug 25 '21 at 13:06
  • 1
    My guess comes from 2 points, firstly, shall we turn down the [log level](https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#logging-level) to Trace in appsettings to make sure the logs from library isn't be filtered. And then, I think we need to check if the log instances in the library uses `Microsoft.ApplicationInsights.AspNetCore` – Tiny Wang Aug 26 '21 at 02:12
  • @Riz Did you try the ways suggested by Tiny Wang? – Ecstasy Aug 27 '21 at 06:32

1 Answers1

0
  1. Approach 1 - You will have to register your business logic class in Core API project in Startup; something similar to below
builder.Services.AddScoped<IMyClass, MyClass>();

And you will have to define a constructor which takes an ILogger instance in your business class; something like below

private readonly ILogger<MyClass> _logger;
public MyClass(ILogger<MyClass> logger)
{
    _logger = logger;
}

// _logger.LogInformation("Hi from MyClass"); e.g. logging

This approach means ILogger will be injected with all required settings into your business lib.

  1. Approach 2 - You will have to install the AppInsights worker package in your business lib project Microsoft.ApplicationInsights.WorkerService from here

Then, you will have to write code for logging into AppInsights, something like below

var serviceCollection = new ServiceCollection();

            serviceCollection.AddApplicationInsightsTelemetryWorkerService(options => options.ConnectionString = "my-key");
            serviceCollection.AddLogging(builder => builder
            .AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information)
            .AddFilter("Default", LogLevel.Information)
            .AddFilter("Microsoft", LogLevel.Warning)
            .AddFilter("System", LogLevel.Warning)
            );

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
            var telemetryClient = serviceProvider.GetService<TelemetryClient>();
            var logger = loggerFactory.CreateLogger("my-logger");
            logger.LogInformation("Hi from MyClass");
            // flush and sleep at the end, before returning to the caller so that no messages are lost
            telemetryClient.Flush(); 
            System.Threading.Thread.Sleep(5000);

You would need the Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Logging packages in your business lib project for this approach. This approach means you will write appinsights logging independently to your lib project.

Jatin
  • 83
  • 7