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):
The problems I have are that:
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"
}
}
}
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.
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.
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.
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".
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
Reference:
Basic logging with TraceWriter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With