我构建我的服务主要是为了我的API,因此它们都会在它们的构造器中注入一个ILogger<>。(Azure WebApps)
现在,我也在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();发布于 2018-07-18 09:27:51
我遵循您提供的教程并下载演示。
您可以参考以下步骤登录到blob存储。Nuget包裹:

TimeJob:
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日志文件如下:


https://stackoverflow.com/questions/51357023
复制相似问题