You can enable Cloud 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:
In Visual Studio, right-click your solution and select Manage Nuget packages for solution.
Select the Include prerelease checkbox.
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:
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. ReplaceYOUR-GOOGLE-PROJECT-ID
with your your Google Cloud 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.
Add the following
using
directives to your application'sGlobal.asax.cs
file:If you don't have the
Global.asax
file in your ASP.NET application, then create one and add to it theusing
directives. For more information aboutGlobal.asax
files, go toGlobal.asax
Syntax.To initialize Trace, add the following to the
Init
function in yourGlobal.asax.cs
file'sWebApiApplication
class:
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 Cloud Trace on Google Cloud and other platforms.
Running on Google Cloud
When your application is running on Google Cloud, you don't need to provide authentication credentials in the form of a service account to the client library. However, you do need to ensure that your Google Cloud platform has the Cloud Trace API access scope enabled.
For the following configurations, the default access-scope settings enable the Cloud Trace API:
- App Engine flexible environment
Google Kubernetes Engine (GKE)
Compute Engine
If you use custom access scopes, then you must ensure that
Cloud Trace API access scope
is enabled. For gcloud
users, specify access scopes using the --scopes
flag
and include the trace.append
Cloud Trace API access scope.
For example, to create a GKE cluster with only
the Cloud 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
If your application is running outside of Google Cloud, then 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:
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
Viewing the traces
After deployment, you can view the traces in the Cloud Console Trace Viewer.
Troubleshooting
For information on troubleshooting issues with Cloud Trace, go to the Troubleshooting page.
Resources
- GitHub:
Google.Cloud.Diagnostics.AspNet
- NuGet:
Google.Cloud.Diagnostics.AspNet
package - Source code
- GitHub issue tracker
- Stack Overflow