Warm tip: This article is reproduced from serverfault.com, please click

How to get complete Log in Azure Function?

发布于 2020-11-28 01:14:28

I try to get the complete log in my Azure function, including ALL the custom log I added in my code, currently I only have something like:

2020-11-28T00:56:59.614 [Information] Executing 'test-log' (Reason='This function was programmatically called via the host APIs.', Id=7f82a0c4-5ae9-416c-8f19-9c00722ded2f) 2020-11-28T00:56:59.848 [Information] Executed 'test-log' (Succeeded, Id=7f82a0c4-5ae9-416c-8f19-9c00722ded2f, Duration=247ms)

Here is how I configure the azure function:

public class Startup : FunctionsStartup
{
    public IConfiguration Configuration { get; private set; }

    public Startup() { }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services
            .AddMvcCore()
            .AddNewtonsoftJson(jsonOptions =>
            {
                jsonOptions.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

        builder.AddConfiguration((configBuilder) => configBuilder
            .AddEnvironmentVariables()
            .Build()
        );

        builder.Services.AddLogging();

        
        Configuration = builder.GetCurrentConfiguration();

        // explicitly call ConfigureServices to setup DI 
        ConfigureServices(builder.Services);
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped(typeof(ILoggerService<>), typeof(LoggerService<>));
        services.AddTelemetryClient(Configuration);
    }

My LoggerService:

public class LoggerService<T> : ILoggerService<T> where T : class
{
    private readonly TelemetryClient _telemetryClient;
    private readonly ILogger<T> _logger;
    // Flag: Has Dispose already been called?
    bool disposed = false;

    public LoggerService(
        ILogger<T> logger
    )
    {
        _telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
        _logger = logger;
    }


    public void LogTrace(string message)
    {
        _logger.LogTrace(message);
    }

    public void LogTrace(string message, IDictionary<string, string> properties = null)
    {
        TrackMessage(message, LogLevel.Trace, properties);
        LogTrace(message);
    }

    private void TrackMessage(string message, LogLevel logLevel, IDictionary<string, string> properties = null)
    {
        _telemetryClient.TrackTrace($"{logLevel}:{message}", properties);
        Flush();
    }
    [...]

And My function:

public class MyFunctions : BaseFunctions { internal readonly ILoggerService _logger;

    public POSFunctions(
        ILoggerService<POSFunctions> logger
    ) 
    {
        _logger= logger;
    }

    [FunctionName("test-log")]
    public async Task<IActionResult> TestLogAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest request
    )
    {
        
        _logger.LogInformation("This is log");
    }

And in the Configuration of my Azure function I have: enter image description here

Why I can not see "This is log" in the Azure Function Log?

Questioner
Cedric Arnould
Viewed
0
Cedric Arnould 2020-11-28 10:41:57

The solution was to add in the host.json file:

  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information"
    },
    "applicationInsights": {
      "enableLiveMetrics": true,
      "enableDependencyTracking": true,
    }
  },