|
1 | 1 | using System;
|
2 | 2 | using System.Runtime.CompilerServices;
|
3 |
| -using Sentry; |
4 | 3 | using Sentry.Unity;
|
5 | 4 | using UnityEngine;
|
6 |
| -using UnityEngine.Assertions; |
7 | 5 |
|
8 | 6 | public class BugFarmButtons : MonoBehaviour
|
9 | 7 | {
|
10 | 8 | private void Awake()
|
11 | 9 | {
|
12 |
| - Debug.Log("Sample 🐛"); |
| 10 | + Debug.Log("The 🐛s awaken!"); |
13 | 11 | }
|
14 | 12 |
|
15 | 13 | private void Start()
|
16 | 14 | {
|
17 |
| - Debug.Log("Sample Start 🦋"); |
| 15 | + // Log messages are getting captured as breadcrumbs |
| 16 | + Debug.Log("Starting the 🦋-Farm"); |
18 | 17 | Debug.LogWarning("Here come the bugs 🐞🦋🐛🐜🕷!");
|
19 | 18 | }
|
20 | 19 |
|
21 |
| - public void AssertFalse() => Assert.AreEqual(true, false); |
22 |
| - |
23 |
| - [MethodImpl(MethodImplOptions.NoInlining)] |
24 |
| - public void ThrowNull() => throw new NullReferenceException(); |
| 20 | + public void ThrowUnhandledException() |
| 21 | + { |
| 22 | + Debug.Log("Throwing a unhandled 🕷 exception!"); |
| 23 | + DoSomeWorkHere(); |
| 24 | + } |
25 | 25 |
|
26 |
| - public void ThrowExceptionAndCatch() |
| 26 | + public void ThrowExceptionButCatch() |
27 | 27 | {
|
28 |
| - Debug.Log("Throwing an instance of 🐛 CustomException!"); |
| 28 | + Debug.Log("Throwing an exception but catching it! 🐜"); |
29 | 29 |
|
30 | 30 | try
|
31 | 31 | {
|
32 |
| - throw new CustomException("Custom bugs 🐛🐛🐛🐛."); |
| 32 | + DoSomeWorkHere(); |
33 | 33 | }
|
34 | 34 | catch (Exception e)
|
35 | 35 | {
|
36 | 36 | SentrySdk.CaptureException(e);
|
37 | 37 | }
|
38 | 38 | }
|
39 | 39 |
|
40 |
| - public void ThrowNullAndCatch() |
| 40 | + private void DoSomeWorkHere() |
41 | 41 | {
|
42 |
| - Debug.Log("Throwing 'null' and catching 🐜🐜🐜 it!"); |
43 |
| - |
44 |
| - try |
| 42 | + if (CheckSomeFakeWork()) |
45 | 43 | {
|
46 |
| - ThrowNull(); |
47 |
| - } |
48 |
| - catch (Exception e) |
49 |
| - { |
50 |
| - SentrySdk.CaptureException(e); |
| 44 | + DoSomeWorkThere(); |
51 | 45 | }
|
52 | 46 | }
|
53 | 47 |
|
54 |
| - public void CaptureMessage() => SentrySdk.CaptureMessage("🕷️🕷️🕷️ Spider message 🕷️🕷️🕷️🕷️"); |
55 |
| - |
56 |
| - // IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much |
57 |
| - [MethodImpl(MethodImplOptions.NoInlining)] |
58 |
| - private void StackTraceExampleB() |
| 48 | + private void DoSomeWorkThere() |
59 | 49 | {
|
60 |
| - var someWork = DateTime.Now.ToString(); |
61 |
| - if (someWork.Length > 0) // This condition will always be true but compiler can't be certain |
| 50 | + if (CheckSomeFakeWork()) |
62 | 51 | {
|
63 |
| - throw new InvalidOperationException("Exception from a lady beetle 🐞"); |
| 52 | + throw new CustomException("Exception from an exceptional lady beetle 🐞!"); |
64 | 53 | }
|
65 | 54 | }
|
66 | 55 |
|
67 |
| - // IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much |
68 |
| - [MethodImpl(MethodImplOptions.NoInlining)] |
69 |
| - public void StackTraceExampleA() |
| 56 | + public void CaptureMessage() |
70 | 57 | {
|
71 |
| - var someWork = DateTime.Now.ToString(); |
72 |
| - if (someWork.Length > 0) // This condition will always be true but compiler can't be certain |
| 58 | + if (CheckSomeFakeWork()) |
73 | 59 | {
|
74 |
| - StackTraceExampleB(); |
| 60 | + // Messages do not have a stacktrace attached by default. This is an opt-in feature. |
| 61 | + // Note: That stack traces generated for message events are provided without line numbers. See known limitations |
| 62 | + // https://docs.sentry.io/platforms/unity/troubleshooting/known-limitations/#line-numbers-missing-in-events-captured-through-debuglogerror-or-sentrysdkcapturemessage |
| 63 | + SentrySdk.CaptureMessage("🕷️🕷️🕷️ Spider message 🕷️🕷️🕷️🕷️"); |
75 | 64 | }
|
76 | 65 | }
|
77 | 66 |
|
78 |
| - // IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much |
79 |
| - [MethodImpl(MethodImplOptions.NoInlining)] |
80 | 67 | public void LogError()
|
81 | 68 | {
|
82 |
| - var someWork = DateTime.Now.ToString(); |
83 |
| - if (someWork.Length > 0) // This condition will always be true but compiler can't be certain |
| 69 | + if (CheckSomeFakeWork()) |
84 | 70 | {
|
| 71 | + // Error logs get captured as messages and do not have a stacktrace attached by default. This is an opt-in feature. |
| 72 | + // Note: That stack traces generated for message events are provided without line numbers. See known limitations |
| 73 | + // https://docs.sentry.io/platforms/unity/troubleshooting/known-limitations/#line-numbers-missing-in-events-captured-through-debuglogerror-or-sentrysdkcapturemessage |
85 | 74 | Debug.LogError("Debug.LogError() called");
|
86 | 75 | }
|
87 | 76 | }
|
88 |
| -} |
89 | 77 |
|
90 |
| -public class CustomException : Exception |
91 |
| -{ |
92 |
| - public CustomException(string message) : base(message) |
| 78 | + public void LogException() |
| 79 | + { |
| 80 | + if (CheckSomeFakeWork()) |
| 81 | + { |
| 82 | + // Error logs get captured as messages and do not have a stacktrace attached by default. This is an opt-in feature. |
| 83 | + Debug.LogException(new NullReferenceException("Some bugs are harder to catch than others. 🦋")); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // NoInlining ends up being inlined through L2CPP anyway. :( |
| 88 | + // We're checking some fake work here to prevent too aggressive optimization. That way, we can show off some proper |
| 89 | + // stack traces that are closer to real-world bugs and events. |
| 90 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 91 | + private static bool CheckSomeFakeWork() => DateTime.Now.Ticks > 0; // Always true but not optimizable |
| 92 | + |
| 93 | + private class CustomException : Exception |
93 | 94 | {
|
| 95 | + public CustomException(string message) : base(message) |
| 96 | + { } |
94 | 97 | }
|
95 | 98 | }
|
0 commit comments