Warm tip: 本文翻译自 stackoverflow.com,查看原文请点击:, please click

.net core-如何获得完整的Azure Azure登录功能?

(.net core - How to get complete Log in Azure Function?)

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

我尝试在我的Azure函数中获取完整的日志,包括我在代码中添加的所有自定义日志,目前我只有这样的东西:

2020-11-28T00:56:59.614 [信息]执行“测试日志”(原因=“通过主机API以编程方式调用此函数。”,Id = 7f82a0c4-5ae9-416c-8f19-9c00722ded2f)2020-11-21 28T00:56:59.848 [Information]执行了“测试日志”(成功,Id = 7f82a0c4-5ae9-416c-8f19-9c00722ded2f,持续时间= 247ms)

这是我配置azure函数的方法:

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);
    }

我的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();
    }
    [...]

和我的功能:

公共类MyFunctions:BaseFunctions {内部只读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");
    }

在我的Azure功能的配置中,我有: 在此处输入图片说明

为什么在Azure功能日志中看不到“这是日志”?

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

解决方案是在host.json文件中添加:

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