An Azure Function App (C# 8) logs to Azure Application Insights. While I can see all its log messages in the live window during "Test/Run", I find only part of these log messages when searching for them in "Application Insights - Logs - traces". And the messages which I find are at Warning level only (no exceptions happened, so not expected here). While entries of other items are even at Trace level (e.g. "Sending invocation id...")
Where do I have to configure what settings?
I have created an HTTP trigger function with a runtime stack of .NET 8.0.
function code:
using System;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace Company.Function
{
public class HttpTrigger1
{
[Function("HttpTrigger1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
try
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
throw new InvalidOperationException("This is a sample exception.");
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred.");
var errorResponse = req.CreateResponse(HttpStatusCode.InternalServerError);
errorResponse.WriteString("An error occurred. Please try again later.");
return errorResponse;
}
}
}
}
host.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information",
"MyNamespace.MyCustomExceptionCategory": "Error",
"MyNamespace.MyInformationCategory": "Information",
"MyNamespace.MyWarningCategory": "Warning"
}
}
}
Program.cs:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.ApplicationInsights;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddApplicationInsights("your appinsights instrumentation key");
loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
});
})
.Build();
host.Run();
Application successfully deployed and able to see in function app. check below:
When I click on Test/Run
ran successfully and get the Exception logs and Information logs in Application insights.
Output: