Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nlykkei opened this issue Sep 16, 2024 · 4 comments

Comments

@nlykkei
Copy link

nlykkei commented Sep 16, 2024

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();
@bhagyshricompany
Copy link

Thanks for informing will check and update you.

@nlykkei
Copy link
Author

nlykkei commented Sep 17, 2024

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

image

Normally, I would get a connection successful status

@bhagyshricompany
Copy link

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

@nlykkei
Copy link
Author

nlykkei commented Sep 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants