public void report(Exception e) { // Create the report and execute the request. var reporter = ReportErrorsServiceClient.Create(); // Get the project ID from the json file using (StreamReader r = new StreamReader("../../../S2TPrivateKey.json")) { string json = r.ReadToEnd(); project_id = Newtonsoft.Json.Linq.JObject.Parse(json)["project_id"].ToString(); } var projectName = new ProjectName(project_id);//construct the ProjectName object // Add a service context to the report. For more details see: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext ServiceContext serviceContext = new ServiceContext() { Service = "SpeechToText", Version = "0.1", }; ReportedErrorEvent errorEvent = new ReportedErrorEvent() { Message = e.ToString(), ServiceContext = serviceContext, }; reporter.ReportErrorEvent(projectName, errorEvent); }
/// <summary> /// Creates a <seealso cref="ReportRequest"/> from a given exception. /// </summary> private static ReportRequest CreateReportRequest(Exception e) { // Create the service. ClouderrorreportingService service = CreateErrorReportingClient(); // Format the project id to the format Error Reporting expects. See: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report string formattedProjectId = string.Format("projects/{0}", ProjectId); // Add a service context to the report. For more details see: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext ServiceContext serviceContext = new ServiceContext() { Service = "e_reporting", Version = "the-exceptional-api-version" }; ErrorContext errorContext = new ErrorContext() { ReportLocation = new SourceLocation() { FunctionName = "A hidden Name" } }; ReportedErrorEvent errorEvent = new ReportedErrorEvent() { Message = e.ToString(), ServiceContext = serviceContext, Context = errorContext }; return(new ReportRequest(service, errorEvent, formattedProjectId)); }
/// <inheritdoc /> public void OnException(ExceptionContext context) { ReportedErrorEvent errorEvent = CreateReportRequest(context); // If the client task has faulted this will throw when accessing 'Result' _clientTask.Result.ReportErrorEvent(_projectResourceName, errorEvent); }
/// <summary> /// Creates a <seealso cref="ReportRequest"/> from a given exception. /// </summary> private static ReportRequest CreateReportRequest(Exception e) { // Create the service. ClouderrorreportingService service = CreateErrorReportingClient(); // Get the project ID from the environement variables. string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); // Format the project id to the format Error Reporting expects. See: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report string formattedProjectId = string.Format("projects/{0}", projectId); // Add a service context to the report. For more details see: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext ServiceContext serviceContext = new ServiceContext() { Service = "myapp", Version = "8c1917a9eca3475b5a3686d1d44b52908463b989", }; ReportedErrorEvent errorEvent = new ReportedErrorEvent() { Message = e.ToString(), ServiceContext = serviceContext, }; return(new ReportRequest(service, errorEvent, formattedProjectId)); }
public void ReportErrorEvent() { string projectId = _fixture.ProjectId; // Sample: ReportErrorEvent // Additional: ReportErrorEvent(*,*,*) 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 = "******" }, // 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); // End sample }
public void ConvertErrorEvents() { var startTime = DateTime.UtcNow; var eventOne = new ReportedErrorEvent { Message = "Event one!", EventTime = Timestamp.FromDateTime(startTime), Context = new ErrorContext { HttpRequest = new HttpRequestContext() { Method = "GET" } } }; var eventTwo = new ReportedErrorEvent { Message = "Event two!", EventTime = Timestamp.FromDateTime(startTime.AddSeconds(2)), Context = new ErrorContext { ReportLocation = new SourceLocation() { LineNumber = 10 } } }; var eventThree = new ReportedErrorEvent { Message = "Event three!", EventTime = Timestamp.FromDateTime(startTime.AddSeconds(4)) }; var errorConsumer = CreateErrorConsumer(new Mock <IConsumer <LogEntry> >().Object); var logEntries = errorConsumer.ConvertErrorEvents(new [] { eventOne, eventTwo, eventThree }); Assert.Equal(3, logEntries.Count()); foreach (var entry in logEntries) { Assert.Equal(s_monitoredResource, entry.Resource); Assert.Contains(_logName, entry.LogName); Assert.Equal(LogSeverity.Error, entry.Severity); if (entry.Timestamp.Equals(eventOne.EventTime)) { Assert.Equal(entry.JsonPayload, eventOne.ToStruct()); } else if (entry.Timestamp.Equals(eventTwo.EventTime)) { Assert.Equal(entry.JsonPayload, eventTwo.ToStruct()); } else if (entry.Timestamp.Equals(eventThree.EventTime)) { Assert.Equal(entry.JsonPayload, eventThree.ToStruct()); } } }
/// <summary>Snippet for ReportErrorEvent</summary> public void ReportErrorEvent() { // Snippet: ReportErrorEvent(string, ReportedErrorEvent, CallSettings) // Create client ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create(); // Initialize request argument(s) string projectName = "projects/[PROJECT]"; ReportedErrorEvent @event = new ReportedErrorEvent(); // Make the request ReportErrorEventResponse response = reportErrorsServiceClient.ReportErrorEvent(projectName, @event); // End snippet }
/// <summary>Snippet for ReportErrorEvent</summary> public void ReportErrorEventResourceNames() { // Snippet: ReportErrorEvent(ProjectName, ReportedErrorEvent, CallSettings) // Create client ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create(); // Initialize request argument(s) ProjectName projectName = ProjectName.FromProject("[PROJECT]"); ReportedErrorEvent @event = new ReportedErrorEvent(); // Make the request ReportErrorEventResponse response = reportErrorsServiceClient.ReportErrorEvent(projectName, @event); // End snippet }
public async Task ReportErrorEventAsync() { // Snippet: ReportErrorEventAsync(string,ReportedErrorEvent,CallSettings) // Additional: ReportErrorEventAsync(string,ReportedErrorEvent,CancellationToken) // Create client ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create(); // Initialize request argument(s) string formattedProjectName = ReportErrorsServiceClient.FormatProjectName("[PROJECT]"); ReportedErrorEvent @event = new ReportedErrorEvent(); // Make the request ReportErrorEventResponse response = await reportErrorsServiceClient.ReportErrorEventAsync(formattedProjectName, @event); // End snippet }
/// <summary>Snippet for ReportErrorEventAsync</summary> public async Task ReportErrorEventResourceNamesAsync() { // Snippet: ReportErrorEventAsync(ProjectName, ReportedErrorEvent, CallSettings) // Additional: ReportErrorEventAsync(ProjectName, ReportedErrorEvent, CancellationToken) // Create client ReportErrorsServiceClient reportErrorsServiceClient = await ReportErrorsServiceClient.CreateAsync(); // Initialize request argument(s) ProjectName projectName = ProjectName.FromProject("[PROJECT]"); ReportedErrorEvent @event = new ReportedErrorEvent(); // Make the request ReportErrorEventResponse response = await reportErrorsServiceClient.ReportErrorEventAsync(projectName, @event); // End snippet }
public void CreateReportErrorEventAsync(Exception ex, string userid, string service, string version, HttpContext httpContext = null) { ProjectName projectName = new ProjectName(projectId); StackTrace stackTrace = new StackTrace(ex, true); StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); ReportedErrorEvent error = new ReportedErrorEvent { Context = new ErrorContext { ReportLocation = new SourceLocation { FilePath = stackFrame.GetFileName(), FunctionName = stackFrame.GetMethod().Name, LineNumber = stackFrame.GetFileLineNumber(), }, User = userid }, // If this is a stack trace, the service will parse it. Message = ex.Message, EventTime = Timestamp.FromDateTime(DateTime.UtcNow), ServiceContext = new ServiceContext { Service = service, Version = version } }; if (httpContext != null) { HttpRequestContext httpRequest = new HttpRequestContext { Method = httpContext.Request.Method, Referrer = httpContext.Request.Path, RemoteIp = httpContext.Connection.RemoteIpAddress.ToString(), Url = httpContext.Request.Path }; error.Context.HttpRequest = httpRequest; } client.ReportErrorEventAsync(projectName, error); }
public void reportError(Exception err) { string projectId = "434076015357"; ProjectName project_name = new ProjectName(projectId); string err_mssage = err.ToString(); ReportedErrorEvent error_event = new ReportedErrorEvent(); ErrorContext context = new ErrorContext(); SourceLocation location = new SourceLocation(); location.FunctionName = this.api; context.ReportLocation = location; error_event.Context = context; error_event.Message = "CSharpProbeFailure: fails on {$this.api} API. Details: {$err_message}"; this.err_client.ReportErrorEvent(project_name, error_event); return; }
public static int Main(string[] args) { // Read projectId from args if (args.Length != 1) { Console.WriteLine("Usage: Project ID must be passed as first argument."); Console.WriteLine(); return(1); } string projectId = args[0]; // Create client ReportErrorsServiceClient client = ReportErrorsServiceClient.Create(); // Initialize request argument(s) ProjectName projectName = new ProjectName(projectId); ReportedErrorEvent @event = new ReportedErrorEvent { Message = "[MESSAGE]", ServiceContext = new ServiceContext { Service = "[SERVICE]", }, Context = new ErrorContext { ReportLocation = new SourceLocation { FilePath = "path/to/file.lang", LineNumber = 42, FunctionName = "meaningOfLife", }, }, }; // Call API method ReportErrorEventResponse response = client.ReportErrorEvent(projectName, @event); // Show the result Console.WriteLine(response); // Success Console.WriteLine("Smoke test passed OK"); return(0); }
public void ReportErrorEvent() { Mock <ReportErrorsService.ReportErrorsServiceClient> mockGrpcClient = new Mock <ReportErrorsService.ReportErrorsServiceClient>(MockBehavior.Strict); ReportErrorEventRequest expectedRequest = new ReportErrorEventRequest { ProjectNameAsProjectName = new ProjectName("[PROJECT]"), Event = new ReportedErrorEvent(), }; ReportErrorEventResponse expectedResponse = new ReportErrorEventResponse(); mockGrpcClient.Setup(x => x.ReportErrorEvent(expectedRequest, It.IsAny <CallOptions>())) .Returns(expectedResponse); ReportErrorsServiceClient client = new ReportErrorsServiceClientImpl(mockGrpcClient.Object, null); ProjectName projectName = new ProjectName("[PROJECT]"); ReportedErrorEvent @event = new ReportedErrorEvent(); ReportErrorEventResponse response = client.ReportErrorEvent(projectName, @event); Assert.Same(expectedResponse, response); mockGrpcClient.VerifyAll(); }
public async Task ReportErrorEventAsync() { Mock <ReportErrorsService.ReportErrorsServiceClient> mockGrpcClient = new Mock <ReportErrorsService.ReportErrorsServiceClient>(MockBehavior.Strict); ReportErrorEventRequest expectedRequest = new ReportErrorEventRequest { ProjectNameAsProjectName = new ProjectName("[PROJECT]"), Event = new ReportedErrorEvent(), }; ReportErrorEventResponse expectedResponse = new ReportErrorEventResponse(); mockGrpcClient.Setup(x => x.ReportErrorEventAsync(expectedRequest, It.IsAny <CallOptions>())) .Returns(new Grpc.Core.AsyncUnaryCall <ReportErrorEventResponse>(Task.FromResult(expectedResponse), null, null, null, null)); ReportErrorsServiceClient client = new ReportErrorsServiceClientImpl(mockGrpcClient.Object, null); ProjectName projectName = new ProjectName("[PROJECT]"); ReportedErrorEvent @event = new ReportedErrorEvent(); ReportErrorEventResponse response = await client.ReportErrorEventAsync(projectName, @event); Assert.Same(expectedResponse, response); mockGrpcClient.VerifyAll(); }
public void ToStruct() { var message = "exception message"; var method = "DELETE"; var lineNumber = 116; var errorEvent = new ReportedErrorEvent() { Message = message, Context = new ErrorContext() { HttpRequest = new HttpRequestContext() { Method = method }, ReportLocation = new SourceLocation() { LineNumber = lineNumber } }, }; var errorStruct = errorEvent.ToStruct(); Assert.NotEmpty(errorStruct?.Fields); Assert.Equal(message, errorStruct.Fields["message"].StringValue); var contextStruct = errorStruct.Fields["context"].StructValue; Assert.NotEmpty(contextStruct?.Fields); var httpStruct = contextStruct.Fields["httpRequest"].StructValue; Assert.NotEmpty(httpStruct?.Fields); Assert.Equal(method, httpStruct.Fields["method"].StringValue); var locationStruct = contextStruct.Fields["reportLocation"].StructValue; Assert.NotEmpty(locationStruct?.Fields); Assert.Equal(lineNumber, locationStruct.Fields["lineNumber"].NumberValue); }
/// <summary> /// Report an exception to the Error Reporting service. /// </summary> private static void report(Exception e) { // Create the report and execute the request. var reporter = ReportErrorsServiceClient.Create(); // Get the project ID from the environement variables. var projectName = new ProjectName( Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID")); // Add a service context to the report. For more details see: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext ServiceContext serviceContext = new ServiceContext() { Service = "myapp", Version = "1.1", }; ReportedErrorEvent errorEvent = new ReportedErrorEvent() { Message = e.ToString(), ServiceContext = serviceContext, }; reporter.ReportErrorEvent(projectName, errorEvent); }
/// <inheritdoc /> public override Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken) { ReportedErrorEvent errorEvent = CreateReportRequest(context); return(_client.ReportErrorEventAsync(_projectResourceName, errorEvent)); }
/// <summary> /// Convert a <see cref="ReportedErrorEvent"/> to a <see cref="Struct"/>. /// </summary> public static Struct ToStruct(this ReportedErrorEvent errorEvent) { GaxPreconditions.CheckNotNull(errorEvent, nameof(errorEvent)); return(Struct.Parser.ParseJson(JsonFormatter.Default.Format(errorEvent))); }
/// <summary> /// Creates a <seealso cref="ReportRequest"/> from a given exception. /// </summary> private static ReportRequest CreateReportRequest(Exception e) { // Create the service. ClouderrorreportingService service = CreateErrorReportingClient(); // Get the project ID from the environement variables. string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); // Format the project id to the format Error Reporting expects. See: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report string formattedProjectId = string.Format("projects/{0}", projectId); // Add a service context to the report. For more details see: // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext ServiceContext serviceContext = new ServiceContext() { Service = "myapp", Version = "8c1917a9eca3475b5a3686d1d44b52908463b989", }; ReportedErrorEvent errorEvent = new ReportedErrorEvent() { Message = e.ToString(), ServiceContext = serviceContext, }; return new ReportRequest(service, errorEvent, formattedProjectId); }
/// <inheritdoc /> public override void Log(ExceptionLoggerContext context) { ReportedErrorEvent errorEvent = CreateReportRequest(context); _client.ReportErrorEvent(_projectResourceName, errorEvent); }
/// <inheritdoc /> public override async Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken) { ReportedErrorEvent errorEvent = CreateReportRequest(context); ReportErrorsServiceClient client = await _clientTask; await client.ReportErrorEventAsync(_projectResourceName, errorEvent); }