示例#1
0
        private static async Task <ISerializable> DeserializePayloadAsync(
            Stream stream,
            IReadOnlyDictionary <string, object?> header,
            CancellationToken cancellationToken = default)
        {
            var payloadLength = header.GetValueOrDefault(LengthKey) switch
            {
                null => (long?)null,
                var value => Convert.ToInt64(value)
            };

            var payloadType = header.GetValueOrDefault(TypeKey) as string;

            // Event
            if (string.Equals(payloadType, TypeValueEvent, StringComparison.OrdinalIgnoreCase))
            {
                var bufferLength = (int)(payloadLength ?? stream.Length);
                var buffer       = await stream.ReadByteChunkAsync(bufferLength, cancellationToken).ConfigureAwait(false);

                var json = Json.Parse(buffer);

                return(new JsonSerializable(SentryEvent.FromJson(json)));
            }

            // User report
            if (string.Equals(payloadType, TypeValueUserReport, StringComparison.OrdinalIgnoreCase))
            {
                var bufferLength = (int)(payloadLength ?? stream.Length);
                var buffer       = await stream.ReadByteChunkAsync(bufferLength, cancellationToken).ConfigureAwait(false);

                var json = Json.Parse(buffer);

                return(new JsonSerializable(UserFeedback.FromJson(json)));
            }

            // Transaction
            if (string.Equals(payloadType, TypeValueTransaction, StringComparison.OrdinalIgnoreCase))
            {
                var bufferLength = (int)(payloadLength ?? stream.Length);
                var buffer       = await stream.ReadByteChunkAsync(bufferLength, cancellationToken).ConfigureAwait(false);

                var json = Json.Parse(buffer);

                return(new JsonSerializable(Transaction.FromJson(json)));
            }

            // Arbitrary payload
            var payloadStream = new PartialStream(stream, stream.Position, payloadLength);

            if (payloadLength != null)
            {
                stream.Seek(payloadLength.Value, SeekOrigin.Current);
            }
            else
            {
                stream.Seek(0, SeekOrigin.End);
            }

            return(new StreamSerializable(payloadStream));
        }
示例#2
0
        public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
        {
            var ex        = new Exception("exception message");
            var timestamp = DateTimeOffset.MaxValue;
            var id        = Guid.Parse("4b780f4c-ec03-42a7-8ef8-a41c9d5621f8");
            var sut       = new SentryEvent(ex, timestamp, id)
            {
                User = new User {
                    Id = "user-id"
                },
                Request = new Request {
                    Method = "POST"
                },
                Contexts = new Contexts {
                    ["context_key"]    = "context_value",
                    [".NET Framework"] = new Dictionary <string, string> {
                        [".NET Framework"]        = "\"v2.0.50727\", \"v3.0\", \"v3.5\"",
                        [".NET Framework Client"] = "\"v4.8\", \"v4.0.0.0\"",
                        [".NET Framework Full"]   = "\"v4.8\""
                    }
                },
                Sdk = new SdkVersion {
                    Name = "SDK-test", Version = "1.1.1"
                },
                Environment = "environment",
                Level       = SentryLevel.Fatal,
                Logger      = "logger",
                Message     = new SentryMessage
                {
                    Message   = "message",
                    Formatted = "structured_message"
                },
                Modules          = { { "module_key", "module_value" } },
                Release          = "release",
                SentryExceptions = new[] { new SentryException {
                                               Value = "exception_value"
                                           } },
                SentryThreads = new[] { new SentryThread {
                                            Crashed = true
                                        } },
                ServerName      = "server_name",
                TransactionName = "transaction",
            };

            sut.Sdk.AddPackage(new Package("name", "version"));
            sut.AddBreadcrumb(new Breadcrumb(timestamp, "crumb"));
            sut.AddBreadcrumb(new Breadcrumb(
                                  timestamp,
                                  "message",
                                  "type",
                                  new Dictionary <string, string> {
                { "data-key", "data-value" }
            },
                                  "category",
                                  BreadcrumbLevel.Warning));

            sut.SetExtra("extra_key", "extra_value");
            sut.Fingerprint = new[] { "fingerprint" };
            sut.SetTag("tag_key", "tag_value");

            var actualString = sut.ToJsonString();

            var actual = SentryEvent.FromJson(Json.Parse(actualString));

            actual.Should().BeEquivalentTo(sut, o =>
            {
                // Due to timestamp precision
                o.Excluding(e => e.Breadcrumbs);
                o.Excluding(e => e.Exception);

                return(o);
            });

            // Expected item[0].Timestamp to be <9999-12-31 23:59:59.9999999>, but found <9999-12-31 23:59:59.999>.
            actual.Breadcrumbs.Should().BeEquivalentTo(sut.Breadcrumbs, o => o.Excluding(b => b.Timestamp));
            var counter = 0;

            foreach (var sutBreadcrumb in sut.Breadcrumbs)
            {
                sutBreadcrumb.Timestamp.Should().BeCloseTo(actual.Breadcrumbs.ElementAt(counter++).Timestamp);
            }
        }
示例#3
0
        public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
        {
            var ex        = new Exception("exception message");
            var timestamp = DateTimeOffset.MaxValue;
            var id        = Guid.Parse("4b780f4c-ec03-42a7-8ef8-a41c9d5621f8");
            var sut       = new SentryEvent(ex, timestamp, id)
            {
                User = new User {
                    Id = "user-id"
                },
                Request = new Request {
                    Method = "POST"
                },
                Contexts = new Contexts
                {
                    ["context_key"]    = "context_value",
                    [".NET Framework"] = new Dictionary <string, string>
                    {
                        [".NET Framework"]        = "\"v2.0.50727\", \"v3.0\", \"v3.5\"",
                        [".NET Framework Client"] = "\"v4.8\", \"v4.0.0.0\"",
                        [".NET Framework Full"]   = "\"v4.8\""
                    }
                },
                Sdk = new SdkVersion {
                    Name = "SDK-test", Version = "1.1.1"
                },
                Environment = "environment",
                Level       = SentryLevel.Fatal,
                Logger      = "logger",
                Message     = new SentryMessage
                {
                    Message   = "message",
                    Formatted = "structured_message"
                },
                Modules          = { { "module_key", "module_value" } },
                Release          = "release",
                SentryExceptions = new[] { new SentryException {
                                               Value = "exception_value"
                                           } },
                SentryThreads = new[] { new SentryThread {
                                            Crashed = true
                                        } },
                ServerName      = "server_name",
                TransactionName = "transaction",
            };

            sut.Sdk.AddPackage(new Package("name", "version"));
            sut.AddBreadcrumb(new Breadcrumb(timestamp, "crumb"));
            sut.AddBreadcrumb(new Breadcrumb(
                                  timestamp,
                                  "message",
                                  "type",
                                  new Dictionary <string, string> {
                { "data-key", "data-value" }
            },
                                  "category",
                                  BreadcrumbLevel.Warning));

            sut.SetExtra("extra_key", "extra_value");
            sut.Fingerprint = new[] { "fingerprint" };
            sut.SetTag("tag_key", "tag_value");

            var actualString = sut.ToJsonString();

            var actual = SentryEvent.FromJson(Json.Parse(actualString));

            // Assert
            actual.Should().BeEquivalentTo(sut, o =>
            {
                // Exceptions are not deserialized
                o.Excluding(x => x.Exception);

                // Timestamps lose some precision when writing to JSON
                o.Using <DateTimeOffset>(ctx =>
                                         ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromMilliseconds(1))
                                         ).WhenTypeIs <DateTimeOffset>();

                return(o);
            });
        }