首页 > 解决方案 > Application Insights 是否具有使用属性格式化消息的概念?

问题描述

Windows 事件日志具有带有指向元数据属性的变量的消息的概念。我想知道 Application Insights 是否可以做同样的事情。我在互联网上找不到任何东西。例如:

const string _loginMessage = "{user} logged in at {time}";
...
telemetryClient.TrackTrace(
    _loginMessage,
    new Dictionary<string, string>
    {
        ["user"] = user.Name,
        ["time"] = DateTime.UtcNow
    });

在 Application Insights 中呈现并查看日志时,它将显示完整的消息。

我知道我可以包装 TrackTrace 并自己执行此操作,但如果这是免费的,我不想这样做。

谢谢

标签: azure-application-insights

解决方案


TrackTrace方法不支持这种消息格式。您需要编写自己的逻辑来实现它。

但是对于一些像 web app / azure function 已经ILogger集成了 Application Insights 的项目,你可以LogInformation使用ILogger. 如下代码(这是一个网络项目):

    public IActionResult Index()
    {
        string _loginMessage = "{user} logged in at {time}";
        string user = "myname222";
        string time = "2021-03-03 vvvvv";

        _logger.LogInformation(
            _loginMessage,user,time);

        return View();
    }

运行项目后,您可以在 azure 门户中看到格式化的消息和属性:

在此处输入图像描述


推荐阅读