Open sandboxFocusImprove this doc

Logging with Application Insights

This article shows how to use PostSharp Logging and Application Insights together.

To use PostSharp Logging with Application Insights:

  1. Add PostSharp Logging to your codebase as described in Getting Started with PostSharp Logging.

  2. Add the PostSharp.Patterns.Diagnostics.ApplicationInsights package to your startup project.

  3. Set up your Application Insights resource according to the official guide.

  4. In the application startup method, include the following code before any logged code:

    // Configure PostSharp Logging to use Application Insights.
    LoggingServices.DefaultBackend = new ApplicationInsightsLoggingBackend( "YOUR_INSTRUMENTATION_KEY" );
    
    Note

    Instead of passing the instrumentation key to the constructor, you can use the parameterless constructor of ApplicationInsightsLoggingBackend. It reads the default Application Insights configuration, for example the environment variable APPINSIGHTS_INSTRUMENTATIONKEY or, on .NET Framework, an ApplicationInsights.config file.

See Also

Reference

ApplicationInsightsLoggingBackend