Application Insights 是否有使用属性格式化消息的概念?
Does Application Insights have the concept of formatted messages using properties?
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
方法不支持这种消息格式。您需要编写自己的逻辑来实现它。
但是对于某些项目,例如 ILogger
与 Application Insights 集成的 Web 应用程序/Azure 函数,您可以使用 ILogger
中的 LogInformation
方法。代码如下(这是一个网络项目):
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 portal中可以看到格式化的消息和属性:
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
方法不支持这种消息格式。您需要编写自己的逻辑来实现它。
但是对于某些项目,例如 ILogger
与 Application Insights 集成的 Web 应用程序/Azure 函数,您可以使用 ILogger
中的 LogInformation
方法。代码如下(这是一个网络项目):
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 portal中可以看到格式化的消息和属性: