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 Google Cloud infrastructure or if you want to specify your Google Cloud 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 Google Cloud project ID:

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

    If you are running on Google Cloud infrastructure, this step is optional because the client library for C# automatically gathers your Google Cloud project ID from a Google Cloud 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"];
        // ...
        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 Google Cloud and when your application runs outside of Google Cloud.

Running on Google Cloud

When your application is running on Google Cloud, your application is automatically authenticated and you don't need to provide authentication credentials. However, you do need to ensure that your Google Cloud 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=

Running locally and elsewhere

When your application is running outside of Google Cloud, 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.

Google Cloud 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:


    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 Cloud Console Trace Viewer.

Go to the Trace Viewer page


หน้านี้มีประโยชน์ไหม โปรดแสดงความคิดเห็น


หากต้องการความช่วยเหลือ ให้ไปที่หน้าการสนับสนุน