Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .autover/changes/84836f7b-8e69-44a1-87dc-22971f426990.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.RuntimeSupport",
"Type": "Patch",
"ChangelogMessages": [
"Fix NullReferenceException when logging a null logging message with structured logging enabled."
]
},
{
"Name": "Amazon.Lambda.Logging.AspNetCore",
"Type": "Patch",
"ChangelogMessages": [
"Fix NullReferenceException when logging a null logging message with structured logging enabled."
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}
}

if (messageTemplate == null)
{
messageTemplate = formatter.Invoke(state, exception);
}

Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, exception, messageTemplate, parameters.ToArray());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ internal void FormattedWriteLine(string level, Exception exeception, string mess

var messageState = new MessageState();

messageState.MessageTemplate = messageTemplate;
messageState.MessageTemplate = messageTemplate ?? string.Empty;
messageState.MessageArguments = args;
messageState.TimeStamp = DateTime.UtcNow;
messageState.AwsRequestId = CurrentAwsRequestId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -285,4 +285,4 @@ public bool UsingPositionalArguments(IReadOnlyList<MessageProperty> messagePrope
}
}
}
#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
using System;
using System.Buffers;
using System.Collections;
Expand Down Expand Up @@ -48,15 +48,15 @@ public override string FormatMessage(MessageState state)
if (state.MessageArguments?.Length == 0)
{
messageProperties = _emptyMessageProperties;
message = state.MessageTemplate;
message = state.MessageTemplate ?? string.Empty;
}
else
{
// Parse the message template for any message properties like "{count}".
messageProperties = ParseProperties(state.MessageTemplate);
messageProperties = ParseProperties(state.MessageTemplate ?? string.Empty);

// Replace any message properties in the message template with the provided argument values.
message = ApplyMessageProperties(state.MessageTemplate, messageProperties, state.MessageArguments);
message = ApplyMessageProperties(state.MessageTemplate ?? string.Empty, messageProperties, state.MessageArguments);
}


Expand Down Expand Up @@ -310,4 +310,4 @@ private void FormatJsonValue(Utf8JsonWriter writer, object value, string formatA
private static string ToInvariantString(object obj) => Convert.ToString(obj, CultureInfo.InvariantCulture);
}
}
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Xunit;

namespace Amazon.Lambda.Tests
Expand Down Expand Up @@ -581,6 +582,46 @@ public void TestJSONParameterLogging()

}

[Fact]
public void JsonLoggingWithNoOriginalFormat()
{
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT", "JSON");
try
{
using (var writer = new StringWriter())
{
ConnectLoggingActionToLogger(message => writer.Write(message));

var configuration = new ConfigurationBuilder()
.AddJsonFile(GetAppSettingsPath("appsettings.json"))
.Build();

var loggerOptions = new LambdaLoggerOptions(configuration);
var loggerFactory = new TestLoggerFactory()
.AddLambdaLogger(loggerOptions);

var logger = loggerFactory.CreateLogger("JSONLogging");

logger.Log(LogLevel.Error, new EventId(1), new Dictionary<string, object>() { { "Param1", "Value1" } }, null, (state, e) =>
{
var sb = new StringBuilder();
foreach(var kvp in state)
{
sb.AppendFormat("{0}:{1}\n", kvp.Key, kvp.Value);
}
return sb.ToString();
});

var text = writer.ToString();
Assert.Contains("Param1:Value1", text);
}
}
finally
{
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT", null);
}
}

private static string GetAppSettingsPath(string fileName)
{
return Path.Combine(APPSETTINGS_DIR, fileName);
Expand Down Expand Up @@ -609,7 +650,7 @@ private static void ConnectLoggingActionToLogger(Action<string> loggingAction)
loggingWithLevelActionField.SetValue(null, loggingWithLevelAction);

Action<string, Exception, string, object[]> loggingWithExceptionLevelAction = (level, exception, message, parameters) => {
var formattedMessage = $"{level}: {message}: parameter count: {parameters?.Length}\n{exception.Message}";
var formattedMessage = $"{level}: {message}: parameter count: {parameters?.Length}\n{exception?.Message}";
loggingAction(formattedMessage);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Amazon.Lambda.RuntimeSupport.Helpers;
using Amazon.Lambda.RuntimeSupport.Helpers;
using Amazon.Lambda.RuntimeSupport.Helpers.Logging;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using System;
Expand Down Expand Up @@ -103,6 +103,41 @@ public void FormatJsonWithStringMessageProperties()
Assert.Equal("Hello AWS", doc.RootElement.GetProperty("message").GetString());
}

[Fact]
public void FormatJsonNullMessageTemplate()
{
var timestamp = DateTime.UtcNow;
var formatter = new JsonLogMessageFormatter();
var state = new MessageState()
{
MessageTemplate = null,
AwsRequestId = "1234",
Level = Helpers.LogLevelLoggerWriter.LogLevel.Warning,
TimeStamp = timestamp
};

var json = formatter.FormatMessage(state);
var doc = JsonDocument.Parse(json);
Assert.Equal(string.Empty, doc.RootElement.GetProperty("message").GetString());
Assert.Equal("1234", doc.RootElement.GetProperty("requestId").GetString());

// Currently the arguments are ignored because they don't exist in the message template but
// having arguments uses a different code path so we need to make sure a NPE doesn't happen.
state = new MessageState()
{
MessageTemplate = null,
MessageArguments = new object[] { true },
AwsRequestId = "1234",
Level = Helpers.LogLevelLoggerWriter.LogLevel.Warning,
TimeStamp = timestamp
};

json = formatter.FormatMessage(state);
doc = JsonDocument.Parse(json);
Assert.Equal(string.Empty, doc.RootElement.GetProperty("message").GetString());
Assert.Equal("1234", doc.RootElement.GetProperty("requestId").GetString());
}

[Fact]
public void FormatJsonWithAllPossibleTypes()
{
Expand Down