Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Application Insights log severity levels

I am trying to find out what is wrong with my code or approach. I am using Azure Functions (v3) and use ILogger interface to log events from my functions. Here is the sample code I am using:

        [FunctionName("GetVersion")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("[TEST] this is informational");
        log.LogDebug("[TEST] this is debug");
        log.LogTrace("[TEST] this is trace");
        return new OkResult();
    }

My Azure Functions have Application Insights are enabled, so logs are written to the traces table, and I can query for them like this:

traces 
| order by timestamp
| where operation_Name == 'GetVersion'
| project timestamp,message,severityLevel
| limit 200

But the result looks like this (columns are timestamp, message and the last one is log severity level):

enter image description here

The problems I have are that:

  1. Severity both for Trace and Debug logs is 0, while they should have distinct levels.
  2. Actual severity levels differ from MS documentation. Trace should be 0 and Debug 1 https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-3.1

Expected behavior is that severity in App Insights matches severity levels from documentation. What am I missing here?

P.S. My host.json looks like this

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "logLevel": {
      "default": "Information",
      "Host.Results": "Information",
      "Function": "Debug",
      "Function.GetVersion": "Trace",
      "Host.Aggregator": "Trace"
    }
  }
}
like image 578
Simon Avatar asked Jun 23 '20 18:06

Simon


People also ask

How do I read application Insights logs?

View logs in Application InsightsGo to Application Insights resource in your resource group. Go to Logs under Monitoring section. Click on traces eye button to get log traces. Select Time Range and click Run.

How do I log exceptions in application Insights?

Diagnose exceptions using Visual StudioOpen the app solution in Visual Studio. Run the app, either on your server or on your development machine by using F5 . Re-create the exception. Open the Application Insights Search telemetry window in Visual Studio.

Does application Insights use log analytics?

Application Insights, a feature of Azure Monitor, is an extensible Application Performance Management (APM) service which can be used to monitor your live applications as well. It sends data to Log Analytics Workspace which is a kind of data Lake that Azure Monitor uses.


2 Answers

The severity levels of application insights are here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.datacontracts.severitylevel?view=azure-dotnet

"LogDebug" and "LogTrace" from ILogger is "Verbose" for application insights. Therefore, as far as I know, Application insights can not distinguish or filter-out what you log as "LogTrace" and filter-in what you log as "LogDebug".

like image 172
JoaoRibeiro Avatar answered Oct 18 '22 19:10

JoaoRibeiro


It seems that the severityLevel doesn't show correctly when using ILogger. I tried with TraceWriter, it works as expected. Here is my testing sample. You can try it instead.

public static async Task<IActionResult> Run(HttpRequest req, TraceWriter  log)
{
    log.Info("101 Azure Function Demo - Basic logging with TraceWriter");
    log.Warning("Here is a warning log message");
    log.Error("Here is an error log message");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello11")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

The result

enter image description here

enter image description here

Reference:

Basic logging with TraceWriter

like image 2
Tony Ju Avatar answered Oct 18 '22 20:10

Tony Ju