I have set up to log exceptions from my .net application to be viewed from App Insights. It was working fine earlier, but now my new errors are not appearing in app insights?

Pamodi Rathnayake 0 Reputation points
2025-04-02T05:47:21.5666667+00:00

Below is my code. I need to understand why its not working?maybe there is a new change in app insights.

if (loggerProviderKey == "ApplicationInsights")

{

var appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];

if (!string.IsNullOrEmpty(appInsightsConnectionString))

{

IOptions<TelemetryConfiguration> telemetryConfigurationOptions = Options.Create(new TelemetryConfiguration()

{

ConnectionString = appInsightsConnectionString,

TelemetryInitializers = { new HbsTelemetryInitializer() }

});

IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions = Options.Create(new ApplicationInsightsLoggerOptions());

// Add the ApplicationInsightsLoggerProvider to the logger factory

_loggerFactory.AddProvider(new ApplicationInsightsLoggerProvider(

telemetryConfigurationOptions,

applicationInsightsLoggerOptions));

}

}

// Create a new logger instance using the logger factory

_logger = _loggerFactory.CreateLogger(_categoryName);

Developer technologies | C#
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Anonymous
    2025-04-02T09:17:07.3566667+00:00

    Hi @Pamodi Rathnayake , Welcome to Microsoft Q&A,

    It is recommended to configure Application Insights in appsettings.json instead of using ConfigurationManager.AppSettings:

    {
      "ApplicationInsights": {
        "ConnectionString": "InstrumentationKey=你的InstrumentationKey"
      }
    }
    

    Do not create TelemetryConfiguration manually, use services.AddApplicationInsightsTelemetry() instead. Use dependency injection (DI) to manage ILoggerFactory, do not create ApplicationInsightsLoggerProvider manually.

    var services = new ServiceCollection();
    var appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];
    
    if (!string.IsNullOrEmpty(appInsightsConnectionString))
    {
        services.AddApplicationInsightsTelemetry(options =>
        {
            options.ConnectionString = appInsightsConnectionString;
        });
        services.AddLogging(logging =>
        {
            logging.AddApplicationInsights();
        });
        var serviceProvider = services.BuildServiceProvider();
        _loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
        _logger = _loggerFactory.CreateLogger(_categoryName);
    }
    

    If an error occurs, please let me know the specific error message and I will follow up.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Pamodi Rathnayake 0 Reputation points
    2025-04-07T10:29:43.7833333+00:00

    I have written a error handling framework using ILoggerFactory in .Net Framework 4.8

    It supports multiple providers including Application Insights.

    Recently I checked the Application Insights Logs and observed that none of my logs are not logging into the app insights.

    1. I was in the correct directory
    2. I have set data sampling to 100%
    3. I have configured the correct connection string
    4. I have installed Fiddler and checked if the request is hitting the Ingestion Endpoint and it does, but it had no response body.Image 1
    5. When called the IngestionEndpoint via powershell, it appeared in the portal.and it had a response body.

    Image 2

    1. My Code
    
    public HbsExceptionLogger(string categoryName)
    
    {
    
        _categoryName = categoryName;
    
        var minimumLogLevelSetting = ConfigurationManager.AppSettings["MinimumLogLevel"];
    
        if (!Enum.TryParse(minimumLogLevelSetting, true, out _minimumLogLevel))
    
        {
    
            _minimumLogLevel = LogLevel.Warning; // Default to Warning if the setting is not a valid LogLevel
    
        }
    
        // Create a new logger factory instance
    
        _loggerFactory = new LoggerFactory();
    
        var loggerProviderKey = ConfigurationManager.AppSettings["LoggerProvider"];
    
        if (loggerProviderKey == "ApplicationInsights")
    
        {
    
            var appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];
    
            //if (!string.IsNullOrEmpty(appInsightsConnectionString))
    
            //{
    
            //    IOptions<TelemetryConfiguration> telemetryConfigurationOptions = Options.Create(new TelemetryConfiguration()
    
            //    {
    
            //        ConnectionString = appInsightsConnectionString,
    
            //        TelemetryInitializers = { new HbsTelemetryInitializer() }
    
            //    });
    
            //    IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions = Options.Create(new ApplicationInsightsLoggerOptions());
    
            //    // Add the ApplicationInsightsLoggerProvider to the logger factory
    
            //    _loggerFactory.AddProvider(new ApplicationInsightsLoggerProvider(
    
            //        telemetryConfigurationOptions,
    
            //        applicationInsightsLoggerOptions));
    
            //}
    
            var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
    
            telemetryConfiguration.ConnectionString = appInsightsConnectionString;
    
            var telemetryInitializer = new HbsTelemetryInitializer();
    
            telemetryConfiguration.TelemetryInitializers.Add(telemetryInitializer);
    
            var telemetryConfigOptions = Options.Create(telemetryConfiguration);
    
            var options = new ApplicationInsightsLoggerOptions();
    
            var appInsightsLoggerOptions = Options.Create(options);
    
            telemetryConfiguration.TelemetryChannel = new InMemoryChannel
    
            {
    
                DeveloperMode = true
    
            };
    
            var aiLoggerProvider = new ApplicationInsightsLoggerProvider(telemetryConfigOptions, appInsightsLoggerOptions);
    
            _loggerFactory.AddProvider(aiLoggerProvider);
    
        }
    
        else if (loggerProviderKey == "Serilog")
    
        {
    
            // Create a new logger that enriches log events with the category name
    
            var serilogLogger = SerilogLoggerSingleton.Instance.ForContext("CategoryName", _categoryName);
    
            _loggerFactory.AddProvider(new Serilog.Extensions.Logging.SerilogLoggerProvider(serilogLogger));
    
        }
    
        else if (loggerProviderKey == "Log4Net")
    
        {
    
            //Add Log4Net provider to the logger factory
    
            _loggerFactory.AddProvider(new Log4NetProvider());
    
        }
    
        else
    
        {
    
            // Add the default logger provider to the logger factory
    
        }
    
        // Create a new logger instance using the logger factory
    
        _logger = _loggerFactory.CreateLogger(_categoryName);
    
    }
    
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    
    {
    
        
    
        if (!IsEnabled(logLevel))
    
        {
    
            return;
    
        }
    
        // Create a new logger instance using the logger factory
    
        //var logger = _loggerFactory.CreateLogger(_categoryName);
    
        // Log the message using the logger instance
    
        _logger.Log(logLevel, eventId, state, exception, formatter);
    
    }
    
    
    0 comments No comments

  3. Pamodi Rathnayake 0 Reputation points
    2025-04-07T17:35:34.5566667+00:00

    after almost breaking my head i found out. It is not working when regional ingestion end point is set, but when global ingestion is set it is working fine

    but when I used the suggested way.(Regional End Point)  its not working. any idea?

    Regional ingestion end point : https://eastasia-0.in.applicationinsights.azure.com

    Global Ingestion end point : https://dc.services.visualstudio.com/

    0 comments No comments

  4. Pamodi Rathnayake 0 Reputation points
    2025-04-08T03:38:05.15+00:00

    Found the answer. Need to have Tls12 in order to communicate with the ingestion end point. After setting this it worked.User's image

    User's image

     System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.