简体   繁体   中英

how to send logging data to app insights from a library project

I have an as.net 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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM