Skip to content

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.

License

Notifications You must be signed in to change notification settings

vladkasianenko/Extensions.Microsoft.Logging.ApplicationInsights

 
 

Repository files navigation

Extensions.Microsoft.Logging.ApplicationInsights

Build SonarCloud Status NuGet NuGet

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.

Getting started

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 and LogEvent are directly passed to the TelemetryClient, all other logging is passed to the ILogger. The ILogger needs to be configured to use the Application Insights provider to forward other telemetry.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%