如何将AppInsights添加到类库中

huangapple go评论49阅读模式
英文:

How to add AppInsights into Class Library

问题

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);
    } 
}
英文:

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);
                } 
            }

答案1

得分: 1

对于一个 .Net Framework 类库,你只应该期望通过构造函数参数或依赖注入来传递一个 TelemetryClient(或一个 TelemetryConfiguration,以便你可以创建一个 TelemetryClient)给构造函数。

通常情况下,读取 ApplicationInsights.config 文件并构建客户端是由调用你的类库的代码来完成的。你不应该在类库本身中执行这些操作。

然后,在你的类库中,你可以手动调用 TelemetryClient 的方法,如 TrackEvent(..)TrackException(...)TrackDependency(...) 等。

第 5 和第 6 步负责在控制器级别跟踪未处理的异常。除非你想为调用代码提供异常处理程序,否则你不能在类库中执行这些操作。

所以,除非你想从类库内部手动发送遥测数据,否则在你的类库中不需要担心 Application Insights。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月16日 14:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定