Skip to content

Seeing NullReferenceException running in Docker on AWS #202

@jstnlef

Description

@jstnlef

While I'm in the process of converting my application from Serilog to ZLogger, I'm occasionally seeing the following error when I deploy my service to AWS in a linux based Docker container.

LOGGING ERROR: System.NullReferenceException: Object reference not set to an instance of an object.
at ZLogger.Internal.MagicalBox.TryReadTo(Type type, Int32 offset, Int32 alignment, String format, Utf8StringWriter`1& handler)
at ZLogger.MessageSequence.ToString(IBufferWriter`1 writer, MagicalBox box, Span`1 parameters)
at ZLogger.LogStates.InterpolatedStringLogState.ToString(IBufferWriter`1 writer)
at ZLogger.ZLoggerEntry`1.ToString(IBufferWriter`1 writer)
at ZLogger.Formatters.SystemTextJsonZLoggerFormatter.FormatLogEntry(IBufferWriter`1 writer, IZLoggerEntry entry)
at ZLogger.ZLoggerEntry`1.FormatUtf8(IBufferWriter`1 writer, IZLoggerFormatter formatter)
at ZLogger.AsyncStreamLineMessageWriter.WriteLoop()

This is causing me to miss some log lines which I expect to see. Running the locally, I never see errors of this type.

My config

builder.Logging.ClearProviders().AddZLoggerConsole(options =>
    {
      options.CaptureThreadInfo = true;
      options.IncludeScopes = true;
      options.FullMode = BackgroundBufferFullMode.Grow;
      options.InternalErrorLogger = exception => { Console.WriteLine("LOGGING ERROR: " + exception); };
      options.UseJsonFormatter(formatter =>
      {
        formatter.UseUtcTimestamp = true;
        formatter.JsonPropertyNames = JsonPropertyNames.Default with
        {
          Timestamp = JsonEncodedText.Encode("t"),
          LogLevel = JsonEncodedText.Encode("l"),
          Message = JsonEncodedText.Encode("m"),
          Category = JsonEncodedText.Encode("c")
        };
        formatter.AdditionalFormatter = (Utf8JsonWriter writer, in LogInfo info) =>
        {
          if (options.CaptureThreadInfo)
          {
            writer.WriteNumber("threadId", info.ThreadInfo.ThreadId);
            if (info.ThreadInfo.ThreadName is not null)
            {
              writer.WriteString("threadName", info.ThreadInfo.ThreadName);
            }
          }

          // Write out Datadog vars since Datadog can't automatically inject them with ZLogger
          var env = builder.Configuration["DD_ENV"];
          if (env is not null)
          {
            writer.WriteString("dd_env", env);
          }

          var service = builder.Configuration["DD_SERVICE"];
          if (service is not null)
          {
            writer.WriteString("dd_service", service);
          }

          var version = builder.Configuration["DD_VERSION"];
          if (version is not null)
          {
            writer.WriteString("dd_version", version);
          }
        };
      });
    });

Is there something specific I should be doing which I am not to prevent such an error? I'm currently using version 2.5.10 of the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions