Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom logging to app insights from .net core

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?

like image 881
Slicc Avatar asked Jun 08 '17 14:06

Slicc


People also ask

How do I log custom events in application insights?

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.

How do you integrate application insights in .NET Core?

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.

How do I add applications insights to .NET framework?

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.


1 Answers

  1. You need to add an Application Insights log capture module for that. Select the appropriate package - one of:
    • Microsoft.ApplicationInsights.TraceListener (to capture System.Diagnostics.Trace calls)
    • Microsoft.ApplicationInsights.EventSourceListener (to capture EventSource events)
    • Microsoft.ApplicationInsights.EtwListener (to capture ETW events)
    • Microsoft.ApplicationInsights.NLogTarget
    • Microsoft.ApplicationInsights.Log4NetAppender

Documentation section: Explore .NET trace logs in Application Insights

  1. Also, you may use 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.

like image 200
Set Avatar answered Oct 04 '22 21:10

Set