Application Insight 的 TelemetryClient 线程安全吗?
Is Application Insight's TelemetryClient Thread Safe?
关于这个 link:https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/
它明确表示:
TelemetryClient is thread-safe.
We recommend you use an instance of TelemetryClient for each module of your app.
但是,MSDN 文档 (https://msdn.microsoft.com/en-us/library/azure/microsoft.applicationinsights.telemetryclient.aspx) 说:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
所以问题是,大多数函数(例如 TrackEvent 和 TrackMetric)都不是静态的。如果我遵循第一篇文章,为我的 Web 服务创建一个单例实例,我会 运行 陷入线程问题吗?
TelemetryClient 是线程安全的。一个有效的用法是创建一个单例并重用它。您不会 运行 陷入重用实例的问题。
MSDN 文档在说给定的 class 不是线程安全的时经常是不正确的。我不确定人们如何标记他们的代码以使这些文档反映 class 的线程安全性,但我已经看到许多这些文档不正确的实例。
the Azure article you linked 的当前版本说:
TelemetryClient is thread-safe.
For ASP.NET and Java projects, incoming HTTP Requests are automatically captured. You might want to create additional instances of TelemetryClient for other module of your app. For instance, you may have one TelemetryClient instance in your middleware class to report business logic events. You can set properties such as UserId and DeviceId to identify the machine. This information is attached to all events that the instance sends.
TelemetryClient.Context.User.Id = "...";
TelemetryClient.Context.Device.Id = "...";
最后一点非常重要。即使 class 是线程安全的,如果您正在编写类似 UserId 可能更改的 Web 应用程序,您可能应该为这些值都相同的每个范围重用遥测客户端的实例(就像每个请求一样),但不是 static/singleton 实例。
更新
在 ASP.NET Core 中,Application Insights 大量使用依赖注入并将 TelemetryClient 注册为单例!如 the docs 中所述:
We don't recommend creating new TelemetryClient
instances in an ASP.NET Core application. A singleton instance of TelemetryClient
is already registered in the DependencyInjection
container, which shares TelemetryConfiguration
with rest of the telemetry. Creating a new TelemetryClient
instance is recommended only if it needs a configuration that's separate from the rest of the telemetry.
这意味着您应该避免在您不希望在整个应用程序中使用的客户端上下文中设置变量,而是利用遥测初始化器来设置类似用户的内容每个遥测对象的 ID。
关于这个 link:https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/
它明确表示:
TelemetryClient is thread-safe.
We recommend you use an instance of TelemetryClient for each module of your app.
但是,MSDN 文档 (https://msdn.microsoft.com/en-us/library/azure/microsoft.applicationinsights.telemetryclient.aspx) 说:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
所以问题是,大多数函数(例如 TrackEvent 和 TrackMetric)都不是静态的。如果我遵循第一篇文章,为我的 Web 服务创建一个单例实例,我会 运行 陷入线程问题吗?
TelemetryClient 是线程安全的。一个有效的用法是创建一个单例并重用它。您不会 运行 陷入重用实例的问题。
MSDN 文档在说给定的 class 不是线程安全的时经常是不正确的。我不确定人们如何标记他们的代码以使这些文档反映 class 的线程安全性,但我已经看到许多这些文档不正确的实例。
the Azure article you linked 的当前版本说:
TelemetryClient is thread-safe.
For ASP.NET and Java projects, incoming HTTP Requests are automatically captured. You might want to create additional instances of TelemetryClient for other module of your app. For instance, you may have one TelemetryClient instance in your middleware class to report business logic events. You can set properties such as UserId and DeviceId to identify the machine. This information is attached to all events that the instance sends.
TelemetryClient.Context.User.Id = "..."; TelemetryClient.Context.Device.Id = "...";
最后一点非常重要。即使 class 是线程安全的,如果您正在编写类似 UserId 可能更改的 Web 应用程序,您可能应该为这些值都相同的每个范围重用遥测客户端的实例(就像每个请求一样),但不是 static/singleton 实例。
更新
在 ASP.NET Core 中,Application Insights 大量使用依赖注入并将 TelemetryClient 注册为单例!如 the docs 中所述:
We don't recommend creating new
TelemetryClient
instances in an ASP.NET Core application. A singleton instance ofTelemetryClient
is already registered in theDependencyInjection
container, which sharesTelemetryConfiguration
with rest of the telemetry. Creating a newTelemetryClient
instance is recommended only if it needs a configuration that's separate from the rest of the telemetry.
这意味着您应该避免在您不希望在整个应用程序中使用的客户端上下文中设置变量,而是利用遥测初始化器来设置类似用户的内容每个遥测对象的 ID。