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 3.0.0-beta04 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 (3.0.0-beta04) of Google.Cloud.ErrorReporting.V1Beta1.

Authentication

When running on Google Cloud, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is to set up Application Default Credentials. The credentials will automatically be used to authenticate. See Set up Application Default Credentials 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.

Using the REST (HTTP/1.1) transport

This library defaults to performing RPCs using gRPC using the binary Protocol Buffer wire format. However, it also supports HTTP/1.1 and JSON, for situations where gRPC doesn't work as desired. (This is typically due to an incompatible proxy or other network issue.) To create a client using HTTP/1.1, specify a RestGrpcAdapter reference for the GrpcAdapter property in the client builder. Sample code:

var client = new ErrorGroupServiceClientBuilder
{
    GrpcAdapter = RestGrpcAdapter.Default
}.Build();

For more details, see the transport selection page.

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)}");
}