Skip to content

Commit 920f42f

Browse files
authored
Initial work for adding structured logging for .NET Lambda runtime client (#1588)
1 parent 189da37 commit 920f42f

File tree

14 files changed

+1929
-66
lines changed

14 files changed

+1929
-66
lines changed

Libraries/src/Amazon.Lambda.Core/ILambdaLogger.cs

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.Runtime.Versioning;
3+
14
namespace Amazon.Lambda.Core
25
{
36
#if NET6_0_OR_GREATER
@@ -62,7 +65,7 @@ public interface ILambdaLogger
6265
#if NET6_0_OR_GREATER
6366

6467
/// <summary>
65-
/// Log message catagorized by the given log level
68+
/// Log message categorized by the given log level
6669
/// <para>
6770
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
6871
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
@@ -73,7 +76,7 @@ public interface ILambdaLogger
7376
void Log(string level, string message) => LogLine(message);
7477

7578
/// <summary>
76-
/// Log message catagorized by the given log level
79+
/// Log message categorized by the given log level
7780
/// <para>
7881
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
7982
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
@@ -142,6 +145,222 @@ public interface ILambdaLogger
142145
/// </summary>
143146
/// <param name="message"></param>
144147
void LogCritical(string message) => Log(LogLevel.Critical.ToString(), message);
148+
149+
150+
private const string ParameterizedPreviewMessage =
151+
"Parameterized logging is in preview till a new version of .NET Lambda runtime client that supports parameterized logging " +
152+
"has been deployed to the .NET Lambda managed runtime. Till deployment has been made the feature can be used by deploying as an " +
153+
"executable including the latest version of Amazon.Lambda.RuntimeSupport and setting the \"EnablePreviewFeatures\" in the Lambda " +
154+
"project file to \"true\"";
155+
156+
/// <summary>
157+
/// Log message categorized by the given log level
158+
/// <para>
159+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
160+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
161+
/// </para>
162+
/// </summary>
163+
/// <param name="level">Log level of the message.</param>
164+
/// <param name="message">Message to log.</param>
165+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
166+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
167+
void Log(string level, string message, params object[] args) => Log(level, message, args);
168+
169+
/// <summary>
170+
/// Log message categorized by the given log level
171+
/// <para>
172+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
173+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
174+
/// </para>
175+
/// </summary>
176+
/// <param name="level">Log level of the message.</param>
177+
/// <param name="exception">Exception to include with the logging.</param>
178+
/// <param name="message">Message to log.</param>
179+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
180+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
181+
void Log(string level, Exception exception, string message, params object[] args)
182+
{
183+
Log(level, message, args);
184+
Log(level, exception.ToString(), args);
185+
}
186+
187+
/// <summary>
188+
/// Log message categorized by the given log level
189+
/// <para>
190+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
191+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
192+
/// </para>
193+
/// </summary>
194+
/// <param name="level">Log level of the message.</param>
195+
/// <param name="message">Message to log.</param>
196+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
197+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
198+
void Log(LogLevel level, string message, params object[] args) => Log(level.ToString(), message, args);
199+
200+
/// <summary>
201+
/// Log message categorized by the given log level
202+
/// <para>
203+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
204+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
205+
/// </para>
206+
/// </summary>
207+
/// <param name="level">Log level of the message.</param>
208+
/// <param name="exception">Exception to include with the logging.</param>
209+
/// <param name="message">Message to log.</param>
210+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
211+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
212+
void Log(LogLevel level, Exception exception, string message, params object[] args) => Log(level.ToString(), exception, message, args);
213+
214+
/// <summary>
215+
/// Log trace message.
216+
/// <para>
217+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
218+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
219+
/// </para>
220+
/// </summary>
221+
/// <param name="message">Message to log.</param>
222+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
223+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
224+
void LogTrace(string message, params object[] args) => Log(LogLevel.Trace.ToString(), message, args);
225+
226+
/// <summary>
227+
/// Log trace message.
228+
/// <para>
229+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
230+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
231+
/// </para>
232+
/// </summary>
233+
/// <param name="exception">Exception to include with the logging.</param>
234+
/// <param name="message">Message to log.</param>
235+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
236+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
237+
void LogTrace(Exception exception, string message, params object[] args) => Log(LogLevel.Trace.ToString(), exception, message, args);
238+
239+
/// <summary>
240+
/// Log debug message.
241+
/// <para>
242+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
243+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
244+
/// </para>
245+
/// </summary>
246+
/// <param name="message">Message to log.</param>
247+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
248+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
249+
void LogDebug(string message, params object[] args) => Log(LogLevel.Debug.ToString(), message, args);
250+
251+
/// <summary>
252+
/// Log debug message.
253+
/// <para>
254+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
255+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
256+
/// </para>
257+
/// </summary>
258+
/// <param name="exception">Exception to include with the logging.</param>
259+
/// <param name="message">Message to log.</param>
260+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
261+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
262+
void LogDebug(Exception exception, string message, params object[] args) => Log(LogLevel.Debug.ToString(), exception, message, args);
263+
264+
/// <summary>
265+
/// Log information message.
266+
/// <para>
267+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
268+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
269+
/// </para>
270+
/// </summary>
271+
/// <param name="message">Message to log.</param>
272+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
273+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
274+
void LogInformation(string message, params object[] args) => Log(LogLevel.Information.ToString(), message, args);
275+
276+
/// <summary>
277+
/// Log information message.
278+
/// <para>
279+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
280+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
281+
/// </para>
282+
/// </summary>
283+
/// <param name="exception">Exception to include with the logging.</param>
284+
/// <param name="message">Message to log.</param>
285+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
286+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
287+
void LogInformation(Exception exception, string message, params object[] args) => Log(LogLevel.Information.ToString(), exception, message, args);
288+
289+
/// <summary>
290+
/// Log warning message.
291+
/// <para>
292+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
293+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
294+
/// </para>
295+
/// </summary>
296+
/// <param name="message">Message to log.</param>
297+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
298+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
299+
void LogWarning(string message, params object[] args) => Log(LogLevel.Warning.ToString(), message, args);
300+
301+
/// <summary>
302+
/// Log warning message.
303+
/// <para>
304+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
305+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
306+
/// </para>
307+
/// </summary>
308+
/// <param name="exception">Exception to include with the logging.</param>
309+
/// <param name="message">Message to log.</param>
310+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
311+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
312+
void LogWarning(Exception exception, string message, params object[] args) => Log(LogLevel.Warning.ToString(), exception, message, args);
313+
314+
/// <summary>
315+
/// Log error message.
316+
/// <para>
317+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
318+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
319+
/// </para>
320+
/// </summary>
321+
/// <param name="message">Message to log.</param>
322+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
323+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
324+
void LogError(string message, params object[] args) => Log(LogLevel.Error.ToString(), message, args);
325+
326+
/// <summary>
327+
/// Log error message.
328+
/// <para>
329+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
330+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
331+
/// </para>
332+
/// </summary>
333+
/// <param name="exception">Exception to include with the logging.</param>
334+
/// <param name="message">Message to log.</param>
335+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
336+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
337+
void LogError(Exception exception, string message, params object[] args) => Log(LogLevel.Error.ToString(), exception, message, args);
338+
339+
/// <summary>
340+
/// Log critical message.
341+
/// <para>
342+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
343+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
344+
/// </para>
345+
/// </summary>
346+
/// <param name="message">Message to log.</param>
347+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
348+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
349+
void LogCritical(string message, params object[] args) => Log(LogLevel.Critical.ToString(), message, args);
350+
351+
/// <summary>
352+
/// Log critical message.
353+
/// <para>
354+
/// To configure the minimum log level set the AWS_LAMBDA_HANDLER_LOG_LEVEL environment variable. The value should be set
355+
/// to one of the values in the LogLevel enumeration. The default minimum log level is "Information".
356+
/// </para>
357+
/// </summary>
358+
/// <param name="exception">Exception to include with the logging.</param>
359+
/// <param name="message">Message to log.</param>
360+
/// <param name="args">Values to be replaced in log messages that are parameterized.</param>
361+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
362+
void LogCritical(Exception exception, string message, params object[] args) => Log(LogLevel.Critical.ToString(), exception, message, args);
363+
145364
#endif
146365

147366
}

Libraries/src/Amazon.Lambda.RuntimeSupport/Amazon.Lambda.RuntimeSupport.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
1616
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
17+
<LangVersion>9.0</LangVersion>
1718
</PropertyGroup>
1819

1920
<PropertyGroup Condition=" '$(ExecutableOutputType)'=='true' ">

Libraries/src/Amazon.Lambda.RuntimeSupport/Context/LambdaConsoleLogger.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Amazon.Lambda.Core;
1616
using Amazon.Lambda.RuntimeSupport.Helpers;
1717
using System;
18+
using System.Runtime.Versioning;
1819

1920
namespace Amazon.Lambda.RuntimeSupport
2021
{
@@ -44,6 +45,24 @@ public void Log(string level, string message)
4445
{
4546
_consoleLoggerRedirector.FormattedWriteLine(level, message);
4647
}
48+
49+
private const string ParameterizedPreviewMessage =
50+
"Parameterized logging is in preview till a new version of .NET Lambda runtime client that supports parameterized logging " +
51+
"has been deployed to the .NET Lambda managed runtime. Till deployment has been made the feature can be used by deploying as an " +
52+
"executable including the latest version of Amazon.Lambda.RuntimeSupport and setting the \"LangVersion\" in the Lambda " +
53+
"project file to \"preview\"";
54+
55+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
56+
public void Log(string level, string message, params object[] args)
57+
{
58+
_consoleLoggerRedirector.FormattedWriteLine(level, message, args);
59+
}
60+
61+
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
62+
public void Log(string level, Exception exception, string message, params object[] args)
63+
{
64+
_consoleLoggerRedirector.FormattedWriteLine(level, exception, message, args);
65+
}
4766
#endif
4867
}
4968
}

0 commit comments

Comments
 (0)