【发布时间】:2019-11-24 16:22:08
【问题描述】:
基本上,我有两种不同的方式获取 ILogger 实例。一个工作得很好,另一个不行。
我有一个这样的 Azure 函数:
class AzureFunctionClass {
private readonly ISomeClass _someclass;
public AzureFunctionClass(ISomeClass someClass){
_someclass = someClass;
}
public Task<IActionResult> AzureFunction(ILogger log){
log.LogInformation("This works, I see this message when run");
_someclass.ExecuteMethod();
}
}
另一个类,不包含 Azure 函数,如下所示:
class SomeClass : ISomeClass {
private readonly ILogger<SomeClass> _log;
public SomeClass(ILogger log){
_log = log;
}
public void ExecuteMethod(){
_log.LogInformation("This doesn't crash so _log isn't null, but it
doesn't write anything");
}
}
Startup.cs:
class Startup : IWebJobsStartup {
public void Configure(IWebJobsBuilder builder){
builder.Services.AddScoped<ISomeClass, SomeClass>();
builder.Services.AddTransient(typeof(ILogger<>), typeof(Logger<>));
builder.Services.AddScoped<ILogger<SomeClass>, Logger<SomeClass>>();
}
}
不,恐怕 AzureFunctionClass 不能只将其 ILogger 实例作为参数传递给 ISomeClass。 我还到处寻找日志文件,例如在 Azure 存储资源管理器中,看看它是否可能只是没有写入 Azure 门户控制台。我找到的每个日志文件都有上述工作案例的日志,而没有一个有其他案例的日志。
【问题讨论】:
标签: c# dependency-injection .net-core azure-functions