Azure / Azure/Azure-Functions

Getting an ILogger<> instance from DI that routes to App Insights

Open
#941 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
PowerShell
Stars
1.1k
Forks
215
Avg merge
4h 2m
Merged PRs (30d)
1

Description

I know that it's possible to declare a `ILogger` as parameter of a Function and it automatically logs to App Insights (if the instrumentation key is declared in the app settings), which is really great.

Now my Functions are also using some services declared in other assemblies (and shared across Functions and ASP.NET Core). My approach is to build a static `ServiceProvider` (as described [here](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.0)) in the static constructor of the class hosting my Functions. Each Function then uses this `ServiceProvider` to retrieve the desired service (through `GetService<>`), which gets properly initialized with all its dependencies injected in its constructor. It all works well, except that the injected loggers (of type `ILogger`) don't output to App Insights.

When consuming the services with ASP.NET Core, I use the extension method `ILoggerFactory.AddApplicationInsights`, which is defined in the package `Microsoft.ApplicationInsights.AspNetCore`. What would be the way to go when running these services in Functions? I "just" need the injected `ILogger<>` instances to behave like the `ILogger` that gets injected at the Function level.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.