PostSharp 6.8 / / Post­Sharp Documentation / Logging / Connecting to Source and Target Logging Frameworks / Instrumentation of ASP.​NET and Http­Client

Instrumentation of ASP.NET and HttpClient

PostSharp Logging is not only able to collect evetns from several logging frameworks, but it also comes with packages that directly support specific software stacks. Currently, three are supported:

This topic contains the following sections:

ASP.NET Core

If you're using ASP.NET Core, PostSharp Logging allows you to change the verbosity of logging dynamically, with our without source code, depending on the parameters of the incoming web requests. It also properly interprets the headers set by the HttpClient for distributed transaction tracking.

To enable per-request logging with ASP.NET Core:

  1. Install the PostSharp.Patterns.Diagnostics.AspNetCore package into your top-level project.

  2. In your Program.Main method, include a call to the AspNetCoreLogging.Initialize method.

    If you want to enable support for distributed logging, you need to pass a value to the correlationProtocol parameter. The only available implementation is currently LegacyHttpCorrelationProtocol.

C#
// Defines a filter that selects trusted requests. 
// Enabling HTTP Correlation Protocol for communication with untrusted devices is a security risk.
Predicate<CorrelationRequest> trustedRequests = request => request.RemoteHost == "localhost" || 
                                                 request.RemoteHost == "127.0.0.1" ||
                                                 request.RemoteHost == "::1";

// Assigns the HTTP Correlation Protocol to ASP.NET Core instrumentation.
AspNetCoreLogging.Initialize( correlationProtocol: new LegacyHttpCorrelationProtocol( trustedRequests ) );

This will log any incoming HTTP request and, if you have set up request correlation, it will interpret the HTTP headers related to distributed headers. To learn how to configure verbosity on a per-request basis, see Adjusting Logging Verbosity for details.

Legacy ASP.NET

PostSharp Logging comes with an HTTP module that integrates with the legacy ASP.NET and IIS.

To enable per-request logging with ASP.NET:

  1. Install the PostSharp.Patterns.Diagnostics.AspNetFramework package into your top-level project.

  2. Add PostSharpLoggingHttpModule as an HTTP module.

    XML
    <configuration>
      <system.webServer>
        <modules>
          <add name="PostSharpLogging" type="PostSharp.Patterns.Diagnostics.Adapters.AspNetFramework.PostSharpLoggingHttpModule"/>
        </modules>
      </system.webServer>
    </configuration>
  3. If you want to enable support for distributed logging, you need set the PostSharpLoggingHttpModule.CorrelationProtocol property during your application startup. The only available implementation is currently LegacyHttpCorrelationProtocol.

    Therefore, add this to your Global.asax:

    C#
    // Defines a filter that selects trusted requests.
    // Enabling HTTP Correlation Protocol for communication with untrusted devices is a security risk.
    Predicate<CorrelationRequest> trustedRequests = request => request.RemoteHost == "localhost" || 
                                                     request.RemoteHost == "127.0.0.1" ||
                                                     request.RemoteHost == "::1";
    
    // Assigns the HTTP Correlation Protocol to the HTTP module.
    PostSharpLoggingHttpModule.CorrelationProtocol = new LegacyHttpCorrelationProtocol( trustedRequests );

This will log any incoming HTTP request and, if you have set up request correlation, it will interpret the HTTP headers related to distributed headers. To learn how to configure verbosity on a per-request basis, see Adjusting Logging Verbosity for details.

HttpClient

You can configure PostSharp Logging to instrument the .NET HttpClient class. This instrumentation will create an activity for any HttpClient operation and will properly set the headers so that distributed transactions can be tracked. It works together with the ASP.NET instrumentation.

To add the HttpClient instrumentation to your code:

  1. Install the PostSharp.Patterns.Diagnostics.HttpClient package into your top-level project.

  2. In your Program.Main method, include a call to the HttpClientLogging.Initialize method.

    If you want to enable support for distributed logging, you need to pass a value to the correlationProtocol parameter. The only available implementation is currently LegacyHttpCorrelationProtocol. It is important that you use the same protocol in the client and the server.

C#
// Defines a filter that selects trusted requests.
// Enabling HTTP Correlation Protocol for communication with untrusted devices is a security risk.
Predicate<CorrelationRequest> trustedRequests = request => request.RemoteHost == "localhost" || 
                                                 request.RemoteHost == "127.0.0.1" ||
                                                 request.RemoteHost == "::1";

// Assigns the HTTP Correlation Protocol to HttpClient instrumentation.
HttpClientLogging.Initialize( correlationProtocol: new LegacyHttpCorrelationProtocol( trustedRequests ) );
See Also