Skip to content

Amazon.Lambda.Logging.AspNetCore JSON logger crashes with ArgumentNullException when HttpLogging enabled #2121

@Dreamescaper

Description

@Dreamescaper

Describe the bug

When AspNetCore HttpLogging is enabled, LambdaLogger crashes with an exception.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

LambdaLogger manages to log everything what AspNetCore internals attempt to log.

Current Behavior

Logging fails with ArgumentNullException.

StackTrace:

        "System.AggregateException: An error occurred while writing to logger(s). (Value cannot be null. (Parameter 'key'))",
        "---> System.ArgumentNullException: Value cannot be null. (Parameter 'key')",
        "at System.ThrowHelper.ThrowArgumentNullException(String name)",
        "at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value)",
        "at Amazon.Lambda.RuntimeSupport.Helpers.Logging.AbstractLogMessageFormatter.ParseProperties(String messageTemplate)",
        "at Amazon.Lambda.RuntimeSupport.Helpers.Logging.JsonLogMessageFormatter.FormatMessage(MessageState state)",
        "at Amazon.Lambda.RuntimeSupport.Helpers.LogLevelLoggerWriter.WrapperTextWriter.FormattedWriteLine(String level, Exception exeception, String messageTemplate, Object[] args)",
        "at Amazon.Lambda.Core.LambdaLogger.Log(LogLevel level, Exception exception, String message, Object[] args)",
        "at Microsoft.Extensions.Logging.LambdaILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)",
        "at Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|14_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)",
        "--- End of inner exception stack trace ---",
        "at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)",
        "at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)",
        "at Microsoft.AspNetCore.HttpLogging.HttpLoggingExtensions.RequestLog(ILogger logger, HttpLog requestLog)",
        "at Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware.InvokeInternal(HttpContext context, HttpLoggingOptions options, HttpLoggingAttribute loggingAttribute, HttpLoggingFields loggingFields)",
        "at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()",
        "--- End of stack trace from previous location ---",
        "at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.ProcessRequest(ILambdaContext lambdaContext, Object context, InvokeFeatures features, Boolean rethrowUnhandledError)"

Reproduction Steps

Use the following Program configuration.

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddLambdaLogger();

builder.Services.AddHttpLogging(_ => { });
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

...

var app = builder.Build();
app.UseHttpLogging();

Attempt to invoke any endpoint.

Additional Information/Context

Seems like LambdaILogger does not handle the case when state has no "{OriginalFormat}" property.
https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs#L73

HttpLogging, however, uses custom TState object, with custom formatter, it has no {OriginalFormat} property:
https://source.dot.net/#Microsoft.AspNetCore.HttpLogging/HttpLoggingExtensions.cs,c735f435f16b6645

AWS .NET SDK and/or Package version used

Amazon.Lambda.Logging.AspNetCore 4.1.0

Targeted .NET Platform

.NET 8

Operating System and version

AmazonLinux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions