11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . IO ;
45using System . Threading ;
@@ -31,10 +32,9 @@ public enum LogOutput
3132 { LogLevel . Disabled , "DISABLED" }
3233 } ;
3334
34- private readonly Queue < ( LogLevel , string ) > _logQueue = new Queue < ( LogLevel , string ) > ( ) ;
35+ private readonly ConcurrentQueue < ( LogLevel , string ) > _logQueue = new ConcurrentQueue < ( LogLevel , string ) > ( ) ;
3536
3637 private readonly object _logLock = new object ( ) ;
37- private readonly object _queueLock = new object ( ) ;
3838
3939 private StreamWriter _outputFile = null ;
4040
@@ -118,10 +118,7 @@ private void Log(LogLevel level, string text)
118118 return ;
119119 }
120120
121- lock ( _queueLock )
122- {
123- _logQueue . Enqueue ( ( level , text ) ) ;
124- }
121+ _logQueue . Enqueue ( ( level , text ) ) ;
125122
126123 if ( ! _isLogging )
127124 {
@@ -142,51 +139,45 @@ private void LogQueue()
142139 {
143140 try
144141 {
145- var data = ( LogLevel . Disabled , "" ) ;
146-
147- lock ( _queueLock )
142+ while ( _logQueue . TryDequeue ( out var data ) )
148143 {
149- if ( _logQueue . Count > 0 )
144+ var ( level , text ) = data ;
145+ if ( level == LogLevel . Disabled || string . IsNullOrEmpty ( text ) )
150146 {
151- data = _logQueue . Dequeue ( ) ;
147+ break ;
152148 }
153- }
154-
155- var ( level , text ) = data ;
156- if ( level == LogLevel . Disabled || string . IsNullOrEmpty ( text ) )
157- {
158- _isLogging = false ;
159- return ;
160- }
161149
162- text = $ "{ _logLevelMap [ level ] } { text } ";
150+ text = $ "{ _logLevelMap [ level ] } { text } ";
163151
164- lock ( _logLock )
165- {
166- if ( level == LogLevel . Error )
152+ lock ( _logLock )
167153 {
168- Console . WriteLine ( text ) ;
169- if ( _output == LogOutput . File && _outputFile != null )
154+ if ( level == LogLevel . Error )
155+ {
156+ Console . WriteLine ( text ) ;
157+ if ( _output == LogOutput . File && _outputFile != null )
158+ {
159+ _outputFile . WriteLine ( text ) ;
160+ }
161+ }
162+ else if ( _output == LogOutput . Console )
163+ {
164+ Console . WriteLine ( text ) ;
165+ }
166+ else if ( _output == LogOutput . File )
170167 {
171168 _outputFile . WriteLine ( text ) ;
172169 }
173170 }
174- else if ( _output == LogOutput . Console )
175- {
176- Console . WriteLine ( text ) ;
177- }
178- else if ( _output == LogOutput . File )
179- {
180- _outputFile . WriteLine ( text ) ;
181- }
182171 }
183-
184- LogQueue ( ) ;
185172 }
186173 catch ( Exception ex )
187174 {
188175 Console . WriteLine ( ex ) ;
189176 }
177+ finally
178+ {
179+ _isLogging = false ;
180+ }
190181 }
191182
192183 public void Debug ( string text )
0 commit comments