Skip to content

Bug: Enum logging throws NotSupportedException and empty log entry when running under .net8 #656

Closed
@nCubed

Description

@nCubed

Expected Behaviour

When logging an Enum, the value of the enum should be logged.

Current Behaviour

Of note: this only occurs with the .net8 runtime; not the .net6 runtime.

v1.6 introduced a change in behavior when logging an Enum. Depending on the Enum, the logged value is either an empty json object or will throw a NotSupportedException.

Using the code snippet provided, the logging output has the following behavior.

v1.5.1 (and prior)

  • logging has no errors and logs the values
{
    "timestamp": "<snip>",
    "level": "Information",
    "service": "<snip>",
    "name": "<snip>",
    "message": 5
}

{
    "timestamp": "<snip>",
    "level": "Information",
    "service": "<snip>",
    "name": "<snip>",
    "message": {
        "r": 199,
        "g": 21,
        "b": 133,
        "a": 255,
        "is_known_color": true,
        "is_empty": false,
        "is_named_color": true,
        "is_system_color": false,
        "name": "MediumVioletRed"
    }
}

v1.6.0

  • custom enum is en empty json object
  • .net enum (and 3rd party dll enum) cause an exception
{
    "timestamp": "<snip>",
    "level": "Information",
    "service": "<snip>",
    "name": "<snip>",
    "message": {}
}

Exception for 2nd log entry:

System.NotSupportedException
  HResult=0x80131515
  Message=JsonTypeInfo metadata for type 'System.Drawing.Color' was not provided by TypeInfoResolver of type '[AWS.Lambda.Powertools.Logging.Serializers.PowertoolsLoggingSerializationContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically. The unsupported member type is located on type 'System.Object'. Path: $.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue)
   at System.Text.Json.JsonSerializer.WriteStringAsObject(Object value, JsonTypeInfo jsonTypeInfo)
   at AWS.Lambda.Powertools.Logging.Serializers.PowertoolsLoggingSerializer.Serialize(Object value, Type inputType)
   at AWS.Lambda.Powertools.Logging.Internal.PowertoolsLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, String message, Object[] args)
   at Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(ILogger logger, String message, Object[] args)
   at AWS.Lambda.Powertools.Logging.LoggerExtensions.LogInformation(ILogger logger, Object message)
   at AWS.Lambda.Powertools.Logging.Logger.LogInformation(Object message)
   at Program.<Main>$(String[] args) in C:\.dev\tmp\AwsPowertoolsLogging\Console02\Program.cs:line 11

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
NotSupportedException: JsonTypeInfo metadata for type 'System.Drawing.Color' was not provided by TypeInfoResolver of type '[AWS.Lambda.Powertools.Logging.Serializers.PowertoolsLoggingSerializationContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

Code snippet

using System.Drawing;
using AWS.Lambda.Powertools.Logging;

Logger.LogInformation(Pet.Lizard);
Logger.LogInformation(Color.MediumVioletRed);

public enum Pet
{
    Cat = 1,
    Dog = 3,
    Lizard = 5
}

Possible Solution

n/a

Steps to Reproduce

  • Use the .net8 runtime
  • Use the code snippet provided with v1.5.1 and then 1.6.0

Powertools for AWS Lambda (.NET) version

v1.6.0

AWS Lambda function runtime

dotnet6

Debugging logs

No response

Metadata

Metadata

Assignees

Labels

area/loggingCore logging utilitybugUnexpected, reproducible and unintended software behaviourreleasedFix or implementation already in main and released

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions