1+ using System ;
2+ using Akka . Actor ;
3+ using Akka . Configuration ;
4+ using Akka . Event ;
5+ using FluentAssertions ;
6+ using Serilog ;
7+ using Serilog . Events ;
8+ using Xunit ;
9+ using Xunit . Abstractions ;
10+
11+ namespace Akka . Logger . Serilog . Tests
12+ {
13+ public class LogMessageSpecs : TestKit . Xunit2 . TestKit
14+ {
15+ public static readonly Config Config = @"akka.loglevel = DEBUG
16+ akka.loggers=[""Akka.Logger.Serilog.SerilogLogger, Akka.Logger.Serilog""]" ;
17+
18+ private readonly ILoggingAdapter _loggingAdapter ;
19+ private readonly TestSink _sink = new TestSink ( ) ;
20+
21+ public LogMessageSpecs ( ITestOutputHelper helper ) : base ( Config , output : helper )
22+ {
23+ global ::Serilog . Log . Logger = new LoggerConfiguration ( )
24+ . WriteTo . Sink ( _sink )
25+ . MinimumLevel . Debug ( )
26+ . CreateLogger ( ) ;
27+ _loggingAdapter = Sys . Log ;
28+ }
29+
30+ [ Fact ]
31+ public void ShouldLogDebugLevelMessage ( )
32+ {
33+ var context = _loggingAdapter ;
34+
35+ _sink . Clear ( ) ;
36+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
37+
38+ context . Debug ( "hi" ) ;
39+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
40+
41+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
42+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Debug ) ;
43+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi" ) ;
44+ }
45+
46+ [ Fact ]
47+ public void ShouldLogDebugLevelMessageWithArgs ( )
48+ {
49+ var context = _loggingAdapter ;
50+
51+ _sink . Clear ( ) ;
52+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
53+
54+ context . Debug ( "hi {0}" , "test" ) ;
55+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
56+
57+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
58+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Debug ) ;
59+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi \" test\" " ) ;
60+ }
61+
62+ [ Fact ]
63+ public void ShouldLogDebugLevelMessageWithException ( )
64+ {
65+ var context = _loggingAdapter ;
66+
67+ _sink . Clear ( ) ;
68+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
69+
70+ var exception = new Exception ( "BOOM!!!" ) ;
71+ context . Debug ( exception , "hi" ) ;
72+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
73+
74+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
75+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Debug ) ;
76+ logEvent . Exception . Should ( ) . Be ( exception ) ;
77+ }
78+
79+ [ Fact ]
80+ public void ShouldLogDebugLevelMessageWithArgsAndException ( )
81+ {
82+ var context = _loggingAdapter ;
83+
84+ _sink . Clear ( ) ;
85+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
86+
87+ var exception = new Exception ( "BOOM!!!" ) ;
88+ context . Debug ( exception , "hi {0}" , "test" ) ;
89+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
90+
91+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
92+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Debug ) ;
93+ logEvent . Exception . Should ( ) . Be ( exception ) ;
94+ }
95+
96+ [ Fact ]
97+ public void ShouldLogInfoLevelMessage ( )
98+ {
99+ var context = _loggingAdapter ;
100+
101+ _sink . Clear ( ) ;
102+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
103+
104+ context . Info ( "hi" ) ;
105+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
106+
107+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
108+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Information ) ;
109+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi" ) ;
110+ }
111+
112+ [ Fact ]
113+ public void ShouldLogInfoLevelMessageWithArgs ( )
114+ {
115+ var context = _loggingAdapter ;
116+
117+ _sink . Clear ( ) ;
118+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
119+
120+ context . Info ( "hi {0}" , "test" ) ;
121+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
122+
123+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
124+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Information ) ;
125+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi \" test\" " ) ;
126+ }
127+
128+ [ Fact ]
129+ public void ShouldLogInfoLevelMessageWithException ( )
130+ {
131+ var context = _loggingAdapter ;
132+
133+ _sink . Clear ( ) ;
134+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
135+
136+ var exception = new Exception ( "BOOM!!!" ) ;
137+ context . Info ( exception , "hi" ) ;
138+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
139+
140+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
141+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Information ) ;
142+ logEvent . Exception . Should ( ) . Be ( exception ) ;
143+ }
144+
145+ [ Fact ]
146+ public void ShouldLogInfoLevelMessageWithArgsAndException ( )
147+ {
148+ var context = _loggingAdapter ;
149+
150+ _sink . Clear ( ) ;
151+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
152+
153+ var exception = new Exception ( "BOOM!!!" ) ;
154+ context . Info ( exception , "hi {0}" , "test" ) ;
155+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
156+
157+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
158+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Information ) ;
159+ logEvent . Exception . Should ( ) . Be ( exception ) ;
160+ }
161+
162+ [ Fact ]
163+ public void ShouldLogWarningLevelMessage ( )
164+ {
165+ var context = _loggingAdapter ;
166+
167+ _sink . Clear ( ) ;
168+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
169+
170+ context . Warning ( "hi" ) ;
171+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
172+
173+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
174+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Warning ) ;
175+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi" ) ;
176+ }
177+
178+ [ Fact ]
179+ public void ShouldLogWarningLevelMessageWithArgs ( )
180+ {
181+ var context = _loggingAdapter ;
182+
183+ _sink . Clear ( ) ;
184+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
185+
186+ context . Warning ( "hi {0}" , "test" ) ;
187+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
188+
189+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
190+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Warning ) ;
191+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi \" test\" " ) ;
192+ }
193+
194+ [ Fact ]
195+ public void ShouldLogWarningLevelMessageWithException ( )
196+ {
197+ var context = _loggingAdapter ;
198+
199+ _sink . Clear ( ) ;
200+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
201+
202+ var exception = new Exception ( "BOOM!!!" ) ;
203+ context . Warning ( exception , "hi" ) ;
204+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
205+
206+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
207+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Warning ) ;
208+ logEvent . Exception . Should ( ) . Be ( exception ) ;
209+ }
210+
211+ [ Fact ]
212+ public void ShouldLogWarningLevelMessageWithArgsAndException ( )
213+ {
214+ var context = _loggingAdapter ;
215+
216+ _sink . Clear ( ) ;
217+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
218+
219+ var exception = new Exception ( "BOOM!!!" ) ;
220+ context . Warning ( exception , "hi {0}" , "test" ) ;
221+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
222+
223+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
224+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Warning ) ;
225+ logEvent . Exception . Should ( ) . Be ( exception ) ;
226+ }
227+
228+ [ Fact ]
229+ public void ShouldLogErrorLevelMessage ( )
230+ {
231+ var context = _loggingAdapter ;
232+
233+ _sink . Clear ( ) ;
234+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
235+
236+ context . Error ( "hi" ) ;
237+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
238+
239+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
240+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Error ) ;
241+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi" ) ;
242+ }
243+
244+ [ Fact ]
245+ public void ShouldLogErrorLevelMessageWithArgs ( )
246+ {
247+ var context = _loggingAdapter ;
248+
249+ _sink . Clear ( ) ;
250+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
251+
252+ context . Error ( "hi {0}" , "test" ) ;
253+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
254+
255+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
256+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Error ) ;
257+ logEvent . RenderMessage ( ) . Should ( ) . Contain ( "hi \" test\" " ) ;
258+ }
259+
260+ [ Fact ]
261+ public void ShouldLogErrorLevelMessageWithException ( )
262+ {
263+ var context = _loggingAdapter ;
264+
265+ _sink . Clear ( ) ;
266+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
267+
268+ var exception = new Exception ( "BOOM!!!" ) ;
269+ context . Error ( exception , "hi" ) ;
270+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
271+
272+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
273+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Error ) ;
274+ logEvent . Exception . Should ( ) . Be ( exception ) ;
275+ }
276+
277+ [ Fact ]
278+ public void ShouldLogErrorLevelMessageWithArgsAndException ( )
279+ {
280+ var context = _loggingAdapter ;
281+
282+ _sink . Clear ( ) ;
283+ AwaitCondition ( ( ) => _sink . Writes . Count == 0 ) ;
284+
285+ var exception = new Exception ( "BOOM!!!" ) ;
286+ context . Error ( exception , "hi {0}" , "test" ) ;
287+ AwaitCondition ( ( ) => _sink . Writes . Count == 1 ) ;
288+
289+ _sink . Writes . TryDequeue ( out var logEvent ) . Should ( ) . BeTrue ( ) ;
290+ logEvent . Level . Should ( ) . Be ( LogEventLevel . Error ) ;
291+ logEvent . Exception . Should ( ) . Be ( exception ) ;
292+ }
293+ }
294+ }
0 commit comments