1- namespace WeihanLi . Common . Helpers ;
1+ // Copyright (c) Weihan Li. All rights reserved.
2+ // Licensed under the Apache license.
3+
4+ namespace WeihanLi . Common . Helpers ;
25
36public sealed class ConsoleOutput : IDisposable
47{
@@ -7,62 +10,32 @@ public sealed class ConsoleOutput : IDisposable
710 private readonly StringWriter _outputWriter = new ( ) ;
811 private readonly StringWriter _errorWriter = new ( ) ;
912
10- private const int NOT_DISPOSED = 0 ;
11- private const int DISPOSED = 1 ;
13+ private const int NotDisposed = 0 ;
14+ private const int Disposed = 1 ;
1215
13- private int _alreadyDisposed = NOT_DISPOSED ;
16+ private int _alreadyDisposed = NotDisposed ;
1417
15- private static readonly SemaphoreSlim _consoleLock = new ( 1 , 1 ) ;
18+ private static readonly SemaphoreSlim ConsoleLock = new ( 1 , 1 ) ;
1619
1720 private ConsoleOutput ( )
1821 {
1922 }
2023
2124 public static ConsoleOutput Capture ( )
2225 {
23- var outputCapture = new ConsoleOutput ( ) ;
24- _consoleLock . Wait ( ) ;
25-
26- try
27- {
28- outputCapture . _originalOutputWriter = Console . Out ;
29- outputCapture . _originalErrorWriter = Console . Error ;
30-
31- Console . SetOut ( outputCapture . _outputWriter ) ;
32- Console . SetError ( outputCapture . _errorWriter ) ;
33- }
34- catch
35- {
36- _consoleLock . Release ( ) ;
37- throw ;
38- }
39-
40- return outputCapture ;
26+ ConsoleLock . Wait ( ) ;
27+ return GetConsoleOutputInternal ( ) ;
4128 }
4229
4330 public static async Task < ConsoleOutput > CaptureAsync ( )
4431 {
45- var outputCapture = new ConsoleOutput ( ) ;
46- await _consoleLock . WaitAsync ( ) ;
47-
48- try
49- {
50- outputCapture . _originalOutputWriter = Console . Out ;
51- outputCapture . _originalErrorWriter = Console . Error ;
52-
53- Console . SetOut ( outputCapture . _outputWriter ) ;
54- Console . SetError ( outputCapture . _errorWriter ) ;
55- return outputCapture ;
56- }
57- finally
58- {
59- _consoleLock . Release ( ) ;
60- }
32+ await ConsoleLock . WaitAsync ( ) ;
33+ return GetConsoleOutputInternal ( ) ;
6134 }
6235
6336 public void Dispose ( )
6437 {
65- if ( Interlocked . CompareExchange ( ref _alreadyDisposed , DISPOSED , NOT_DISPOSED ) == NOT_DISPOSED )
38+ if ( Interlocked . CompareExchange ( ref _alreadyDisposed , Disposed , NotDisposed ) == NotDisposed )
6639 {
6740 if ( _originalOutputWriter != null )
6841 {
@@ -72,9 +45,9 @@ public void Dispose()
7245 {
7346 Console . SetError ( _originalErrorWriter ) ;
7447 }
75- if ( _consoleLock . CurrentCount < 1 )
48+ if ( ConsoleLock . CurrentCount < 1 )
7649 {
77- _consoleLock . Release ( ) ;
50+ ConsoleLock . Release ( ) ;
7851 }
7952 }
8053 }
@@ -88,4 +61,25 @@ public void Clear()
8861 _outputWriter . GetStringBuilder ( ) . Clear ( ) ;
8962 _errorWriter . GetStringBuilder ( ) . Clear ( ) ;
9063 }
64+
65+ private static ConsoleOutput GetConsoleOutputInternal ( )
66+ {
67+ var outputCapture = new ConsoleOutput ( ) ;
68+ try
69+ {
70+ outputCapture . _originalOutputWriter = Console . Out ;
71+ outputCapture . _originalErrorWriter = Console . Error ;
72+
73+ Console . SetOut ( outputCapture . _outputWriter ) ;
74+ Console . SetError ( outputCapture . _errorWriter ) ;
75+ }
76+ catch
77+ {
78+ ConsoleLock . Release ( ) ;
79+ throw ;
80+ }
81+
82+ return outputCapture ;
83+
84+ }
9185}
0 commit comments