skip to Main Content

We want to get an ILogger instance so that it can be passed to other library.
We tried below, but the ILogger instance does not log into Application Insights. It logs into Event Viewer successfully.

        var serviceCollection = new ServiceCollection();
        serviceCollection.AddLogging(builder => builder
        .AddFilter("Default", LogLevel.Information)
        .AddFilter("Microsoft", LogLevel.Warning)
        .AddFilter("System", LogLevel.Warning)
        .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; })
        .AddApplicationInsights(telemetry =>
        telemetry.ConnectionString = "my-key",
        options => options = new ApplicationInsightsLoggerOptions()));


        var serviceProvider = serviceCollection.BuildServiceProvider();
        var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
        var logger = loggerFactory.CreateLogger("my-logger");
        logger.LogInformation("Hi");

We have added the necessary packages i.e. Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.ApplicationInsights

Is there no way, we can get an ILogger instance from ServiceCollection for AppInsights?

2

Answers


  1. Chosen as BEST ANSWER

    I figured out that it was not logging to AppInsights because there was no TelemetryChannel or TelemetryClient properly configured.

    Approach 1 - Using Telemetry Channel

    using (var channel = new InMemoryChannel()) {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.Configure<TelemetryConfiguration>(config => config.TelemetryChannel = channel);
                    serviceCollection.AddLogging(builder => builder
                    .AddFilter("Default", LogLevel.Information)
                    .AddFilter("Microsoft", LogLevel.Warning)
                    .AddFilter("System", LogLevel.Warning)
                    .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; })
                    .AddApplicationInsights(config => config.ConnectionString = "my-key",
                    options => { }));
                    var serviceProvider = serviceCollection.BuildServiceProvider();
                    var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
                    var logger = loggerFactory.CreateLogger("my-logger");
                    logger.LogInformation("my new try");                 channel.Flush();
                    System.Threading.Thread.Sleep(1000);
                }
    

    Approach 2 - Injecting Telemetry Client using the Microsoft.ApplicationInsights.WorkerService package

    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)
                .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; }));
    
                var serviceProvider = serviceCollection.BuildServiceProvider();
                var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
                var telemetryClient = serviceProvider.GetService<TelemetryClient>();
                var logger = loggerFactory.CreateLogger("my-logger");
                logger.LogInformation("my new try");                 
                telemetryClient.Flush();
                System.Threading.Thread.Sleep(1000);
    

    Both approaches will work fine; we used approach 2.


    • If you are using visual studio then you can configure the application Insights directly through the connection service option.

    • Just click on the conneted service file in the solution explorer
      enter image description here

    • After that click on Add a service dependency to configure the application insights
      enter image description here

    • Now a pop will emerge in which select Azure Application Insights then click on next.
      enter image description here

    • Now select an application Insghts from the list or create a new one .

    enter image description here

    • Now just continue to click next / finish until the process is complete. Thus, now you have configured the application insights in your web form and traces will start appearing in the application insights.

    Azure portal output :-
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search