Capture search query string on an ASP.Net MVC app using Application Insights on Azure

Being completely new to Azure Application Insights, I am wondering how can I capture a simple free text search query string on an ASP.Net MVC application using Azure Application Insights.

The app creates a GET request while the user enters a search query on the application. The query looks like this https://example.com/GalleryPartial?search=Application&id=&sort=. What I need to capture is the text entered for search, which is “Application” in this particular instance.

I have tried using the Logs from Application Insights life below to capture the links:

requests
| where url contains "search"
| summarize count() by bin(timestamp, 1m), URL
| render timechart 

Problem: How can I extract the search query only and see a list of all the search queries (text entered by users) in the Application Insights Dashboard? I don’t have any more code than just capturing some telemetry for Application Azure.

The View is as follows:

@ModelType IEnumerable(Of Models.UXFilter)
@code
Dim m_Search As String
If ViewContext.Controller.ViewBag.SearchObject IsNot Nothing Then
    m_Search = ViewContext.Controller.ViewBag.SearchObject
Else
    m_Search = ""
End If
End Code

<div>
    <div class="wrapperSearch">
         <input class="inputSearch" placeholder="Search..." type="text" id="searchValue" value="@m_Search" autocomplete="off">
    </div>
</div>

The Jquery for the search is as follows:

    $(document).ready(function () {
    var amountFilter = 0;

    $('#searchValue').on('keyup', function () {

        var filterText = $('#searchValue').val();
        var DepId = JSON.parse('@Html.Raw(Json.Encode(ViewData("ID")))');
        var filterTextLength = filterText.length;

        $("#log").ajaxError(function (event, jqxhr, settings, exception) {
            alert(exception);
        });

        var runIt = 'false';

        if (filterText.length > 2) {
            runIt = 'true';
        } else if (filterText.length == 0) {
            runIt = 'true';
        }

        if (runIt == 'true') {
            var existingUrl = window.location.href;
            var n = existingUrl.indexOf("/Gallery");
            if (n < 0) {
                var url = '@Url.Action("Gallery", "UX", New With {.id = "filter"})?search=' + filterText;
                window.location.href = url;
            }

            $.get('@Url.Action("GalleryPartial")',
                    { searchen: filterText, id: DepId, sort: "" }, function (data) {

                        $("#target").html(data);
                });

            SetupFilters();
        }
    }); 
}

Answer

in Kusto there are several helper methods you can use. In your case parse_url can be helpful as it will contain an array of query parameters that can be easily accessed:

requests
| extend searchText = tostring(parse_url(url).["Query Parameters"]["search"])
| summarize count() by bin(timestamp, 1m), searchText 
| render timechart 

Leave a Reply

Your email address will not be published. Required fields are marked *