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 .

For more information, see 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. Add the following XML element to the <appSettings> section your application's Web.config file and update YOUR-GOOGLE-PROJECT-ID to be your project ID or number:

    <add key="projectId" value="YOUR-GOOGLE-PROJECT-ID" />
  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 Global.asax in your ASP.NET application, create one and add to it the using directives. For more information about Global.asax files, go to Global.asax Syntax.

  3. In the Global.asax.cs file's WebApiApplication class, add the following Init code block to initialize trace:

    public override void Init()
        string projectId = ConfigurationManager.AppSettings["projectId"];
        // ...
        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.

Running on Google Cloud Platform

You don't need to provide authentication credentials for instances running on Google Cloud Platform, provided Stackdriver Trace API access scope is enabled on that instance. However, we recommend setting the broadest possible access scope for your instance and then using Cloud Identity and Access Management to restrict access.

App Engine flexible environment

On App Engine flexible environment, the Stackdriver Trace API access scope is enabled by default, and the Google.Cloud.Diagnostics.AspNet client library can be used without providing credentials or a project ID.

App Engine standard environment

On App Engine standard, C# isn't currently supported.


To use Trace with GKE, add the trace.append OAuth access scope when you create your cluster.

To create a cluster using gcloud, do the following:

  1. (Optional) Update gcloud to the latest version:
    gcloud components update
  2. Set your default project ID:
    gcloud config set project [PROJECT_ID]
  3. If you're working with zonal clusters, set your default compute zone:
    gcloud config set compute/zone [COMPUTE_ZONE]
  4. If you're working with regional clusters, set your default compute region:
    gcloud config set compute/region [COMPUTE_REGION]
  5. Issue the create command:
    gcloud container clusters create example-cluster-name --scopes

For more detailed information on creating a cluster, see Creating a cluster.

Compute Engine

For Compute Engine VM instances, you need to explicitly enable the trace.append Stackdriver Trace API access scope for each VM instance. When creating a new instance through the Google Cloud Platform Console, select the following values in the Identity and API access section of the Create an instance panel:

  1. Select Compute Engine default service account for Service account
  2. Select Allow full access to all Cloud APIs for Access Scopes

To use something other than the Compute Engine default service account see the Creating and enabling service accounts for instances and Running locally and elsewhere sections. The important thing is that the service account you use has the Cloud Trace Agent role.

Running locally and elsewhere

To run Stackdriver Trace outside of GCP you must supply your GCP project ID and appropriate service account credentials directly to Google.Cloud.Diagnostics.AspNet client library . Your service account must contain the Cloud Trace agent role. For instructions, see Creating a service account.

Project ID

Provide a project ID to the Google.Cloud.Diagnostics.AspNet client library by entering your project ID into the Web.config file as described in Configuring the client library section.


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


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


    set 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 = "";
            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)
                    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

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Trace
Need help? Visit our support page.