I’m pushing my first .Net 8 Function App out to Azure and I’m getting an error that the ILogger is NULL. I’m following the same code as I’ve done before using .Net 6. It also works as expected when I run the app locally on my development machine.
Is there a setting that I might be missing to get this to work?
public class StorageUtilityHandler
{
private readonly ILogger<StorageUtilityHandler> _logger;
private readonly IBusinessLogic _businessLogic;
public StorageUtilityHandler(ILogger<StorageUtilityHandler> logger, IBusinessLogic businessLogic)
{
_logger = logger;
_businessLogic = businessLogic;
}
[Function("LogProcessorQueue")]
public static Task ProcessLogQueueAsync(
[ServiceBusTrigger("%LogServiceBusQueue%", Connection = "ServiceBusConnection")] string logFileMessage, ILogger log)
{
if (logFileMessage != null)
{
try
{
LogEntity? logEntity = JsonSerializer.Deserialize<LogEntity>(logFileMessage);
if (logEntity != null)
{
//If ERROR then send out Email
if(logEntity.Message.Contains("ERROR"))
{
var _email = new EmailService();
_email.SendLogError(logEntity);
}
var storageConnection = Environment.GetEnvironmentVariable("StorageConnection");
var loggingTableName = Environment.GetEnvironmentVariable("LoggingTableName");
TableClient tableClient = new(storageConnection, loggingTableName);
// Create the table if it doesn't already exist to verify we've successfully authenticated.
tableClient.CreateIfNotExists(); //Remove in PROD
tableClient.AddEntity(logEntity);
}
}
catch (Exception ex)
{
log.LogError($"Error processing Log message. {ex.Message} | {ex.StackTrace} | {ex.InnerException}");
}
}
return Task.CompletedTask;
}
}
Program.cs
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddTransient<IBusinessLogic, BusinessLogic>();
})
.Build();
All my Service Bus messages from this FA are going into the Dead Letter queue. Looking at the messages in the Dead Letter queue, they look correct. When I look at the logs from Application Insights, I get the following error about ILogger being NULL.
Operation_Name: LogProcessorQueue
Exception: System.ArgumentNullException: Value cannot be null. (Parameter 'logger')
at System.ThrowHelper.Throw(String paramName)
at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
at Microsoft.Extensions.Logging.LoggerExtensions.LogError(ILogger logger, String message, Object[] args)