azure-functions-host icon indicating copy to clipboard operation
azure-functions-host copied to clipboard

Unable to log errors during startup or dependency injection (DI) of function dependencies

Open nlykkei opened this issue 1 year ago • 7 comments

What problem would the feature you're requesting solve? Please describe.

The Azure Function host doesn't catch and display errors during startup or dependency injection of function dependencies. Instead a generic error is shown (see below).

If I try to catch and log errors myself, then I can't assume Application Insights to be configured, so I would need to log to the console. But if I do that, then nothing is logged, not even if I inspect the filesystem logs, where nothing is ever written.

Could you please describe how to catch and log startup and dependency injection errors, so I can inspect them, including stack traces?

The Azure Function host won't let me log information in Program.cs, when creating and running a HostBuilder in Azure.

Describe the solution you'd like

I would like to be provided with a stack trace of the exception and/or being able to catch and log such errors myself to the console.

Describe alternatives you've considered

I don't think I have any alternatives

Additional context

Azure Functions host: 4.x Language: .NET 8 (dotnet-isolated) Service plan: Linux / Consumption plan

Azure Function App Test/Run Error:

image

Program.cs:

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger("Startup");

logger.LogInformation("The application is starting...");

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration(builder =>
    {
        var environment = Environment.GetEnvironmentVariable("ENVIRONMENT") ?? "development";

        builder.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
    })
    .ConfigureServices((context, services) =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
        
        services.AddDbContext<AssetOwnershipContext>((sp, options) =>
        {
            var logger = sp.GetRequiredService<ILogger>();
            var connectionString = context.Configuration.GetConnectionString("SqlServer") ?? throw new ConfigurationErrorsException("Connection string not found");
            logger.LogInformation("Using connection string: {connectionString}", connectionString);
            options.UseSqlServer(connectionString);
        });

        services.AddScoped<IApplicationService, ApplicationService>();
        services.AddScoped<IEmailDispatcher, EmailDispatcher>();
        services.AddTransient<IAssetOwnershipRepository, AssetOwnershipRepository>();
        services.AddSingleton<GraphServiceClient>(sp =>
            new GraphServiceClient(
                new DefaultAzureCredential(new DefaultAzureCredentialOptions()
                {
                    ExcludeSharedTokenCacheCredential = true
                })
            )
        );
    })
    .Build();

host.Run();

nlykkei avatar Sep 16 '24 17:09 nlykkei

Thanks for informing will check and update you.

bhagyshricompany avatar Sep 17 '24 06:09 bhagyshricompany

@bhagyshricompany Thank you. Also filesystem logs is not working for this setup:

image

Normally, I would get a connection successful status

nlykkei avatar Sep 17 '24 07:09 nlykkei

I checked one sample function creation to log error its working fine. image

bhagyshricompany avatar Sep 17 '24 10:09 bhagyshricompany

Thank you. Yes, it's working fine locally, but not in Azure. Have you tried to deploy your function app?

nlykkei avatar Sep 17 '24 10:09 nlykkei

can you try it again.

bhagyshricompany avatar Oct 01 '24 09:10 bhagyshricompany

Yes, viewing filesystem logging is not working

On Tue, 1 Oct 2024 at 11.03, bhagyshricompany @.***> wrote:

can you try it again.

— Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-host/issues/10474#issuecomment-2385220967, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADLPSPCEAGL6DHBL6UI5UH3ZZJQOPAVCNFSM6AAAAABOJZKWXKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBVGIZDAOJWG4 . You are receiving this because you authored the thread.Message ID: @.***>

nlykkei avatar Oct 01 '24 10:10 nlykkei

@kshyju please comment.

bhagyshricompany avatar Oct 22 '24 14:10 bhagyshricompany

This has been addressed in the latest version of the host (4.1037). Deployment is actively rolling out.

Closing as resolved, but let us know if you have any questions or problems.

fabiocav avatar Feb 05 '25 21:02 fabiocav