Search code examples
azure-functionskqlazure-log-analytics-workspace

How to log custom properties from Azure Function to Log analytics?


I have an Azure function that is logging to Azure Monitor, I am logging custom properties so I can create my own columns. I tried two way I found to do this as below

        //method 1
         _logger.LogInformation("{prop1} {prop2}", "value1", "value2");
        
        
        //method 2
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>()
            {
            {"prop1", "value1" },
            {"prop2", "value2" }
        };
        _telemetryClient.TrackTrace("Message", keyValuePairs);

They both work in adding the message to the Traces table in App Insights, the TrackTrace is better in generating a message that I can get column by name, as it adds a customDimensions json body that I can query customDimensions['prop1'] which is handy.

Now my problem is I can't find the Traces table when I go to see the logs in my Log Analytics Workspace, there is a FunctionAppLogs table, but that doesn't show logs from the TrackTrace Method, only loggings from the _logger, but that is not coming as a json body that I can work with easily, just normal text not even in json format. "prop1 prop2"

How can I get my logged properties in a json format that I can easily work with and query in the log analytics workspace?

my host.json settings file

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "logLevel": {
      "FunctionApps": "Information"
    }
  }
}

Solution

  • I found the logs in AppTraces table in log analytics. Custom properties are logged under column called properties, and it is a json body where I can easily do properties['prop1'] has 'value1' in the kql query