I'm using App Insights in a .net core web app and I can successfully see the telemetry data in the app insights logs. I would now like to be able to push up my own log events, but cannot seem to get it to work (i.e. my custom log events do not appear in app insights). My code is as follows:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddApplicationInsights(app.ApplicationServices);
loggerFactory.AddDebug();
var logger = loggerFactory.CreateLogger<Startup>();
logger.LogInformation("I am a log event");
I've also tried setting the log level explicitly like:
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug);
But I never see the text "I am a log event" in app insights.
Can anyone tell me how I can fix this?
In the Azure Portal, navigate to the Application Insights resource, and click Log Analytics. Log queries help you to fully leverage the value of the data collected in Azure Monitor Logs. Query your custom events by entering “customEvents” in the prompt and click Run.
Open your project in Visual Studio. Go to Project > Add Application Insights Telemetry. Choose Azure Application Insights, then select Next. Choose your subscription and Application Insights instance (or create a new instance with Create new), then select Next.
Add Application Insights automatically From within your ASP.NET web app project in Visual Studio: Select Project > Add Application Insights Telemetry > Application Insights Sdk (local) > Next > Finish > Close.
Documentation section: Explore .NET trace logs in Application Insights
TelemetryClient
directly. It has more options, not only TrackTrace
that is used by loggers.//TelemetryClient telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();
...
telemetryClient.TrackEvent("<EventName>");
telemetryClient.TrackMetric("<metric name>", 1);
telemetryClient.TrackTrace("message", SeverityLevel.Information);
Documentation section: Application Insights API for custom events and metrics
For the live example, you may look into feature test int ApplicationInsights-aspnetcore
repo.
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