Samy Boulos
Samy Boulos

Reputation: 113

POST JSON to log event into Application Insights

I need to write a PowerShell Script shell to log an entry into App Insights. I found this post which is useful but I am having a hard time writing to different fields other than the ones being used there. For example I am trying to populate the message field. I don't know the field name in JSON and I don't know where it should go. As you can see in this example I tried placing it everywhere and still did not work. I tried searching for their REST API documentation and still can't find the specs for the JSON. Can anyone help?

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

Upvotes: 3

Views: 3329

Answers (2)

Max Kuzkin
Max Kuzkin

Reputation: 103

We've been recently trying to achieve the same and didn't find good instructions on the web too, so posting our findings here.

1. API Endpoint

1.1 Authentication

You will need your instrumentation key - see How to get Azure Instrumentation Key for details. That key must be stored in the "iKey" property of the message payload.

1.2 Request

Overall, there seem to be 4 different message types (defined by the "baseType" property) that can be set. All of them need to are sent via POST to the same endpoint

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

[ { MESSAGE1 }, { MESSAGE2 }, ... ]

Where the format of each message depends on its type. No authentication headers are needed - the "iKey" field of the message serves as the authentication key.

1.3 Response

The following response is sent in case of success

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

{ 
    "itemsReceived" : 1,
    "itemsAccepted" : 1, 
    "errors":[]
}

Where the "itemsAccepted" property will indicate the number of records accepted by Application Insights.

2. Message Formats

Each individual message in the array must follow one of the following 4 possible JSON formats.

2.1 The "EventData" ("CUSTOM EVENT") message format

This event is shown as the "Custom Event" in the Application Insights. The following payload must be used:

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

2.2 The "MessageData" ("TRACE") message format

This event is shown as the "TRACE" in the Application Insights. The following payload must be used:

{
   "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 meaning

level 0 = "Verbose"
level 1 = "Information"
level 2 = "Warning"
level 3 = "Error"
level 4 = "Critical"

2.3 The "MetricData" message format

The following payload must be used:

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

2.4 The "ExceptionData" message format

The following payload must be used:

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

Upvotes: 10

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

You can put the message in the properties:{}.

After execute the powershell script, go to azure portal, you should see the message field there: enter image description here

But if you wanna add a field in the Custom Event Properties section, there seams no way to do it.

Upvotes: 1

Related Questions