azure - Azure WebJob,ILogger DI 注入日志到 BlobStorage
问题描述
我主要为我的 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();
解决方案
我按照您提供的教程下载演示。
时间工作:
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");
}
推荐阅读
- image-processing - 在视频 c# 中跟踪和分配唯一 ID 到对象
- r - R Shiny:googleway 包 - 在更新期间关闭自动缩放和平移
- image - 新导航更新后项目徽标重置。怎么把它们放回去?
- python - 使用隐式方法求解 PDE 系统
- artifactory - 基于校验和的存储 Jfrog Artifactory
- cookies - 跨多个相似域的 Cookie 冲突
- javascript - 如何在段落标签中选择单词
- marklogic - 解释推理规则
- php - 404 显示 index.php 在 codeigniter 中删除时间
- sql - 说明过程的存储过程问题:[dbo].[GetInfo] 包含对对象的未解析引用