Setting up Stackdriver Trace for C# ASP.NET

You can enable Stackdriver Trace for ASP.NET Framework applications by using the Google.Cloud.Diagnostics.AspNet client library for C#.

Installing the client library

To use the Google.Cloud.Diagnostics.AspNet client library in Visual Studio, do the following:

  1. In Visual Studio, right-click your solution and select Manage Nuget packages for solution.

  2. Select the Include prerelease checkbox.

  3. Search for and install the package named Google.Cloud.Diagnostics.AspNet .

Configuring the client library

After the Google.Cloud.Diagnostics.AspNet is installed, complete the following steps to enable and configure Trace:

  1. If you aren't running on GCP infrastructure or if you want to specify your GCP project ID in your application, add the following XML element to the <appSettings> section your application's Web.config file. Replace YOUR-GOOGLE-PROJECT-ID with your your GCP project ID:

    <add key="projectId" value="YOUR-GOOGLE-PROJECT-ID" />

    If you are running on GCP infrastructure, this step is optional because the client library for C# automatically gathers your GCP project ID from a GCP metadata server.

  2. Add the following using directives to your application's Global.asax.cs file:

    
    using Google.Cloud.Diagnostics.AspNet;
    using Google.Cloud.Diagnostics.Common;
    

    If you don't have the Global.asax file in your ASP.NET application, then create one and add to it the using directives. For more information about Global.asax files, go to Global.asax Syntax.

  3. To initialize Trace, add the following to the Init function in your Global.asax.cs file's WebApiApplication class:

    public override void Init()
    {
        string projectId = ConfigurationManager.AppSettings["projectId"];
        // ...
        base.Init();
        TraceConfiguration traceConfig = TraceConfiguration
            .Create(bufferOptions: BufferOptions.NoBuffer());
        CloudTrace.Initialize(this, projectId, traceConfig);
    }
    

Trace is now configured to sample the HTTP requests received by your application. To initiate traces from your application, see sample application for ASP.NET Framework.

Configuring your platform

You can use Trace on GCP and when your application runs outside of GCP.

Running on GCP

When your application is running on GCP, your application is automatically authenticated and you don't need to provide authentication credentials. However, you do need to ensure that your GCP platform has the Stackdriver Trace API access scope enabled.

For the following configurations, the default settings for the access scopes have the Stackdriver Trace API enabled:

  • App Engine flexible environment
  • Google Kubernetes Engine

  • Compute Engine

If you use custom access scopes, then you must ensure that Stackdriver Trace API access scope enabled. For gcloud users, specify access scopes using the --scopes flag and include the trace.append Stackdriver Trace API access scope. For example, to create a GKE cluster with only the Stackdriver Trace API enabled, do the following:

gcloud container clusters create example-cluster-name --scopes=https://www.googleapis.com/auth/trace.append

Running locally and elsewhere

When your application is running outside of GCP, you must provide authentication credentials in the form of a service account to the client library. The service account must contain the Cloud Trace agent role. For instructions, see Creating a service account.

GCP client libraries use Application default credentials (ADC) to find your application's credentials. You provide these credentials by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable:

Linux/macOS

    export GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key

Windows

    set GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key

PowerShell:

    $env:GOOGLE_APPLICATION_CREDENTIALS="path-to-your-service-accounts-private-key"

Sample Trace application for ASP.NET Framework

public class TraceController : Controller
{
    // This incoming HTTP request should be captured by Trace.
    public ActionResult Index()
    {
        using (CloudTrace.Tracer.StartSpan(nameof(Index)))
        {
            string url = "https://www.googleapis.com/discovery/v1/apis";
            var response = TraceOutgoing(url);
            ViewData["text"] = response.Result.ToString();
            return View();
        }
    }

    public async Task<string> TraceOutgoing(string url)
    {
        // Manually trace a specific operation.
        using (CloudTrace.Tracer.StartSpan("get-api-discovery-doc"))
        {
            using (var httpClient = new HttpClient())
            {
                // This outgoing HTTP request should be captured by Trace.
                using (var response = await httpClient.GetAsync(url)
                    .ConfigureAwait(false))
                {
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
    }
}

Viewing the traces

After deployment, you can view the traces in the GCP Console Trace Viewer.

Go to the Trace Viewer page

Resources

Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...

Stackdriver Trace
Trenger du hjelp? Gå til brukerstøttesiden vår.