LogicApps to Send Error Notification Based on configuration table.
Logic Apps structure to get the data from Application Insights and get the notification configurations from the database table and send emails based on the action.
The code for the structure below. The subscriptionId has to updated.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Exception_Result_Query": {
"inputs": {
"body": "\n\nunion (traces \n| where message startswith \"Error: \" and timestamp > ago(30m) \n| join kind= leftouter (\n requests \n | where timestamp > ago(30m) and success==\"False\"\n) on operation_Id \n| project success , name ,resultCode,duration,url, operation_Name, appName=cloud_RoleName, outerType=customDimensions.prop__exceptionType, details=customDimensions.prop__details, innermostMessage=customDimensions.prop__message, stackTrace=customDimensions.prop__stackTrace,severityLevel,customDimensions.LogLevel , timestamp),\n(exceptions\n| where timestamp > ago(30m)\n| join kind= leftouter (\n requests \n | where timestamp > ago(30m) and success==\"False\"\n) on operation_Id \n| project success , name ,resultCode,duration,url, operation_Name, appName=cloud_RoleName, outerType, details, innermostMessage, stackTrace=details,severityLevel,customDimensions.LogLevel,timestamp )\n| order by operation_Name asc ",
"host": {
"connection": {
"name": "@parameters('$connections')['applicationinsights_1']['connectionId']"
}
},
"method": "post",
"path": "/api/QueryDraft",
"queries": {
"timerange": "Set in query",
"version": "2"
}
},
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"For_each": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "queryResults",
"value": "@items('For_each')"
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
},
"foreach": "@body('Exception_Result_Query')?['value']",
"runAfter": {
"Exception_Result_Query": [
"Succeeded"
]
},
"type": "Foreach"
},
"For_each_2": {
"actions": {
"Get_Notification_Details": {
"inputs": {
"body": {
"query": "SELECT Parameters EmailId,Action\n FROM [dbo].[Notification]\n where Deleted is null\n and Active =1\n and IntegrationName= '@{items('For_each_2')?['operation_Name']}'"
},
"host": {
"connection": {
"name": "@parameters('$connections')['sql_1']['connectionId']"
}
},
"method": "post",
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('default'))},@{encodeURIComponent(encodeURIComponent('default'))}/query/sql"
},
"runAfter": {},
"type": "ApiConnection"
},
"Notification_Result": {
"actions": {
"Switch": {
"cases": {
"Case": {
"actions": {
"Send_email": {
"inputs": {
"body": {
"Attachments": [
{
"ContentBytes": "@{base64(items('For_each_2')?['stackTrace'])}",
"Name": "@{items('For_each_2')?['operation_Name']}_@{items('For_each_2')?['appName']}_stackTrace.TXT"
},
{
"ContentBytes": "@{base64(items('For_each_2')?['details'])}",
"Name": "@{items('For_each_2')?['operation_Name']}_@{items('For_each_2')?['appName']}_details.TXT"
}
],
"Body": "<p> </p>\n<p>The Azure process <strong> @{items('For_each_2')?['appName']}</strong> has thrown <strong>exception and failed.</strong></p>\n<p> </p>\n<table style=\"height: 412px; width: 536px;\">\n<tbody>\n<tr style=\"height: 3px;\">\n<td style=\"width: 526px; height: 3px;\" colspan=\"2\">\n<h2><strong>Summary</strong></h2>\n</td>\n</tr>\n<tr style=\"height: 37px;\">\n<td style=\"width: 193px; height: 37px;\">\n<h4><strong>Name</strong></h4>\n</td>\n<td style=\"width: 327px; height: 37px;\"> @{items('For_each_2')?['operation_Name']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Severity Level</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['severityLevel']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Sucess</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['success']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Duration</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['duration']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Exception Type</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['outerType_dynamic']}@{items('For_each_2')?['outerType_string']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>TimeStamp</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['timestamp']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Formatted Message</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['innermostMessage_dynamic']}@{items('For_each_2')?['innermostMessage_string']}</td>\n</tr>\n</tbody>\n</table>\n<p> </p>\n<p>The attachments provide the full stacktrace and formatted exception details associated with the issue.</p>",
"IsHtml": true,
"Subject": "@{items('For_each_2')?['customDimensions_LogLevel']} - @{items('For_each_2')?['appName']} - @{items('For_each_2')?['operation_Name']} - @{items('For_each_2')?['timestamp']}",
"To": "@{body('Get_Notification_Details')['ResultSets']?['Table1'][0]['EmailId']}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['gmail']['connectionId']"
}
},
"method": "post",
"path": "/Mail"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"case": "Email"
}
},
"default": {
"actions": {}
},
"expression": "@body('Get_Notification_Details')['ResultSets']?['Table1'][0]['Action']",
"runAfter": {},
"type": "Switch"
}
},
"foreach": "@if(equals(body('Get_Notification_Details')['ResultSets']?['Table1'], json('null')), json('[ ]'),body('Get_Notification_Details')['ResultSets']?['Table1'])",
"runAfter": {
"Get_Notification_Details": [
"Succeeded"
]
},
"type": "Foreach"
}
},
"foreach": "@body('Exception_Result_Query')?['value']",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "queryResults",
"type": "array"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Minute",
"interval": 30
},
"type": "Recurrence"
}
}
},
"parameters": {
"$connections": {
"value": {
"applicationinsights_1": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/applicationinsights-1",
"connectionName": "applicationinsights-1",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/applicationinsights"
},
"gmail": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/gmail",
"connectionName": "gmail",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/gmail"
},
"sql_1": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/sql-5",
"connectionName": "sql-5",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/sql"
}
}
}
}
}
The code for the structure below. The subscriptionId has to updated.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Exception_Result_Query": {
"inputs": {
"body": "\n\nunion (traces \n| where message startswith \"Error: \" and timestamp > ago(30m) \n| join kind= leftouter (\n requests \n | where timestamp > ago(30m) and success==\"False\"\n) on operation_Id \n| project success , name ,resultCode,duration,url, operation_Name, appName=cloud_RoleName, outerType=customDimensions.prop__exceptionType, details=customDimensions.prop__details, innermostMessage=customDimensions.prop__message, stackTrace=customDimensions.prop__stackTrace,severityLevel,customDimensions.LogLevel , timestamp),\n(exceptions\n| where timestamp > ago(30m)\n| join kind= leftouter (\n requests \n | where timestamp > ago(30m) and success==\"False\"\n) on operation_Id \n| project success , name ,resultCode,duration,url, operation_Name, appName=cloud_RoleName, outerType, details, innermostMessage, stackTrace=details,severityLevel,customDimensions.LogLevel,timestamp )\n| order by operation_Name asc ",
"host": {
"connection": {
"name": "@parameters('$connections')['applicationinsights_1']['connectionId']"
}
},
"method": "post",
"path": "/api/QueryDraft",
"queries": {
"timerange": "Set in query",
"version": "2"
}
},
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"For_each": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "queryResults",
"value": "@items('For_each')"
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
},
"foreach": "@body('Exception_Result_Query')?['value']",
"runAfter": {
"Exception_Result_Query": [
"Succeeded"
]
},
"type": "Foreach"
},
"For_each_2": {
"actions": {
"Get_Notification_Details": {
"inputs": {
"body": {
"query": "SELECT Parameters EmailId,Action\n FROM [dbo].[Notification]\n where Deleted is null\n and Active =1\n and IntegrationName= '@{items('For_each_2')?['operation_Name']}'"
},
"host": {
"connection": {
"name": "@parameters('$connections')['sql_1']['connectionId']"
}
},
"method": "post",
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('default'))},@{encodeURIComponent(encodeURIComponent('default'))}/query/sql"
},
"runAfter": {},
"type": "ApiConnection"
},
"Notification_Result": {
"actions": {
"Switch": {
"cases": {
"Case": {
"actions": {
"Send_email": {
"inputs": {
"body": {
"Attachments": [
{
"ContentBytes": "@{base64(items('For_each_2')?['stackTrace'])}",
"Name": "@{items('For_each_2')?['operation_Name']}_@{items('For_each_2')?['appName']}_stackTrace.TXT"
},
{
"ContentBytes": "@{base64(items('For_each_2')?['details'])}",
"Name": "@{items('For_each_2')?['operation_Name']}_@{items('For_each_2')?['appName']}_details.TXT"
}
],
"Body": "<p> </p>\n<p>The Azure process <strong> @{items('For_each_2')?['appName']}</strong> has thrown <strong>exception and failed.</strong></p>\n<p> </p>\n<table style=\"height: 412px; width: 536px;\">\n<tbody>\n<tr style=\"height: 3px;\">\n<td style=\"width: 526px; height: 3px;\" colspan=\"2\">\n<h2><strong>Summary</strong></h2>\n</td>\n</tr>\n<tr style=\"height: 37px;\">\n<td style=\"width: 193px; height: 37px;\">\n<h4><strong>Name</strong></h4>\n</td>\n<td style=\"width: 327px; height: 37px;\"> @{items('For_each_2')?['operation_Name']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Severity Level</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['severityLevel']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Sucess</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['success']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Duration</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['duration']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Exception Type</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['outerType_dynamic']}@{items('For_each_2')?['outerType_string']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>TimeStamp</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['timestamp']}</td>\n</tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 193px; height: 38px;\">\n<h4>Formatted Message</h4>\n</td>\n<td style=\"width: 327px; height: 38px;\"> @{items('For_each_2')?['innermostMessage_dynamic']}@{items('For_each_2')?['innermostMessage_string']}</td>\n</tr>\n</tbody>\n</table>\n<p> </p>\n<p>The attachments provide the full stacktrace and formatted exception details associated with the issue.</p>",
"IsHtml": true,
"Subject": "@{items('For_each_2')?['customDimensions_LogLevel']} - @{items('For_each_2')?['appName']} - @{items('For_each_2')?['operation_Name']} - @{items('For_each_2')?['timestamp']}",
"To": "@{body('Get_Notification_Details')['ResultSets']?['Table1'][0]['EmailId']}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['gmail']['connectionId']"
}
},
"method": "post",
"path": "/Mail"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"case": "Email"
}
},
"default": {
"actions": {}
},
"expression": "@body('Get_Notification_Details')['ResultSets']?['Table1'][0]['Action']",
"runAfter": {},
"type": "Switch"
}
},
"foreach": "@if(equals(body('Get_Notification_Details')['ResultSets']?['Table1'], json('null')), json('[ ]'),body('Get_Notification_Details')['ResultSets']?['Table1'])",
"runAfter": {
"Get_Notification_Details": [
"Succeeded"
]
},
"type": "Foreach"
}
},
"foreach": "@body('Exception_Result_Query')?['value']",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "queryResults",
"type": "array"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Minute",
"interval": 30
},
"type": "Recurrence"
}
}
},
"parameters": {
"$connections": {
"value": {
"applicationinsights_1": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/applicationinsights-1",
"connectionName": "applicationinsights-1",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/applicationinsights"
},
"gmail": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/gmail",
"connectionName": "gmail",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/gmail"
},
"sql_1": {
"connectionId": "/subscriptions/SubscriptionID/resourceGroups/Demo-Logic-App/providers/Microsoft.Web/connections/sql-5",
"connectionName": "sql-5",
"id": "/subscriptions/SubscriptionID/providers/Microsoft.Web/locations/uksouth/managedApis/sql"
}
}
}
}
}
Comments
Post a Comment