|
| 1 | +To add an attachment, you can either add it to the scope, pass it to any of the `capture` methods, or manipulate the list of attachments in an `EventProcessor` or `beforeSend`. |
| 2 | + |
| 3 | +### Passing Attachments to Capture |
| 4 | + |
| 5 | +You may pass attachments to any of the `capture` methods, for example, when capturing an exception: |
| 6 | + |
| 7 | +```java |
| 8 | +import io.sentry.Attachment; |
| 9 | +import io.sentry.Hint; |
| 10 | +import io.sentry.Sentry; |
| 11 | + |
| 12 | +Sentry.captureException(new IllegalStateException(), Hint.withAttachment("/path/to/file.txt")) |
| 13 | +``` |
| 14 | + |
| 15 | +```kotlin |
| 16 | +import io.sentry.Attachment; |
| 17 | +import io.sentry.Hint; |
| 18 | +import io.sentry.Sentry; |
| 19 | + |
| 20 | +Sentry.captureException(IllegalStateException(), Hint.withAttachment("/path/to/file.txt")) |
| 21 | +``` |
| 22 | + |
| 23 | +### Adding Attachments in `EventProcessor` |
| 24 | + |
| 25 | +You may also manipulate attachments in `EventProcessor`: |
| 26 | + |
| 27 | +```java |
| 28 | +import io.sentry.Attachment; |
| 29 | +import io.sentry.EventProcessor; |
| 30 | +import io.sentry.Hint; |
| 31 | +import io.sentry.Sentry; |
| 32 | +import io.sentry.SentryEvent; |
| 33 | + |
| 34 | +class AttachmentManipulationEventProcessor implements EventProcessor { |
| 35 | + @Override |
| 36 | + public SentryEvent process(SentryEvent event, Hint hint) { |
| 37 | + hint.addAttachment(new Attachment("/path/to/file.txt")) |
| 38 | + return event; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor |
| 43 | + |
| 44 | +// Send an event to Sentry. During construction of the event |
| 45 | +// the attachment will be added by the event processor. |
| 46 | +Sentry.captureMessage("Hello, world!"); |
| 47 | +``` |
| 48 | + |
| 49 | +```kotlin |
| 50 | +import io.sentry.Attachment; |
| 51 | +import io.sentry.EventProcessor; |
| 52 | +import io.sentry.Hint; |
| 53 | +import io.sentry.Sentry |
| 54 | +import io.sentry.SentryEvent; |
| 55 | +import io.sentry.protocol.Message |
| 56 | + |
| 57 | +class AttachmentManipulationEventProcessor: EventProcessor { |
| 58 | + override fun process(event: SentryEvent, hint: Hint): SentryEvent? { |
| 59 | + hint.addAttachment(Attachment("/path/to/file.txt")) |
| 60 | + return event |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor |
| 65 | + |
| 66 | +// Send an event to Sentry. During construction of the event |
| 67 | +// the attachment will be added by the event processor. |
| 68 | +Sentry.captureMessage("Hello, world!") |
| 69 | +``` |
| 70 | + |
| 71 | +<Note> |
| 72 | + |
| 73 | +Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`. |
| 74 | + |
| 75 | +</Note> |
| 76 | + |
| 77 | +### Adding Attachments in `beforeSend` |
| 78 | + |
| 79 | +Another way of adding attachments is using `beforeSend`: |
| 80 | + |
| 81 | +```java |
| 82 | +import io.sentry.Attachment; |
| 83 | +import io.sentry.Hint; |
| 84 | +import io.sentry.Sentry; |
| 85 | + |
| 86 | +options.setBeforeSend((event, hint) -> { |
| 87 | + hint.addAttachment(new Attachment("/path/to/file.txt")) |
| 88 | + return event; |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +```kotlin |
| 93 | +import io.sentry.Attachment; |
| 94 | +import io.sentry.Hint; |
| 95 | +import io.sentry.Sentry; |
| 96 | +import io.sentry.SentryOptions.BeforeSendCallback |
| 97 | + |
| 98 | +options.beforeSend = BeforeSendCallback { event, hint -> |
| 99 | + hint.addAttachment(Attachment("/path/to/file.txt")) |
| 100 | + event |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +<Note> |
| 105 | + |
| 106 | +Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`. |
| 107 | + |
| 108 | +</Note> |
| 109 | + |
| 110 | +### Adding Attachments to the Scope |
0 commit comments