Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject App Insights for Custom Events and Metrics

What is the "right" way to use custom events and metrics for app insights in ASP.NET Core with dependency injection? Is there a way to inject TelemetryClient?

Everything I can find instantiates the TelemetryClient directly and TelemetryClient doesn't implement an interface.

like image 611
spottedmahn Avatar asked Jun 18 '18 19:06

spottedmahn


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.

What are events in application Insights?

In Application Insights, a custom event is a data point that you can display in Metrics Explorer as an aggregated count and in Diagnostic Search as individual occurrences. (It isn't related to MVC or other framework "events.") Insert TrackEvent calls in your code to count various events.

How do you add app Insights to app?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.

What telemetry is automatically collected by application Insights?

Application Insights sends telemetry from your web application to the Azure portal so that you can analyze the performance and usage of your application. The telemetry model is standardized, so it's possible to create platform and language-independent monitoring.


2 Answers

TelemetryClient is automatically injected to DI when you configure Application Insights using either .UseApplicationInsights() or AddApplicationInsights() methods. You can use constructor injection to get the TelemetryClient instance as shown below.

public class HomeController : Controller
{
    private TelemetryClient telemetry;

    public HomeController(TelemetryClient telemetry)
    {
        this.telemetry = telemetry;
    }

    public IActionResult Index()
    {
        this.telemetry.TrackEvent("HomePageRequested");
        return View();
    }
}

https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#track-custom-traceeventmetric

like image 157
cijothomas Avatar answered Oct 05 '22 14:10

cijothomas


i'm not sure you need to do dependency injection for that part.

other dependency injected stuff, like .UseApplicationInsights() does the work to ensure application insights is configured, etc.

after that, code that would do custom events or metrics normally create a TelemetryClient on demand, set shared context on the instance, and then write events/metrics through it, via the TrackEvent, TrackMetric, etc methods. Presuming AI is correctly configured in other places, those new telemetry client instances will have other shared/preconfigured/injected settings ready to go.

like image 30
John Gardner Avatar answered Oct 05 '22 16:10

John Gardner