/// <summary> /// Append log. /// </summary> /// <param name="loggingEvent">The event.</param> protected override void Append(LoggingEvent loggingEvent) { if (loggingEvent == null) { return; } if (!Hub.IsEnabled && _sdkHandle == null) { if (Dsn == null) { return; } lock (_initSync) { if (_sdkHandle == null) { _sdkHandle = _initAction(Dsn); Debug.Assert(_sdkHandle != null); } } } var exception = loggingEvent.ExceptionObject ?? loggingEvent.MessageObject as Exception; var evt = new SentryEvent(exception) { Sdk = { Name = Constants.SdkName, Version = NameAndVersion.Version }, Logger = loggingEvent.LoggerName, Level = loggingEvent.ToSentryLevel() }; evt.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version); if (!string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage)) { evt.Message = loggingEvent.RenderedMessage; } evt.SetExtras(GetLoggingEventProperties(loggingEvent)); if (SendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity)) { evt.User = new User { Id = loggingEvent.Identity }; } if (!string.IsNullOrWhiteSpace(Environment)) { evt.Environment = Environment; } _ = Hub.CaptureEvent(evt); }
public void Finish_LinksExceptionToEvent() { // Arrange var client = Substitute.For <ISentryClient>(); var hub = new Hub(client, new SentryOptions { Dsn = DsnSamples.ValidDsnWithoutSecret }); var exception = new InvalidOperationException(); var transaction = new Transaction(hub, "my name", "my op"); // Act transaction.Finish(exception); var @event = new SentryEvent(exception); hub.CaptureEvent(@event); // Assert transaction.Status.Should().Be(SpanStatus.InternalError); client.Received(1).CaptureEvent(Arg.Is <SentryEvent>(e => e.Contexts.Trace.TraceId == transaction.TraceId && e.Contexts.Trace.SpanId == transaction.SpanId && e.Contexts.Trace.ParentSpanId == transaction.ParentSpanId ), Arg.Any <Scope>()); }
public Fixture() { Hub = Substitute.For <IHub>(); Tracer = new TransactionTracer(Hub, "foo", "bar") { IsSampled = true }; _scope = new Scope { Transaction = Tracer }; var logger = Substitute.For <IDiagnosticLogger>(); logger.IsEnabled(Arg.Any <SentryLevel>()).Returns(true); Options = new SentryOptions { TracesSampleRate = 1.0, Debug = true, DiagnosticLogger = logger }; Hub.GetSpan().ReturnsForAnyArgs(_ => Spans?.LastOrDefault(s => !s.IsFinished) ?? Tracer); Hub.CaptureEvent(Arg.Any <SentryEvent>(), Arg.Any <Scope>()).Returns(_ => { Spans.LastOrDefault(s => !s.IsFinished)?.Finish(SpanStatus.InternalError); return(SentryId.Empty); }); Hub.When(hub => hub.ConfigureScope(Arg.Any <Action <Scope> >())) .Do(callback => callback.Arg <Action <Scope> >().Invoke(_scope)); }
/// <summary> /// Append log. /// </summary> /// <param name="loggingEvent">The event.</param> protected override void Append(LoggingEvent loggingEvent) { // Not to throw on code that ignores nullability warnings. // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (loggingEvent is null) { return; } if (!Hub.IsEnabled && _sdkHandle == null) { if (Dsn == null) { return; } lock (_initSync) { if (_sdkHandle == null) { _sdkHandle = _initAction(Dsn); Debug.Assert(_sdkHandle != null); } } } var exception = loggingEvent.ExceptionObject ?? loggingEvent.MessageObject as Exception; var evt = new SentryEvent(exception) { Logger = loggingEvent.LoggerName, Level = loggingEvent.ToSentryLevel() }; if (evt.Sdk is {} sdk) { sdk.Name = Constants.SdkName; sdk.Version = NameAndVersion.Version; if (NameAndVersion.Version is {} version) { sdk.AddPackage(ProtocolPackageName, version); } } if (!string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage)) { evt.Message = loggingEvent.RenderedMessage; } evt.SetExtras(GetLoggingEventProperties(loggingEvent)); if (SendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity)) { evt.User = new User { Id = loggingEvent.Identity }; } if (!string.IsNullOrWhiteSpace(Environment)) { evt.Environment = Environment; } _ = Hub.CaptureEvent(evt); }
/// <summary> /// Append log. /// </summary> /// <param name="loggingEvent">The event.</param> protected override void Append(LoggingEvent loggingEvent) { if (loggingEvent == null) { return; } if (!Hub.IsEnabled && _sdkHandle == null) { if (Dsn == null) { return; } lock (_initSync) { if (_sdkHandle == null) { _sdkHandle = _initAction(Dsn); Debug.Assert(_sdkHandle != null); } } } // Either log a BreadCrumb or an Event if (MinimumBreadcrumbLevel < loggingEvent.Level) { var exception = loggingEvent.ExceptionObject ?? loggingEvent.MessageObject as Exception; var evt = new SentryEvent(exception) { Sdk = { Name = Constants.SdkName, Version = NameAndVersion.Version }, Logger = loggingEvent.LoggerName, Level = loggingEvent.ToSentryLevel() }; evt.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version ?? string.Empty); if (!string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage)) { evt.Message = loggingEvent.RenderedMessage; } evt.SetExtras(GetLoggingEventProperties(loggingEvent)); if (SendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity)) { evt.User = new User { Id = loggingEvent.Identity }; } if (!string.IsNullOrWhiteSpace(Environment)) { evt.Environment = Environment; } Hub.CaptureEvent(evt); } else { string message = !string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage) ? loggingEvent.RenderedMessage : ""; string type = ""; string category = loggingEvent.LoggerName; BreadcrumbLevel level = loggingEvent.ToBreadcrumbLevel(); IDictionary <string, string> data = GetLoggingEventProperties(loggingEvent).ToDictionary(x => x.Key, x => x.Value.ToString()); Hub.AddBreadcrumb(message, category, type, data, level); } }