- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 MVC Web 应用程序,并且我正在使用简单注入(inject)器进行 DI。单元测试几乎覆盖了我所有的代码。但是,现在我已经在某些 Controller 中添加了一些遥测调用,我在设置依赖项时遇到了问题。
遥测调用用于将指标发送到 Microsoft Azure 托管的 Application Insights 服务。该应用程序不在Azure中运行,只是在具有ISS的服务器中运行。 AI 门户告诉您有关应用程序的各种信息,包括您使用遥测库发送的任何自定义事件。因此, Controller 需要 Microsoft.ApplicationInsights.TelemetryClient 的实例,该实例没有接口(interface),并且是一个密封类,具有 2 个构造函数。我尝试像这样注册它(混合生活方式与这个问题无关,我只是为了完整性而将其包括在内):
// hybrid lifestyle that gives precedence to web api request scope
var requestOrTransientLifestyle = Lifestyle.CreateHybrid(
() => HttpContext.Current != null,
new WebRequestLifestyle(),
Lifestyle.Transient);
container.Register<TelemetryClient>(requestOrTransientLifestyle);
问题是,由于 TelemetryClient 有 2 个构造函数,SI 会提示并验证失败。我发现一篇文章展示了如何覆盖容器的构造函数解析行为,但这似乎相当复杂。首先我想备份并问这个问题:
如果我不将 TelemetryClient 设置为注入(inject)依赖项(只需在类中创建一个新依赖项),那么该遥测数据是否会在每次运行单元测试时发送到 Azure,从而创建大量虚假数据?或者 Application Insights 是否足够智能,知道它正在单元测试中运行,而不发送数据?
任何对此问题的“见解”将不胜感激!
谢谢
最佳答案
Application Insights 有 example通过模拟 TelemetryChannel
对 TelemetryClient
进行单元测试。
TelemetryChannel
实现了 ITelemetryChannel
,因此很容易模拟和注入(inject)。在此示例中,您可以记录消息,然后从 Items
收集它们以进行断言。
public class MockTelemetryChannel : ITelemetryChannel
{
public IList<ITelemetry> Items
{
get;
private set;
}
...
public void Send(ITelemetry item)
{
Items.Add(item);
}
}
...
MockTelemetryChannel = new MockTelemetryChannel();
TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = MockTelemetryChannel,
InstrumentationKey = Guid.NewGuid().ToString()
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
TelemetryClient telemetryClient = new TelemetryClient(configuration);
container.Register<TelemetryClient>(telemetryClient);
关于c# - 将 Application Insights 与单元测试结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33815397/
我有多个应用程序使用 Application Insights for Production Data。我正在尝试使用 City 遥测字段来映射我们当前的用户。这些数据的跟踪似乎非常不一致,并且在大多
有没有办法在 ASP.NET Web 应用程序中禁用 Application Insights?假设我想关闭生产中运行的应用程序中的所有数据收集。 最佳答案 如果 ikey 在 Application
如何在 Azure Application Insights 中将时差转换为毫秒 let startTime = todatetime('2017-05-15T17:02:23.7148691Z');
我在 Application Insights Analytics 中有一些数据,它有一个动态对象作为自定义维度的属性。例如: | timestamp | name
我从 Chrome 开发工具和 Google Page Speed Insights 页面运行 Google Page Speed Insights(移动)得到了不同的结果。当我从 Chrome 开发
我在我的 ASP.NET MVC Angular 应用程序中使用 App insights。我在我的布局文件中插入了 JavaScript block (我得到了 from the Microsoft
在我们当前的本地设置中,我们有 20 多个 .net core 3.1 API 应用程序(单独的 ASP.NET Core API 应用程序)。我们已开始将 2 个 APi 应用程序迁移到使用单个 A
我有一个应用洞察查询。在此查询中,我想将多个列加入/合并为一个列以显示如何实现这一点。 我想结合 ip、城市、州、国家。 customEvents | where timestamp >= ago(7
我有一个托管在 Windows Server 2008 上的 ASP.Net MVC 4 应用程序。我使用的是 Microsoft Application Insights,它非常适用于客户端指标,例
我已经开始使用应用程序洞察来记录来自控制台应用程序的消息。 仅记录严重和错误。未记录信息或跟踪。关于为什么排除信息的任何想法? class Program { static void Mai
我将 App Service 捆绑的 App Insights 代理与 .Net 4.7 应用程序一起使用,并且没有使用 SDK。我配置代理的唯一选项是应用程序设置,如文档 https://docs.
我正在使用 Application insights 进行由外向内测试,但我的代码未使用它进行检测。我不想收到关于我的服务的每周电子邮件摘要,因为大多数栏目都是空白的。如何禁用正在发送的电子邮件? (
这不是一个特别技术性的问题,但是其他人的洞察数据有问题吗? http://www.facebook.com/insights/?sk=ao_119242438097337 自 18 日(6 天前)以来
我们的 ASP.NET MVC 应用程序包含一些 URI 路径参数,例如: https://example.com/api/query/14hes1017ceimgS2ESsIec 在 Applica
我想使用一个包含数组的变量,所以我可以将它与 in 过滤器一起使用。 这行得通: traces | where cloud_RoleName in ("A", "B") 这不起作用(语法错误): le
我的 Facebook Insights 帐户中添加了 Facebook“Insights For My Domain”。我有一个不再使用的旧域,想从我的 Facebook Insights 帐户中删
有人使用过Sonatype Insight-Application Health Checker吗?它要求您扫描应用程序(war、jar、zip 等),然后将其结果上传到Sonatype Insigh
我正在使用这个引用 documentation用于应用洞察。 如何使用不同查询的输出进行子选择? //Query 1 Events | where Timestamp >= ago(30min) a
我正在试用预览版,并尝试将见解添加到本地部署在我机器上的 IIS Web 应用程序。它是一个运行在普通应用程序池中的 .Net 4.5 应用程序。添加见解后启动应用程序时,出现此异常: 无法加载文件或
如何将 Application insights 最终用户用户分析添加到 DocFx 以跟踪每个文档页面的使用情况? Application Insights 文档指出您需要在结束标记之前立即插入下面
我是一名优秀的程序员,十分优秀!