1
+ // Copyright © WireMock.Net
2
+
3
+ using System ;
4
+ using Newtonsoft . Json ;
5
+ using Stef . Validation ;
6
+ using TUnit . Core . Logging ;
7
+ using WireMock . Admin . Requests ;
8
+ using WireMock . Logging ;
9
+
10
+ namespace WireMock . Net . TUnit ;
11
+
12
+ /// <summary>
13
+ /// When using TUnit, this class enables to log the output from WireMock.Net to the <see cref="TUnitLogger"/>.
14
+ /// </summary>
15
+ // ReSharper disable once InconsistentNaming
16
+ public sealed class TUnitWireMockLogger : IWireMockLogger
17
+ {
18
+ private readonly TUnitLogger _tUnitLogger ;
19
+
20
+ /// <summary>
21
+ /// Create a new instance on the <see cref="TUnitWireMockLogger"/>.
22
+ /// </summary>
23
+ /// <param name="tUnitLogger">Represents a class which can be used to provide test output.</param>
24
+ public TUnitWireMockLogger ( TUnitLogger tUnitLogger )
25
+ {
26
+ _tUnitLogger = Guard . NotNull ( tUnitLogger ) ;
27
+ }
28
+
29
+ /// <inheritdoc />
30
+ public void Debug ( string formatString , params object [ ] args )
31
+ {
32
+ _tUnitLogger . LogDebug ( Format ( "Debug" , formatString , args ) ) ;
33
+ }
34
+
35
+ /// <inheritdoc />
36
+ public void Info ( string formatString , params object [ ] args )
37
+ {
38
+ _tUnitLogger . LogInformation ( Format ( "Info" , formatString , args ) ) ;
39
+ }
40
+
41
+ /// <inheritdoc />
42
+ public void Warn ( string formatString , params object [ ] args )
43
+ {
44
+ _tUnitLogger . LogWarning ( Format ( "Warning" , formatString , args ) ) ;
45
+ }
46
+
47
+ /// <inheritdoc />
48
+ public void Error ( string formatString , params object [ ] args )
49
+ {
50
+ _tUnitLogger . LogError ( Format ( "Error" , formatString , args ) ) ;
51
+ }
52
+
53
+ /// <inheritdoc />
54
+ public void Error ( string formatString , Exception exception )
55
+ {
56
+ _tUnitLogger . LogError ( Format ( "Error" , formatString , exception . Message ) , exception ) ;
57
+
58
+ if ( exception is AggregateException ae )
59
+ {
60
+ ae . Handle ( ex =>
61
+ {
62
+ _tUnitLogger . LogError ( Format ( "Error" , "Exception {0}" , ex . Message ) , exception ) ;
63
+ return true ;
64
+ } ) ;
65
+ }
66
+ }
67
+
68
+ /// <inheritdoc />
69
+ public void DebugRequestResponse ( LogEntryModel logEntryModel , bool isAdminRequest )
70
+ {
71
+ var message = JsonConvert . SerializeObject ( logEntryModel , Formatting . Indented ) ;
72
+ _tUnitLogger . LogDebug ( Format ( "DebugRequestResponse" , "Admin[{0}] {1}" , isAdminRequest , message ) ) ;
73
+ }
74
+
75
+ private static string Format ( string level , string formatString , params object [ ] args )
76
+ {
77
+ Guard . NotNull ( formatString ) ;
78
+
79
+ var message = args . Length > 0 ? string . Format ( formatString , args ) : formatString ;
80
+ return $ "{ DateTime . UtcNow } [{ level } ] : { message } ";
81
+ }
82
+ }
0 commit comments