Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
421 stars 185 forks source link

Question: Local development logging to console #1138

Closed Sn00z3r closed 2 months ago

Sn00z3r commented 1 year ago

All,

I'm aware this might not be the best location to ask this as this is not an issue, however I'm hoping somebody can enlighten me. I'm creating an isolated .net6 azure function and I'm having some issues with logging when running locally.

The issue is that I don't see any logging from my contructors or initalization services (which are added as hostedservice), this is probably because azure core function cli is not connected to stdout during this phase or something.

Also, can't seem to set loglevel to namespaces, so when I choose loglvel for Function: Informational, I'm also getting all informational output from the sdk.

So my question, can somebody enlighten me how this works :)

Best regards

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        services.AddHostedService<InitializationService>();
        services.AddSingleton<ChronicleApi>();
    })
    .Build();

await host.RunAsync();
public sealed class InitializationService : IHostedService
{
    private readonly ILogger<InitializationService> _logger;
    private readonly ChronicleApi _chronicleApi;

    public InitializationService(ILogger<InitializationService> logger, ChronicleApi chronicleApi)
    {
        _logger = logger;
        _chronicleApi = chronicleApi;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await _chronicleApi.Initialize(cancellationToken);
    }

    etc.
    public sealed class ChronicleApi
{
    private readonly ILogger<ChronicleApi> _logger;
    private readonly IConfiguration _configuration;

    public ChronicleApi(ILogger<ChronicleApi> logger, IConfiguration configuration)
    {
        _logger = logger;
        _logger.LogInformation($"{this.GetType().Name} initialized!");
    }

    public async Task Initialize(CancellationToken cancellationToken)
    {
        _logger.LogInformation($"API initialized!");
    }
Sn00z3r commented 1 year ago

Feel free to close if this is the wrong repo to ask this, but if you could point me to where I need to be that would be great :)

jviau commented 1 year ago

Hi @Sn00z3r - we do not add the console logger by default. To add the console logger, please add this to your host building call:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        services.AddHostedService<InitializationService>();
        services.AddSingleton<ChronicleApi>();
    })
    .ConfigureLogging(b => b.AddConsole()) // add this
    .Build();

await host.RunAsync();

If you want it for local development only, you will need to play around with the code above to only add console in that environment. Also, I am going off of memory here so the exact code may not be correct.

Sn00z3r commented 1 year ago

Hi there,

That doesn't do anything. I can't see any log messages from anything earlier than the actual function run.

Also have no way to granular change the loglevel to specific namespaces so I don't see the information logs from azure functions sdk myself (e.g. omit all the function ran successful messages :))

Best regards!

brettsam commented 2 months ago

This issue is quite old and it's possible it's already been fixed. I just tried a default isolated app and did this:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddHostedService<MyHostedService>();
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .ConfigureLogging(b => b.AddConsole()) // add this
    .Build();

host.Run();

public class MyHostedService : IHostedService
{
    public MyHostedService(ILogger<MyHostedService> logger)
    {
        logger.LogInformation("FROM HOSTED SERVICE!");
        Console.WriteLine("FROM CONSOLE!");
    }

    public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

When I hit F5, I see both of these logs show up in the console:

image

If you're still running into issues around this, can you please open a new issue and we'll follow up that way with a fresh repro?