コード例 #1
0
        private async Task PublishErrorReportToRollbarAsync(OwnerMetaData owner, LambdaErrorReport report)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }
            if (owner.RollbarAccessToken == null)
            {
                return;
            }

            // convert error report into rollbar data structure
            var rollbar = new Rollbar {
                AccessToken = owner.RollbarAccessToken,
                Data        = new Data {
                    Environment = report.ModuleId,
                    Level       = report.Level?.ToLowerInvariant() ?? "error",
                    Timestamp   = report.Timestamp,
                    CodeVersion = report.GitSha,
                    Platform    = report.Platform,
                    Language    = report.Language,
                    Framework   = report.Framework,
                    Fingerprint = report.Fingerprint,
                    Title       = $"{report.FunctionName}: {report.Message}",
                    Custom      = new {
                        report.Message,
                        report.Module,
                        report.ModuleId,
                        report.FunctionId,
                        report.FunctionName,
                        report.GitBranch,
                        report.RequestId
                    },
                    Body = new DataBody {
                        TraceChain = report.Traces?.Select(trace => new Trace {
                            Exception = new ExceptionClass {
                                Class       = trace.Exception?.Type,
                                Message     = trace.Exception?.Message,
                                Description = trace.Exception?.StackTrace
                            },
                            Frames = trace.Frames?.Select(frame => new Frame {
                                Filename = frame.FileName,
                                Lineno   = frame.LineNumber.GetValueOrDefault(),
                                Method   = frame.MethodName
                            }).ToArray()
                        }).ToArray()
                    }
                }
            };

            // in case there are no captured traces, inject a simple error message
            if (rollbar.Data.Body.TraceChain?.Any() != true)
            {
                rollbar.Data.Body.TraceChain = null;
                rollbar.Data.Body            = new DataBody {
                    Message = new Message {
                        Body = report.Raw
                    }
                };
            }

            // send payload to rollbar
            try {
                var response = _rollbarClient.SendRollbarPayload(rollbar);
                LogInfo($"Rollbar.SendRollbarPayload() succeeded: {response}");
            } catch (WebException e) {
                if (e.Response == null)
                {
                    LogWarn($"Rollbar request failed (status: {e.Status}, message: {e.Message})");
                }
                else
                {
                    using (var stream = e.Response.GetResponseStream()) {
                        if (stream == null)
                        {
                            LogWarn($"Rollbar.SendRollbarPayload() failed: {e.Status}");
                        }
                        using (var reader = new StreamReader(stream)) {
                            LogWarn($"Rollbar.SendRollbarPayload() failed: {reader.ReadToEnd()}");
                        }
                    }
                }
            } catch (Exception e) {
                LogErrorAsWarning(e, "Rollbar.SendRollbarPayload() failed");
            }
        }