4
votes

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://docs.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"
    }
  }
}
2

2 Answers

1
votes

The severity levels of application insights are here: https://docs.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".

2
votes

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