Making Azure Log Analytics queries pretty or not-so-pretty

One of the challenges in Log Analytics is how to effectively work with more powerful and longer Log Analytics queries which are available with the new query language. To edit longer queries we can open them with the Azure Log Analytics console (shown below), however this console takes and represents the query transferred from the OMS console in a single line which isn’t very readable.

Luckily, Microsoft thought of this and the console has a pre-built ability to take single line queries and make them more readable – called “Prettify Query”. A sample of query once it had been “Prettify-ed” is below:

If that was the entire story I wouldn’t have written this blog post. However, there is the other half of the story. How do you take a “prettify-ed” query and use it in the OMS console? Here’s the steps that I use to do this in less than a minute with little to no typing.

Step 1: Highlight and copy the existing query from the Azure Log Analytics console.

Step 2: Paste the full query into Microsoft Word.

Step 3: Remove all comments (easy to find by searching for //)

Step 4: Do a Replace All values of ^p with no value as shown below. This removes all of the extra page lines.

Step 5: Do a Replace All for eight spaces ( ) with no value. This removes the extra spaces.

Step 6: Do a Replace All for two spaces ( ) with no value. This removes the remaining extra space. A sample of how this query should not look is shown below.

Step 7: Copy the new query to the OMS console and run the query to test.

If you need to you can re-open it in Advanced Analytics and then click on the “Prettify” option to make it readable again.

NOTE: If your query is too long you may need to cut and paste it over into the Advanced Analytics portal.

The Not-so-pretty query is shown below:

And the “Prettify-ed” query results look much more readable below:

Summary:

  • To clean up a single line query and make it more readable open it in the Azure Log Analytics console, right-click and choose the “Prettify Query” shown in the first graphic.
  • To take a multiple line query and get it ready to use in the OMS console, open it in Word then remove the comments, replace the ^p references, replace the blank spaces and copy and paste the single line version of the query into the OMS console.

I hope that this saves a lot of time to other people who are creating longer queries leveraging the power of the new Log Analytics query language!

Leave a Reply