azure-application-insights - 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 并自己执行此操作,但如果这是免费的,我不想这样做。
谢谢
解决方案
该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 门户中看到格式化的消息和属性:
推荐阅读
- php - 使用 CASE 查询时产生的别名问题
- javascript - 如何在不丢失格式的情况下实现 sql 的代码折叠/折叠
- intellij-idea - gradlew clean deployNodes - QUASAR 警告:Quasar Java 代理未运行
- soap - 错误:在 Sabre 发送 CreatePassengerNameRecordRS 请求时出现 ERR.SP.BUSINESS_ERROR
- c++ - 什么是 msvc_x64_x64 以及与 msvc_x64 有何不同
- c# - 如何在 C# 中隐藏打印弹出窗口?
- python - 损失在 Pytorch 中不会收敛,但在 TensorFlow 中会收敛
- javascript - 为什么打印此变量会显示提示,而不是错误消息?
- git - 试图从功能分支中挑选:没有“樱桃”?
- sql - 使用 HQL 计算两个字段的差异之间的范围