Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core + ApplicationInsights Logging Errors as Trace

I am using Microsoft.ApplicationInsights.AspNetCore (https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore).

I've enabled application insights by adding .UseApplicationInsights() in Programs.cs and in my startup class:

    loggerFactory.AddApplicationInsights(app.ApplicationServices);

This all works fine, I am able to see requests in app insights but when I try to log an error (for example in my controller):

        _logger.LogError("My Error Log");
        _logger.LogError("Test", new Exception("Test"));

Both are logged as trace events and not exceptions in app insights.

How do I make it so it logs as an exception?

like image 471
TheWebGuy Avatar asked Oct 31 '18 02:10

TheWebGuy


People also ask

How do I add application insights to ASP NET Core?

ASP.NET Core applications To add Application Insights telemetry to to ASP.NET Core applications, use the Microsoft.ApplicationInsights.AspNetCore NuGet package. This can be configured from with Visual Studio as a Connected service, or manually.

How do I query and analyze my logs using application insights?

If you use this provider, you can query and analyze your logs by using the Application Insights tools. The logging provider is included as a dependency of Microsoft.ApplicationInsights.AspNetCore, which is the package that provides all available telemetry for ASP.NET Core.

How to enable applicationinsightsloggerprovider in ASP NET Core?

ASP.NET Core applications ApplicationInsightsLoggerProvider is enabled by default for ASP.NET Core applications when ApplicationInsights is configured using Code or Code-less approach. Only Warning and above ILogger logs (from all categories) are sent to Application Insights by default. But you can customize this behavior.

When do I need to capture logs in ASP NET Core?

Consider the following example controller: For more information, see Logging in ASP.NET Core. Some scenarios require capturing logs as part of the app startup routine, before the request-response pipeline is ready to accept requests.


1 Answers

If you want to log the error as Exception in app insights, this line of code _logger.LogError("Test", new Exception("Test")); should be changed.

Change it to _logger.LogError(new Exception(), "test");, which means the new Exception() should be the first paramter.

And you can add application insights SDK by right click your project -> add -> Application Insights Telemetry, which is very useful doing some thing automatically(ie. adding .UseApplicationInsights() in Programs.cs): enter image description here

I also post my test steps:

1.Adding application insights SDK as mentioned above

2.Add loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Information); in Startup.cs -> Configure() method, code as below:

            public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    app.UseHsts();
                }

                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseCookiePolicy();

                app.UseMvc();

                //Add this line of code
                loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Information);
            }

3.Then in somewhere you wanna log error:

        public class AboutModel : PageModel
        {
            private ILogger _logger;

            public AboutModel(ILogger<AboutModel> logger)
            {
                _logger = logger;
            }

            public string Message { get; set; }

            public void OnGet()
            {
                _logger.LogInformation("it is just a test herexxxx");

               //Only this format can log as exception
                _logger.LogError(new Exception(), "it is a new Exceptionxxxx");

               //it will log as trace
                _logger.LogError("error logs xxx");
                Message = "Your application description page.";
            }
        }

4.Test result as below: enter image description here

like image 69
Ivan Yang Avatar answered Oct 10 '22 15:10

Ivan Yang