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?

Liam
  • 27,717
  • 28
  • 128
  • 190
highbury zhu
  • 97
  • 1
  • 1
  • 9
  • 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. – Ian Kemp Nov 01 '19 at 11:02
  • Thank you for your comment. I will edit it. – highbury zhu Nov 01 '19 at 11:03

1 Answers1

1

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!!

Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18
  • 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. – highbury zhu Nov 01 '19 at 11:41
  • Have you setup your key in appsettings.json ? – Mauricio Atanache Nov 01 '19 at 11:44
  • In controller, I have set it. But in the repo of my class library, I do not set it. – highbury zhu Nov 01 '19 at 11:48
  • I've edited my post to explain how to set it up, second point will use this entry in your settings file. – Mauricio Atanache Nov 01 '19 at 11:50
  • 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. – Mauricio Atanache Nov 01 '19 at 11:54
  • Yes, I think this might be where the problem lies. How can I inject the Telemetry in using class? In my api project, I use the interface provided by class library. Pass it my parameter when initial the class? – highbury zhu Nov 01 '19 at 12:02
  • No need, keep injecting your class in the container in normal way, let me edit the post. – Mauricio Atanache Nov 01 '19 at 12:04
  • Thank you very much guys. Since I am not familiar in addscoped, so this may cost a bit time for me to try this. – highbury zhu Nov 01 '19 at 12:36
  • One more thing, since in our repo of webapi, there is no startup.cs, where should I put it? – highbury zhu Nov 01 '19 at 12:43
  • What kind of project are you using for your webapi – Mauricio Atanache Nov 01 '19 at 12:52
  • Maybe it is a common thing that webapi without startup.cs ?https://stackoverflow.com/questions/43076150/asp-net-web-api-startup-cs-doesnt-exist – highbury zhu Nov 01 '19 at 14:21
  • Anyway, thank you, I know the direction to solve the problem – highbury zhu Nov 01 '19 at 14:22
  • You may be using Dotnet framework version and not dotnet core version, in this case, is harder to set up because framework version do not have an IOC container out of the box, you will need to use a third party component, on the other hand, with dotnet core IOC container is a first class citizen. – Mauricio Atanache Nov 01 '19 at 14:38