This package adds support for logging to both Application Insights and Microsoft.Extensions.Logging from a common interface thus removing the need for a dependency on ILogger
and TelemetryClient
.
First, configure dependency injection to include the ILoggerTelemetry<T>
and ILoggerTelemetryFactory
interfaces.
public void ConfigureServices(IServiceCollection serivces)
{
// Add logging and Application Insights if your platform does not
// services.AddLogging();
// serivces.AddApplicationInsightsTelemetry();
services.AddLoggerTelemetry()
}
Then use one of the following to take a dependency:
private readonly ILoggerTelemetry logger;
public MyClass(ILoggerTelemetry<MyClass> logger)
=> this.logger = logger;
public MyClass(ILoggerTelemetryFactory loggerFactory)
=> this.logger = loggerFactory.CreateLogger<MyClass>();
Finally, use the class to log like you would with ILogger
and also be able to pass availability and event telemetry to the TelemetryClient
.
public void ChangeTheThing()
{
logger.LogTrace("About to change the thing");
// ...
logger.LogEvent(nameof(ChangeTheThing));
}
Note: Only requests to the ILoggerTelemetry methods
LogAvailability
andLogEvent
are directly passed to the TelemetryClient, all other logging is passed to theILogger
. The ILogger needs to be configured to use the Application Insights provider to forward other telemetry.