@@ -8,7 +8,6 @@ public class ConsoleWrapper : IConsoleWrapper
8
8
{
9
9
private static bool _override ;
10
10
private static TextWriter _testOutputStream ;
11
- private static bool _outputResetPerformed = false ;
12
11
private static bool _inTestMode = false ;
13
12
14
13
/// <inheritdoc />
@@ -20,7 +19,7 @@ public void WriteLine(string message)
20
19
}
21
20
else
22
21
{
23
- EnsureConsoleOutputOnce ( ) ;
22
+ EnsureConsoleOutput ( ) ;
24
23
Console . WriteLine ( message ) ;
25
24
}
26
25
}
@@ -34,7 +33,7 @@ public void Debug(string message)
34
33
}
35
34
else
36
35
{
37
- EnsureConsoleOutputOnce ( ) ;
36
+ EnsureConsoleOutput ( ) ;
38
37
System . Diagnostics . Debug . WriteLine ( message ) ;
39
38
}
40
39
}
@@ -50,9 +49,9 @@ public void Error(string message)
50
49
{
51
50
if ( ! _override )
52
51
{
53
- var errordOutput = new StreamWriter ( Console . OpenStandardError ( ) ) ;
54
- errordOutput . AutoFlush = true ;
55
- Console . SetError ( errordOutput ) ;
52
+ var errorOutput = new StreamWriter ( Console . OpenStandardError ( ) ) ;
53
+ errorOutput . AutoFlush = true ;
54
+ Console . SetError ( errorOutput ) ;
56
55
}
57
56
Console . Error . WriteLine ( message ) ;
58
57
}
@@ -70,23 +69,69 @@ public static void SetOut(TextWriter consoleOut)
70
69
Console . SetOut ( consoleOut ) ;
71
70
}
72
71
73
- private static void EnsureConsoleOutputOnce ( )
72
+ private static void EnsureConsoleOutput ( )
74
73
{
75
- if ( _outputResetPerformed ) return ;
76
- OverrideLambdaLogger ( ) ;
77
- _outputResetPerformed = true ;
74
+ // Check if we need to override console output for Lambda environment
75
+ if ( ShouldOverrideConsole ( ) )
76
+ {
77
+ OverrideLambdaLogger ( ) ;
78
+ }
79
+ }
80
+
81
+ private static bool ShouldOverrideConsole ( )
82
+ {
83
+ // Don't override if we're in test mode
84
+ if ( _inTestMode ) return false ;
85
+
86
+ // Always override in Lambda environment to prevent Lambda's log wrapping
87
+ var isLambda = ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "AWS_LAMBDA_FUNCTION_NAME" ) ) ;
88
+
89
+ return isLambda && ( ! _override || HasLambdaReInterceptedConsole ( ) ) ;
90
+ }
91
+
92
+ internal static bool HasLambdaReInterceptedConsole ( )
93
+ {
94
+ return HasLambdaReInterceptedConsole ( ( ) => Console . Out ) ;
78
95
}
79
96
80
- private static void OverrideLambdaLogger ( )
97
+ internal static bool HasLambdaReInterceptedConsole ( Func < TextWriter > consoleOutAccessor )
81
98
{
82
- if ( _override )
99
+ // Lambda might re-intercept console between init and handler execution
100
+ try
101
+ {
102
+ var currentOut = consoleOutAccessor ( ) ;
103
+ // Check if current output stream looks like it might be Lambda's wrapper
104
+ var typeName = currentOut . GetType ( ) . FullName ?? "" ;
105
+ return typeName . Contains ( "Lambda" ) || typeName == "System.IO.TextWriter+SyncTextWriter" ;
106
+ }
107
+ catch
108
+ {
109
+ return true ; // Assume re-interception if we can't determine
110
+ }
111
+ }
112
+
113
+ internal static void OverrideLambdaLogger ( )
114
+ {
115
+ OverrideLambdaLogger ( ( ) => Console . OpenStandardOutput ( ) ) ;
116
+ }
117
+
118
+ internal static void OverrideLambdaLogger ( Func < Stream > standardOutputOpener )
119
+ {
120
+ try
121
+ {
122
+ // Force override of LambdaLogger
123
+ var standardOutput = new StreamWriter ( standardOutputOpener ( ) )
124
+ {
125
+ AutoFlush = true
126
+ } ;
127
+ Console . SetOut ( standardOutput ) ;
128
+ _override = true ;
129
+ }
130
+ catch ( Exception )
83
131
{
84
- return ;
132
+ // Log the failure but don't throw - degraded functionality is better than crash
133
+ _override = false ;
85
134
}
86
- // Force override of LambdaLogger
87
- var standardOutput = new StreamWriter ( Console . OpenStandardOutput ( ) ) ;
88
- standardOutput . AutoFlush = true ;
89
- Console . SetOut ( standardOutput ) ;
90
135
}
91
136
92
137
internal static void WriteLine ( string logLevel , string message )
@@ -102,14 +147,14 @@ public static void ResetForTest()
102
147
_override = false ;
103
148
_inTestMode = false ;
104
149
_testOutputStream = null ;
105
- _outputResetPerformed = false ;
106
150
}
107
151
108
152
/// <summary>
109
153
/// Clear the output reset flag
110
154
/// </summary>
111
155
public static void ClearOutputResetFlag ( )
112
156
{
113
- _outputResetPerformed = false ;
157
+ // This method is kept for backward compatibility but no longer needed
158
+ // since we removed the _outputResetPerformed flag
114
159
}
115
160
}
0 commit comments