Skip to content

Commit 53d194f

Browse files
(#140) WIP
1 parent 4999405 commit 53d194f

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed

src/Stravaig.Extensions.Logging.Diagnostics.Tests/Verify/VerifyDefaultSettingsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class VerifyDefaultSettingsTests
1616
[Test]
1717
public async Task TestExplicitDefaultAsync()
1818
{
19-
var settings = new VerifySettings().AddStravaigTests();
19+
var settings = new VerifySettings()
20+
.AddStravaigTests();
2021
var logs = GetLogEntries();
2122
await Verifier.Verify(logs, settings);
2223
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyLogExceptionTests,
6+
FormattedMessage: An Exception was thrown.,
7+
Exception: {
8+
Message: Current depth is 5.,
9+
Type: System.ApplicationException,
10+
InnerException: {
11+
Message: Current depth is 4.,
12+
Type: System.ApplicationException,
13+
InnerException: {
14+
Message: Current depth is 3.,
15+
Type: System.ApplicationException,
16+
InnerException: {
17+
Message: Current depth is 2.,
18+
Type: System.ApplicationException,
19+
InnerException: {
20+
Message: Current depth is 1.,
21+
Type: System.ApplicationException,
22+
InnerException: {
23+
Message: This method throws exceptions!,
24+
Type: System.InvalidOperationException
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}
32+
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using NUnit.Framework;
5+
using Stravaig.Extensions.Logging.Diagnostics.Verify;
6+
using VerifyNUnit;
7+
using VerifyTests;
8+
9+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests.Verify;
10+
11+
[TestFixture]
12+
public class VerifyLogExceptionTests
13+
{
14+
[Test]
15+
public async Task VerifyInnerExceptionsAsync()
16+
{
17+
var logger = new TestCaptureLogger<VerifyLogExceptionTests>();
18+
try
19+
{
20+
ThrowAnExceptionWithInnerExceptions(5);
21+
}
22+
catch (Exception ex)
23+
{
24+
logger.LogInformation(ex, "An Exception was thrown.");
25+
}
26+
27+
var logs = logger.GetLogs();
28+
29+
VerifySettings verifySettings = new VerifySettings()
30+
.AddStravaigTests(Settings.Default | Settings.StackTrace);
31+
await Verifier.Verify(logs, verifySettings);
32+
}
33+
34+
private void ThrowAnExceptionWithInnerExceptions(int depth = 1)
35+
{
36+
try
37+
{
38+
if (depth > 0)
39+
ThrowAnExceptionWithInnerExceptions(depth - 1);
40+
}
41+
catch (Exception ex)
42+
{
43+
throw new ApplicationException($"Current depth is {depth}.", ex);
44+
}
45+
46+
throw new InvalidOperationException($"This method throws exceptions!");
47+
}
48+
}

src/Stravaig.Extensions.Logging.Diagnostics.Verify/LogEntryConverter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private class Context
3434
internal bool IsWritingExceptionMessage => Using(Settings.ExceptionMessage);
3535
internal bool IsWritingExceptionType => Using(Settings.ExceptionType);
3636
internal bool IsWritingInnerException => Using(Settings.InnerException);
37+
internal bool IsWritingStackTrace => Using(Settings.StackTrace);
3738

3839
internal int CurrentSequence(LogEntry logEntry) =>
3940
Using(Settings.KeepSequenceCadence)
@@ -115,6 +116,12 @@ private static void WriteException(Context ctx, Exception ex)
115116
WriteException(ctx, ex.InnerException);
116117
}
117118

119+
if (ctx.IsWritingStackTrace)
120+
{
121+
ctx.Writer.WritePropertyName(nameof(ex.StackTrace));
122+
ctx.Writer.WriteValue(ex.StackTrace);
123+
}
124+
118125
ctx.Writer.WriteEndObject();
119126
}
120127

src/Stravaig.Extensions.Logging.Diagnostics.Verify/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public enum Settings
6161
/// </summary>
6262
InnerException = 0x0000_0100,
6363

64+
StackTrace = 0x0000_0200,
65+
6466
/// <summary>
6567
/// Indicates the default settings are to be used.
6668
/// </summary>

src/Stravaig.Extensions.Logging.Diagnostics.Verify/VerifySettingsExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
44

55
public static class VerifySettingsExtensions
66
{
7-
public static VerifySettings AddStravaigTests(this VerifySettings verifySettings)
7+
/// <summary>
8+
/// Adds the settings required for verifying captured log entries using Stravaig Logging Diagnostics.
9+
/// </summary>
10+
/// <param name="verifySettings">The verify settings object to modify.</param>
11+
/// <param name="settings">The settings for verifying the tests.</param>
12+
/// <returns>The verify Settings object.</returns>
13+
public static VerifySettings AddStravaigTests(this VerifySettings verifySettings, Settings settings = Settings.Default)
814
{
915
verifySettings.AddExtraSettings(jsonSettings =>
1016
{
11-
jsonSettings.Converters.Add(new LogEntryConverter());
17+
// Remove the default version if it exists.
18+
jsonSettings.Converters.RemoveAll(c => c is LogEntryConverter);
19+
20+
// Create replacement converter.
21+
var logEntryConverter = new LogEntryConverter(settings);
22+
jsonSettings.Converters.Add(logEntryConverter);
1223
});
1324
return verifySettings;
1425
}

0 commit comments

Comments
 (0)