我需要编写PowerShell脚本shell才能将条目记录到App Insights中。我发现此post很有用,但是我很难写到除此处使用的其他字段以外的其他字段。例如,我正在尝试填充消息字段。我不知道JSON中的字段名称,也不知道应该去哪里。正如您在本示例中看到的那样,我尝试将其放置在任何地方,但仍然无法正常工作。我尝试搜索他们的REST API文档,但仍然找不到JSON的规范。 有人可以帮忙吗?
[{
"name": "Microsoft.ApplicationInsights.Event",
"time": "2018-09-20T16:57:16.1771869Z",
"iKey": "1234",
"message": "This is a message",
"tags": {
"ai.operation.name": "Name",
"ai.user.id": "userId",
"ai.cloud.roleInstance": "Machine 1"
},
"data": {
"baseType": "EventData",
"message": "message1",
"baseData": {
"name": "Event from my service",
"message": "message2",
"properties": {
"x": "value x",
"y": "value y",
"z": "value z"
}
}
}
}]
答案 0 :(得分:1)
我们最近一直在尝试实现相同的目标,但也没有在网络上找到好的说明,因此请在此处发布我们的发现。
您将需要您的检测密钥 - 有关详细信息,请参阅 How to get Azure Instrumentation Key。该密钥必须存储在消息负载的“iKey”属性中。
总的来说,似乎可以设置 4 种不同的消息类型(由“baseType”属性定义)。所有这些都需要通过 POST 发送到同一个端点
POST https://dc.services.visualstudio.com/v2/track
[ { MESSAGE1 }, { MESSAGE2 }, ... ]
每条消息的格式取决于其类型。不需要身份验证头 - 消息的“iKey”字段用作身份验证密钥。
如果成功则发送以下响应
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"itemsReceived" : 1,
"itemsAccepted" : 1,
"errors":[]
}
其中“itemsAccepted”属性将指示 Application Insights 接受的记录数。
数组中的每条消息都必须遵循以下 4 种可能的 JSON 格式之一。
此事件在 Application Insights 中显示为“自定义事件”。必须使用以下有效负载:
{
"name": "Microsoft.ApplicationInsights.Event",
"time": "2015-05-21T16:43:14.4670675-06:00",
"iKey": "[MyInstrumentationKey]",
"tags": {
},
"data": {
"baseType": "EventData",
"baseData": {
"ver": 2,
"name": "SampleEvent",
"properties": {
"x": "value x",
"y": "value y",
"z": "value z"
}
}
}
}
此事件在 Application Insights 中显示为“TRACE”。必须使用以下有效负载:
{
"name": "Microsoft.ApplicationInsights.Event",
"time": "2021-02-25T21:35:45.0000000Z",
"iKey": "[MyInstrumentationKey]",
"tags":{
},
"data": {
"baseType": "MessageData",
"baseData": {
"ver": 2,
"message": "Simple Trace Log Message",
"severityLevel": 2,
"properties": {
"x": "value x",
"y": "value y",
"z": "value z"
}
}
}
}
severityLevel 含义
level 0 = "Verbose"
level 1 = "Information"
level 2 = "Warning"
level 3 = "Error"
level 4 = "Critical"
必须使用以下有效负载:
{
"name": "Microsoft.ApplicationInsights.Event",
"time": "2021-02-25T21:35:45.0000000Z",
"iKey": "[MyInstrumentationKey]",
"tags": {
},
"data": {
"baseType": "MetricData",
"baseData": {
"ver": 2,
"metrics": [
{
"name": "BasicMetric",
"kind": "Measurement",
"value": 42
}
],
"properties": {
"x": "value x",
"y": "value y",
"z": "value z"
}
}
}
}
必须使用以下有效负载:
{
"name": "Microsoft.ApplicationInsights.Event",
"time": "2021-02-25T21:35:45.0000000Z",
"iKey": "[MyInstrumentationKey]",
"tags": {
},
"data": {
"baseType": "ExceptionData",
"baseData": {
"ver": 2,
"handledAt": "UserCode",
"properties": {
"x": "value x",
"y": "value y",
"z": "value z"
},
"exceptions": [
{
"id": 26756241,
"typeName": "System.Exception",
"message": "Something bad has happened!",
"hasFullStack": true,
"parsedStack": [
{
"level": 0,
"method": "Console.Program.Main",
"assembly": "Console, Version=1.0",
"fileName": "/ApplicationInsights/Test.cs",
"line": 42
}
]
}
]
}
}
}
答案 1 :(得分:0)