【问题标题】:Can I mock data (timestamps) in Application Insights?我可以在 Application Insights 中模拟数据(时间戳)吗?
【发布时间】:2020-01-23 01:53:56
【问题描述】:

我正在使用 Application Insights 来查看我的应用程序的遥测。作为演示,我想用数据填充 App Insights,这样我就可以生成详细的图形和图表来显示潜在的用户场景。我希望我的数据可以跨越一周甚至几个月,但我没有时间等那么久。

是否可以在我的应用程序中手动将时间戳/日期放入我的遥测调用中,例如过去几个月,以便我可以获得当时的信息?

【问题讨论】:

  • Maybe this helps? - 我不确定 AI 是否采用本地时间,或者服务器是否在收到数据时记录,所以这可能无济于事。

标签: c# azure timestamp azure-application-insights telemetry


【解决方案1】:

您可以使用ITelemetryInitializer 来实现。

方法一:

您可以定义一个自定义属性,您可以定义自定义时间戳,而不是直接更改时间戳:

如果使用此方法,在您实现 ITelemetryInitializer 的自定义类中,代码如下:

    public class MyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            DateTimeOffset dateTimeOffset = new DateTimeOffset(new DateTime(2020, 1, 10));

            //define a custom property, which is a date time
            telemetry.Context.GlobalProperties["Custom_timestamp"] = dateTimeOffset.ToString();

        }
     }

执行代码后,您可以看到该属性已添加到 azure 门户中的每个遥测数据中:

在构建查询生成图形时,可以使用这个自定义属性(注意:该属性是字符串类型,所以可以使用内置函数todatetime()进行转换它是日期时间类型)而不是使用时间戳。

方法二:

此方法尝试直接更改时间戳。我可以看到时间戳在本地更改,它不会发送到应用程序洞察力。所以目前,我建议你应该使用方法1。

如下代码:

public class MyTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        //try to directly change the Timestamp, it changes successfully in local(in visual studio), but it does not send to application insights.
        telemetry.Timestamp = new DateTimeOffset(new DateTime(2020, 1, 10));

    }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2018-10-05
    • 2018-08-20
    • 1970-01-01
    • 2012-03-27
    • 2022-10-24
    相关资源
    最近更新 更多