0

I have used application insights in web api. It works well. Currently, our controller need call a class library (referred by nuget package). I need to use application insight in class library. There is no exception but nothing logged in Application insights. I write the code as following. Our TelemetryConfiguration have initialized in controller already.

var telemetryClient = new TelemetryClient();
var customEvent = new Microsoft.ApplicationInsights.DataContracts.EventTelemetry
{
    Name = "helloworld",
};
// customEvent.Metrics.Add({ "latency", 42});
telemetryClient.TrackEvent(customEvent);

What should I do to make the application insights work?

2
  • 1
    "it doesn't work in the library" doesn't explain what's actually happening. Does it throw an exception? Does nothing get logged to AppInsights? Please edit that information into your question. Commented Nov 1, 2019 at 11:02
  • Thank you for your comment. I will edit it. Commented Nov 1, 2019 at 11:03

1 Answer 1

2

Normally the following steps are enough to log to App Insights:

1- In your WebApi startup class and your library project add App Insights assembly thru nuget.

Microsoft.ApplicationInsights

2- Register App Insights in your startup class:

services.AddApplicationInsightsTelemetry(Configuration);

3- Setup your instrumentation key in appsettings.json:

"ApplicationInsights": {
  "InstrumentationKey": "<Your instrumentation key here>"
}

4- In any class you need, inject a TelemetryClient and use it.

using Microsoft.ApplicationInsights

namespace MyNamesPace
{
    public class MyClass
    {
        private readonly TelemetryClient _telemetryClient;

        public MyClass(TelemetryClient telemetryClient)
        {
            _telemetryClient= telemetryClient;
        }

        public myClassMethod()
        {
            // Use your _telemetryClient instance
            _telemetryClient.TrackEvent("Your Telemetry Event");
        }
    }
}

4- In your controller inject your class

namespace MyApiNamesPace
{
    public class MyController : ControllerBase
    {
        private readonly IMyClass _myClass;

        public MyClass(IMyClass myClass)
        {
            _myClass = myClass;
        }

        public IActionResult myAction()
        {
            _myClass.MyClassMethod();
        }
    }
}

5- Don't forget to register your class in your DI container, in startup class:

services.AddScoped<IMyClass, MyClass>();

Happy programming!!

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for your answer. step 1, step 2 have already done. I think my code in class above is almost same as you write in step 3. But nothing get logged to AppInsights. Since my class is another repo, I referred by nuget package. I don't know whether if affect the behavior.
Have you setup your key in appsettings.json ?
In controller, I have set it. But in the repo of my class library, I do not set it.
I've edited my post to explain how to set it up, second point will use this entry in your settings file.
Note that you need to inject the Telemetry in the using class to use the instance and configuration registered in your api project. If you instantiate a new telemetry in your class instead of injecting it, settings are going to get lost.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.