ApplicationInsights - 如何跟踪 WebAPI 使用情况
ApplicationInsights - How to Track WebAPI Usage
我已经使用 ApplicationInsights 几个月了。
虽然它通常对 MVC 和 Web 应用程序非常有用,但我有一些 WebAPI 服务应用程序,我想跟踪它们的使用情况。
例如,我的应用程序中有不同的 RESTful API,我想查看每天(或一段时间内)调用了多少次来比较使用情况。
ApplicationInsights 中的用法选项卡似乎是为 HTML Web 应用程序量身定制的。
有没有办法将其用于服务?
如果我答对了你的问题,你想根据用户确定每个 API 的使用情况。您已经为每个 API 提供了请求数据,但无法根据用户对其进行过滤,因为我们不会跟踪每个遥测事件的用户。
如果您想根据用户或任何 属性 过滤遥测数据(请求、异常等),您需要它在遥测事件的属性中可用,以便您可以直接过滤它并在指标资源管理器中生成图表。为此,您可以创建一个 TelemetryInitializer
.
遥测初始化程序允许您添加全局属性以及所有遥测数据,这意味着它将针对从您的应用程序发送的所有遥测事件执行。下面是跟踪 UserID 以及每个遥测事件的示例。我假设 User.Identity
将为您设置,因为您正在使用 Windows 身份验证。只要在 Http 请求中设置 IPrinicipal
,这也适用于任何其他身份验证(Azure AD、基于证书的身份验证等)。
public class UserTelemetryIntitializer : ITelemetryInitializer
{
public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
{
var context = HttpContext.Current;
if (context == null)
return;
// If user has authenticated, add a property UserID to telemetry which will have
// * domain\username for Windows Authentication
// * username@domain.com for Azure AD Authentication
if (context.User.Identity.IsAuthenticated)
telemetry.Context.Properties["UserID"] = context.User.Identity.Name;
else
telemetry.Context.Properties["UserID"] = "Null";
}
}
不要忘记加载遥测初始化器,使用 ApplicationInsights.config
:
<ApplicationInsights>
<TelemetryInitializers>
<!-- Fully qualified type name, assembly name: -->
<Add Type="MvcWebApp.Telemetry.UserTelemetryIntitializer, MvcWebApp"/>
...
</TelemetryInitializers>
</ApplicationInsights>
或者,您也可以使用代码 而不是 加载初始化程序。在您的 global.asax.cs
或 WebApiConfig.cs
中:
protected void Application_Start()
{
TelemetryConfiguration.Active.TelemetryInitializers
.Add(new UserTelemetryIntitializer());
}
您可以阅读有关遥测初始化器的更多信息here。
我已经使用 ApplicationInsights 几个月了。 虽然它通常对 MVC 和 Web 应用程序非常有用,但我有一些 WebAPI 服务应用程序,我想跟踪它们的使用情况。
例如,我的应用程序中有不同的 RESTful API,我想查看每天(或一段时间内)调用了多少次来比较使用情况。
ApplicationInsights 中的用法选项卡似乎是为 HTML Web 应用程序量身定制的。
有没有办法将其用于服务?
如果我答对了你的问题,你想根据用户确定每个 API 的使用情况。您已经为每个 API 提供了请求数据,但无法根据用户对其进行过滤,因为我们不会跟踪每个遥测事件的用户。
如果您想根据用户或任何 属性 过滤遥测数据(请求、异常等),您需要它在遥测事件的属性中可用,以便您可以直接过滤它并在指标资源管理器中生成图表。为此,您可以创建一个 TelemetryInitializer
.
遥测初始化程序允许您添加全局属性以及所有遥测数据,这意味着它将针对从您的应用程序发送的所有遥测事件执行。下面是跟踪 UserID 以及每个遥测事件的示例。我假设 User.Identity
将为您设置,因为您正在使用 Windows 身份验证。只要在 Http 请求中设置 IPrinicipal
,这也适用于任何其他身份验证(Azure AD、基于证书的身份验证等)。
public class UserTelemetryIntitializer : ITelemetryInitializer
{
public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
{
var context = HttpContext.Current;
if (context == null)
return;
// If user has authenticated, add a property UserID to telemetry which will have
// * domain\username for Windows Authentication
// * username@domain.com for Azure AD Authentication
if (context.User.Identity.IsAuthenticated)
telemetry.Context.Properties["UserID"] = context.User.Identity.Name;
else
telemetry.Context.Properties["UserID"] = "Null";
}
}
不要忘记加载遥测初始化器,使用 ApplicationInsights.config
:
<ApplicationInsights>
<TelemetryInitializers>
<!-- Fully qualified type name, assembly name: -->
<Add Type="MvcWebApp.Telemetry.UserTelemetryIntitializer, MvcWebApp"/>
...
</TelemetryInitializers>
</ApplicationInsights>
或者,您也可以使用代码 而不是 加载初始化程序。在您的 global.asax.cs
或 WebApiConfig.cs
中:
protected void Application_Start()
{
TelemetryConfiguration.Active.TelemetryInitializers
.Add(new UserTelemetryIntitializer());
}
您可以阅读有关遥测初始化器的更多信息here。