We have a Azure Function v2.0 that uses a dependency that performs logging, instead of forwarding the ILogger that is injected into the function, can this component in some way get ahold of IServiceLocator so we can perform GetService() to get ahold of the current logger.
I have tried to bootstrap it in Startup, but this is not possible since the ILogger is not yet built up at that point in the lifecycle of the function app (see here: Weird exception when trying to use Dependency Injection in an Azure Function).
What is the best practice for using (singleton) dependencies in Azure Functions where we want to inject the ILogger?
EDIT: As requested I will provide som sample code to illustrate better what I am trying to achieve:
Startup {
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IContentProvider, IContentProvider>();
}
}
...
public class ContentProvider : IContentProvider {
private ILogger _logger;
public ContentProvider(ILogger logger) {
_logger = logger;
}
public bool DoSomething() {
_logger.LogInformation("Doing something..."); // BOOM: _logger is null
}
}
...
public class Functions {
public StoreFunctions(
IContentProvider contentProvider
)
{
_contentProvider = contentProvider;
}
[FunctionName("MetadataStoreMessage")]
public async Task ReadMessage([ServiceBusTrigger(TopicName, SubscriptionName, Connection = "MessageBusConnection")]
string messageBody,
ILogger logger,
ExecutionContext context)
{
_contentProvider.DoSomething();
}
}
EDIT 2: By injecting ILogger the logger in not null, but at the same time it is not set up with logging to an additional logger (Seq), where we want all our logs to be sent:
builder.Services
.AddLogging(logging => logging.AddSeq(
Environment.GetEnvironmentVariable("SeqEndpoint"),
apiKey: Environment.GetEnvironmentVariable("SeqApiKey"))
);
builder.Services.AddSingleton<IContentProvider, ContentProvider>();
What do I have to set to enable the AddLogging() to apply to the injectable ILogger ?