If you’re writing Azure Functions you’re probably familiar with Application Insights for doing logging.
The basic examples that Microsoft supply log to Application Insights out of the box.
What these examples don’t fully explain, however, is how to log in a class that isn’t the main (i.e. top-level) function class.
A Basic Setup
Let’s consider a basic setup, where the function logic is all contained in a single class, away from the main function.
public class DoStuff
private readonly ILogger<DoStuff> logger;
public DoStuff(ILogger<DoStuff> logger)
this.logger = logger;
public void DoIt()
logger.LogInformation("Starting to do it.");
// Do something
ILogger<T> is being injected here. This is setup in the default dependency injection configuration, so there’s no need to add it manually.
Unfortunately this isn’t sufficient for the logging to appear in Application Insights - some further configuration changes are required.
host.json will need to be changed as well:
Here we’ve added the
The namespace specified as a child of
AzureFunction.Example - this namespace and all its children will have the specified log level applied to their loggers.
Assuming all your classes are in the defined namespace (or a child namespace), this change you will allow you to log in any and all classes of your solution!