Google.Cloud.ErrorReporting.V1Beta1
Google.Cloud.ErrorReporting.V1Beta1
is a.NET client library for the Google Cloud Error Reporting API.
Note:
This documentation is for version 2.0.0-beta05
of the library.
Some samples may not work with other versions.
Support for automatic error reporting can be found in the
Google.Cloud.Diagnostics.AspNetCore
and
Google.Cloud.Diagnostics.AspNetCore3
NuGet packages.
Those packages are generally preferred over using the Error Reporting API directly.
Installation
Install the Google.Cloud.ErrorReporting.V1Beta1
package from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Please ensure you enable pre-release packages (for example, in the
Visual Studio NuGet user interface, check the "Include prerelease"
box). Some of the following samples might only work with the latest
pre-release version (2.0.0-beta05
) of Google.Cloud.ErrorReporting.V1Beta1
.
Authentication
When running on Google Cloud Platform, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to
download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to refer to it.
The credentials will automatically be used to authenticate. See the Getting Started With
Authentication guide for more details.
Getting started
All operations are performed through the following client classes:
Create a client instance by calling the static Create
or CreateAsync
methods. Alternatively,
use the builder class associated with each client class (e.g. ErrorGroupServiceClientBuilder for ErrorGroupServiceClient)
as an easy way of specifying custom credentials, settings, or a custom endpoint. Clients are thread-safe,
and we recommend using a single instance across your entire application unless you have a particular need
to configure multiple client objects separately.
Sample code using the Error Reporting API directly
Report an error
ReportErrorsServiceClient client = ReportErrorsServiceClient.Create();
ProjectName projectName = new ProjectName(projectId);
ReportedErrorEvent error = new ReportedErrorEvent
{
Context = new ErrorContext
{
ReportLocation = new SourceLocation
{
FilePath = "SampleApp.BusinessLogic/ComplexLogic.cs",
FunctionName = "ComputeTrickyAnswer",
LineNumber = 100
},
User = "userid"
},
// If this is a stack trace, the service will parse it.
Message = "Computation failed",
EventTime = Timestamp.FromDateTime(DateTime.UtcNow),
ServiceContext = new ServiceContext
{
Service = "SampleApp",
Version = "1.0.0"
}
};
client.ReportErrorEvent(projectName, error);
List error groups with statistics
ErrorStatsServiceClient client = ErrorStatsServiceClient.Create();
ProjectName projectName = new ProjectName(projectId);
PagedEnumerable<ListGroupStatsResponse, ErrorGroupStats> groupStats = client.ListGroupStats(
projectName,
new QueryTimeRange { Period = Period._30Days });
foreach (ErrorGroupStats item in groupStats)
{
// Sample output: Group: 8002882452986879952; Count: 6; Services: SampleApp/1.0.0
IEnumerable<string> services = item.AffectedServices.Select(s => $"{s.Service}/{s.Version}");
Console.WriteLine($"Group: {item.Group.GroupId}; Count: {item.Count}; Services: {string.Join(", ", services)}");
}