|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Verify Logs - Eceptions |
| 4 | +--- |
| 5 | + |
| 6 | +# Verifying Exeptions |
| 7 | + |
| 8 | +There are three settings: |
| 9 | +* `None`: Does not verify any exceptions. |
| 10 | +* `Message`: Verifies the exception message. This can cause brittle tests as the message may contain nondeterministic information. |
| 11 | +* `Type`: Verifies the exception type. |
| 12 | +* `StackTrace`: Verifies the stack trace of the exception. This can cause brittle tests as code moves around, is refactored, packages updated, etc. |
| 13 | +* `IncludeInnerExceptions`: Recursively verifies the inner exceptions. |
| 14 | + |
| 15 | +Each of these settings are flags and can be joined together like the example below. |
| 16 | + |
| 17 | +The default value is `Type` and `IncludeInnerExceptions`. |
| 18 | + |
| 19 | +```csharp |
| 20 | +public async Task VerifyWarningLogsAreEmittedCorrectlyTestAsync() |
| 21 | +{ |
| 22 | + // ... Code that produces logs |
| 23 | + |
| 24 | + VerifySettings verifySettings = new VerifySettings() |
| 25 | + .AddCapturedLogs(new LoggingCaptureVerifySettings() |
| 26 | + { |
| 27 | + Exception = ExceptionSetting.Message | ExceptionSetting.Type | ExceptionSetting.IncludeInnerExceptions, |
| 28 | + }); |
| 29 | + |
| 30 | + await Verifier.Verify(logs, verifySettings); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Example output |
| 35 | + |
| 36 | +### None |
| 37 | + |
| 38 | +``` |
| 39 | +[ |
| 40 | + { |
| 41 | + Sequence: 0, |
| 42 | + LogLevel: Error, |
| 43 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 44 | + MessageTemplate: Am error happened that resulted in an exception being thrown. |
| 45 | + } |
| 46 | +] |
| 47 | +``` |
| 48 | + |
| 49 | +### Message |
| 50 | + |
| 51 | +``` |
| 52 | +[ |
| 53 | + { |
| 54 | + Sequence: 0, |
| 55 | + LogLevel: Error, |
| 56 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 57 | + MessageTemplate: Am error happened that resulted in an exception being thrown., |
| 58 | + Exception: { |
| 59 | + Message: This is the application exception, it contains an inner exception. |
| 60 | + } |
| 61 | + } |
| 62 | +] |
| 63 | +``` |
| 64 | + |
| 65 | +### Type |
| 66 | + |
| 67 | +``` |
| 68 | +[ |
| 69 | + { |
| 70 | + Sequence: 0, |
| 71 | + LogLevel: Error, |
| 72 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 73 | + MessageTemplate: An error happened that resulted in an exception being thrown., |
| 74 | + Exception: { |
| 75 | + Type: System.ApplicationException |
| 76 | + } |
| 77 | + } |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +### StackTrace |
| 82 | + |
| 83 | +``` |
| 84 | +[ |
| 85 | + { |
| 86 | + Sequence: 0, |
| 87 | + LogLevel: Error, |
| 88 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 89 | + MessageTemplate: An error happened that resulted in an exception being thrown., |
| 90 | + Exception: { |
| 91 | + StackTrace: |
| 92 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.DoTheThing() in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 60 |
| 93 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.VerifyMessageSettingAsync(ExceptionSetting setting) in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 33 |
| 94 | + } |
| 95 | + } |
| 96 | +] |
| 97 | +``` |
| 98 | + |
| 99 | +Note here that Verify has detected the project directory in the output and replaced it with `{ProjectDirectoy}` so that it doesn't break on other team members' machines or on build servers. |
| 100 | + |
| 101 | +### IncludeInnerExceptions |
| 102 | + |
| 103 | +``` |
| 104 | +[ |
| 105 | + { |
| 106 | + Sequence: 0, |
| 107 | + LogLevel: Error, |
| 108 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 109 | + MessageTemplate: An error happened that resulted in an exception being thrown., |
| 110 | + Exception: { |
| 111 | + InnerException: {} |
| 112 | + } |
| 113 | + } |
| 114 | +] |
| 115 | +``` |
| 116 | + |
| 117 | +This setting on its own isn't very useful. |
| 118 | + |
| 119 | +### All: Type, Message, StackTrace, IncludeInnerExceptions |
| 120 | + |
| 121 | +``` |
| 122 | +[ |
| 123 | + { |
| 124 | + Sequence: 0, |
| 125 | + LogLevel: Error, |
| 126 | + CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests, |
| 127 | + MessageTemplate: An error happened that resulted in an exception being thrown., |
| 128 | + Exception: { |
| 129 | + Type: System.ApplicationException, |
| 130 | + Message: This is the application exception, it contains an inner exception., |
| 131 | + StackTrace: |
| 132 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.DoTheThing() in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 61 |
| 133 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.VerifyMessageSettingAsync(ExceptionSetting setting) in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 34, |
| 134 | + InnerException: { |
| 135 | + Type: System.InvalidOperationException, |
| 136 | + Message: Boo! You performed an invalid operation., |
| 137 | + StackTrace: |
| 138 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.PerformThePartOfTheTaskThatActuallyBreaks() in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 68 |
| 139 | + at Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyExceptionSettingTests.DoTheThing() in {ProjectDirectory}Verify/VerifyExceptionSettingTests.cs:line 57 |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +] |
| 144 | +``` |
0 commit comments