-1

I went through the following tutorial to add AppInsights into a C# project

https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net

Using the manual method. All worked as expected.

I now want to add this same feature into a .Net Framework Class Library.

Following that tutorial I can't carry out steps 5 and 6 because they are explicitly used in an MVC project.

How could I add or what is the equivalent code for steps 5 and 6 from that link to add the same code into a .Net Framework Class Library?

Edit 1

So after implementing the manual method in an MVC app all looks good.

In my Class Library in the constructor i have added similar code as below

private TelemetryClient _telClient;

public class SomeClass(TelemetryClient telClient)
{
    _telClient = new TelemetryClient();
}

public void SomeMethod()
{
    _telClient.TrackException(new Exception("Hello World");
}

In my MVC app i have the below code

 if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
            {
                //If customError is Off, then AI HTTPModule will report the exception
                if (filterContext.HttpContext.IsCustomErrorEnabled)
                {   
                    var ai = new TelemetryClient();
                    ai.TrackException(filterContext.Exception);
                    SomeClass sc = new SomeClass(ai);
                } 
            }
Computer
  • 2,149
  • 7
  • 34
  • 71

1 Answers1

1

For a .Net Framework Class library you should only expect a TelemetryClient (or a TelemetryConfiguration so you can create a TelemetryClient) to be passed to the constructor using a constructor argument or dependency injection.

Typically the reading the ApplicationInsights.config file and constructing a client is done by the code that calls your class library. You shouldn't do that in the class library itself.

Then, in your class library you can manually call the methods of the TelemetryClient like TrackEvent(..), TrackException(...), TrackDependency(...) etc.

Steps 5 and 6 take care of tracking unhandled exceptions on a controller level. You cannot do that in a class library unless you want to provide an exception handler to the calling code.

So, unless you want to manually send telemetry from within your class library you shouldn't bothered about Application Insights at all in your class library.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Thanks! In my class library I do have a telemetryClient but in debug mode i can see all the properties are null. I then added telemetryConfiguration and assigned the connection string property to the one set in the applicationInsights. I can see the code bring run but nothing listed in the AI logs after seeing TrackException with a manual value. So are you suggesting this can't be done in a class library? – Computer Feb 16 '23 at 07:57
  • Can you post soem code like how you get the telemetryClient and add the telemetryConfiguration? Can you see telemetry in the Visual Studio -> View -> Other Windows -> Application Insights Search? Because using a teletryclient in a class library can be done. – Peter Bons Feb 16 '23 at 13:41
  • 1
    Yes i can see data when i load it up using that menu but only if i implement the manual method in an MVC app. I will edit the original question with what i have done and whats the code in the class library – Computer Feb 17 '23 at 09:42
  • @Computer I see the update but now it is not clear to me what the actual problem is, or is everything solved now? – Peter Bons Feb 17 '23 at 09:54
  • Ok so the issue i have is when i add the ai var to the SomeClass constuctor (please see edit again above) it calls the method in the class at another stage i.e. clicking a button to call SomeMethod() but no Exception is listed. – Computer Feb 17 '23 at 10:07