Azure 发布JSON以将事件记录到Application Insights中

Azure 发布JSON以将事件记录到Application Insights中,azure,azure-application-insights,Azure,Azure Application Insights,我需要编写一个PowerShell脚本shell来记录进入App Insights的条目。我发现这是有用的,但我有一个困难的时间写不同的领域以外的那些正在使用那里。例如,我试图填充消息字段。我不知道JSON中的字段名,也不知道它应该放在哪里。正如你在这个例子中看到的,我试着把它放在任何地方,但仍然不起作用。我试图搜索他们的RESTAPI文档,但仍然找不到JSON的规范。 有人能帮忙吗 [{ "name": "Microsoft.ApplicationIns

我需要编写一个PowerShell脚本shell来记录进入App Insights的条目。我发现这是有用的,但我有一个困难的时间写不同的领域以外的那些正在使用那里。例如,我试图填充消息字段。我不知道JSON中的字段名,也不知道它应该放在哪里。正如你在这个例子中看到的,我试着把它放在任何地方,但仍然不起作用。我试图搜索他们的RESTAPI文档,但仍然找不到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"
                                                 }
                              }
             }
}]

您可以将
消息
放在
属性:{}

执行powershell脚本后,转到azure门户,您应该会在那里看到消息字段:


但是,如果您想在“自定义事件属性”部分添加一个字段,则没有办法。

我们最近一直在尝试实现这一点,但也没有在web上找到好的说明,因此将我们的发现发布在此处

1.API端点 1.1认证 您将需要您的检测密钥-有关详细信息,请参阅。该密钥必须存储在消息负载的“iKey”属性中

1.2请求 总的来说,似乎可以设置4种不同的消息类型(由“baseType”属性定义)。它们都需要通过POST发送到同一个端点

POST https://dc.services.visualstudio.com/v2/track

[ { MESSAGE1 }, { MESSAGE2 }, ... ]
其中,每条消息的格式取决于其类型。不需要身份验证头-消息的“iKey”字段用作身份验证密钥

1.3答复 如果成功,将发送以下响应

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{ 
    "itemsReceived" : 1,
    "itemsAccepted" : 1, 
    "errors":[]
}
其中,“itemsAccepted”属性将指示Application Insights接受的记录数

2.消息格式 数组中的每条消息必须遵循以下4种可能的JSON格式之一

2.1“事件数据”(“自定义事件”)消息格式 此事件在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"
         }
      }
   }
}
{
   "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"
         }
      }
   }
}
{
   "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
                  }
               ]
            }
         ]
      }
   }
}
2.2“消息数据”(“跟踪”)消息格式 此事件在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"
         }
      }
   }
}
{
   "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"
         }
      }
   }
}
{
   "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
                  }
               ]
            }
         ]
      }
   }
}
严格意义

level 0 = "Verbose"
level 1 = "Information"
level 2 = "Warning"
level 3 = "Error"
level 4 = "Critical"
2.3“MetricData”信息格式 必须使用以下有效载荷:

{
   "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"
         }
      }
   }
}
{
   "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"
         }
      }
   }
}
{
   "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
                  }
               ]
            }
         ]
      }
   }
}
2.4“例外数据”信息格式 必须使用以下有效载荷:

{
   "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"
         }
      }
   }
}
{
   "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"
         }
      }
   }
}
{
   "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
                  }
               ]
            }
         ]
      }
   }
}

尝试使用SDK本身通过常规.NET应用发送相同的数据。然后,您可以使用fiddler观察json中发送的确切内容。然后就可以很容易地重构json了。做得很好,@Max。