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?
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.
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.
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.
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.
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
):
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With