【问题标题】:Azure WebJob, ILogger DI Injected logging to BlobStorageAzure WebJob、ILogger DI 注入日志记录到 Blob 存储
【发布时间】:2018-07-16 07:53:41
【问题描述】:

我主要为我的 API 构建我的服务,因此它们都在其构造函数中注入了 ILogger。 (Azure 网络应用)

现在我也在 Azure 中使用 Web 作业,并按照以下说明使用 DI 构建它:https://matt-roberts.me/azure-webjobs-in-net-core-2-with-di-and-configuration/

我现在的问题是,我希望 service.logger 实例登录到 Azure BlobStorage 以进行 Web 作业。 通过绑定,我得到了时间触发函数的 ILogger 实例。但是如何将服务日志记录到 BobStorage?

提前致谢。

loggerFactory.AddConsole();
loggerFactory.AddAzureWebAppDiagnostics();

public async Task TimeJob(
    [TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timerInfo,
    ILogger log
    )
    ....

编辑

IServiceCollection services = new ServiceCollection()
.AddLogging(config =>
{
    config.AddConfiguration(configuration.GetSection("Logging"));
    config.AddConsole();
    config.AddAzureWebAppDiagnostics();
 })
 .AddSingleton(configuration)
 ;

 // register services and such
 configurationCallback(services);

 return services.BuildServiceProvider();

.

        Program.Configuration = SetupHelper.SetupConfiguration(args);
        Program.Services = SetupHelper.SetupServiceProvider(Program.Configuration, Program.ConfigureServices);
        var loggerFactory = Program.Services.GetService<ILoggerFactory>();

        var hostConfig = new JobHostConfiguration(Program.Configuration)
        {
            JobActivator = new CustomJobActivator(Program.Services),
            LoggerFactory = loggerFactory
        };
        hostConfig.Queues.MaxPollingInterval = TimeSpan.FromSeconds(10);
        hostConfig.Queues.BatchSize = 1;
        hostConfig.UseTimers();

        //hostConfig.Tracing.ConsoleLevel = TraceLevel.Off;

        if (hostConfig.IsDevelopment)
        {
            hostConfig.UseDevelopmentSettings();
        }

        var host = new JobHost(hostConfig);
        host.RunAndBlock();

示例服务

public abstract class Service : IService
{
    protected readonly ILogger Logger;
    protected readonly IUserInfo UserInfo;
protected bool Disposed;

protected Service(
    ILogger logger,
    IUserInfo userInfo)
{
    this.Logger = logger;
    this.UserInfo = userInfo;
}

public abstract void Dispose();

【问题讨论】:

    标签: azure logging dependency-injection azure-webjobs


    【解决方案1】:

    我按照您提供的教程下载demo

    您可以参考以下步骤登录 Blob 存储。 Nuget 包:

    时间工作:

    public void DoSomethingUseful([TimerTrigger("0 */1 * * * *", RunOnStartup = true)] TimerInfo timerInfo, ILogger log)
            {
                // Act on the DI-ed class:
                string thing = _usefulRepository.GetFoo();
                Console.WriteLine($"{DateTime.Now} - {thing}");
                ILoggerFactory loggerFactory = new LoggerFactory()
                .AddConsole()
                .AddDebug()
                .AddAzureWebAppDiagnostics();
    
                log = loggerFactory.CreateLogger<SampleScheduledWebJob>();
    
                log.LogInformation("hello world");
    
            }
    

    部署到azure后,我可以看到KUDU控制台下的日志和blob日志文件如下:

    【讨论】:

    • 您好,谢谢,但问题不在于 Web 作业中的 ILogger 绑定,而在于注入的服务中。他们应该使用 ILoggerFactory 但他们没有。我添加了更多代码。
    • @Mario,也许你可以参考这个blog 关于.NET Core 控制台应用程序中的依赖注入、日志记录和配置。然后你可以upload core console application as webjob to azure
    • 我按照我提供给你的上述链接,我可以在 KUDU 中看到日志信息。但如果您还想将日志信息添加到 Blob 存储中,可以在诊断日志中启用 Application Logging(Blob)
    猜你喜欢
    • 2018-02-17
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多