Skip to content

NLog 6.0: Logging Throws on Invalid Format Strings #5932

@TheBigNeo

Description

@TheBigNeo

After upgrading from NLog 5.5 to NLog 6.0, we discovered a critical change in behaviour:
NLog 6.0 throws System.FormatException when an invalid format string is used in log messages.


NLog version: 6.0.1
Platform: net9.0

Current NLog config

LogManager.Setup().LoadConfiguration(builder =>
{
    builder.ForLogger().FilterMinLevel(LogLevel.Trace).WriteToColoredConsole();
});
LogManager.Setup().SetupSerialization(serializationBuilder => serializationBuilder.RegisterValueFormatterWithStringQuotes());

What is the current result?

Log.Info("My ID is {id:l}", Guid.Parse("502f088f-1c1f-4f1e-8401-a2528211086a")); // Throws System.FormatException

What is the expected result?

Log.Info("My ID is {id:l}", Guid.Parse("502f088f-1c1f-4f1e-8401-a2528211086a")); // Output: My ID is {id:l}

Full exception details

I have created a sample solution: https://github.com/TheBigNeo/demo_NLogFormatException

System.FormatException: Format string can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
   at System.Guid.ThrowBadGuidFormatSpecification()
   at System.Guid.ToString(String format, IFormatProvider provider)
   at NLog.MessageTemplates.ValueFormatter.SerializeSimpleObject(Object value, String format, IFormatProvider formatProvider, StringBuilder builder, Boolean convertToString)
   at NLog.MessageTemplates.ValueFormatter.FormatObject(Object value, String format, IFormatProvider formatProvider, StringBuilder builder)
   at NLog.MessageTemplates.ValueFormatter.FormatValue(Object value, String format, CaptureType captureType, IFormatProvider formatProvider, StringBuilder builder)
   at NLog.Internal.LogMessageTemplateFormatter.RenderHole(StringBuilder sb, CaptureType captureType, String holeFormat, IFormatProvider formatProvider, Object value)
   at NLog.Internal.LogMessageTemplateFormatter.RenderHole(StringBuilder sb, Hole& hole, IFormatProvider formatProvider, Object value)
   at NLog.Internal.LogMessageTemplateFormatter.Render(TemplateEnumerator& templateEnumerator, IFormatProvider formatProvider, ReadOnlySpan`1& parameters, IList`1& messageTemplateParameters)
   at NLog.Logger.WriteToTargetsWithSpan(ITargetWithFilterChain targetsForLevel, LogLevel level, Exception exception, IFormatProvider formatProvider, String message, ReadOnlySpan`1 args)
   at NLog.Logger.WriteToTargetsWithSpan(LogLevel level, Exception exception, IFormatProvider formatProvider, String message, ReadOnlySpan`1 args)
   at NLog.Logger.Info[TArgument](String message, TArgument argument)
   at NLog6.Program.Main() in C:\Projects\RiderProjects\Sample\NLogFormatException\NLog6\Program.cs:line 55


If this is not an error, maybe a setting could help.

LogManager.Setup().AllowInvalidFormatSpecifiers(true);

  • Is there a version in which it did work?
    • NLog 5.5.0
  • Can you help us by writing an unit test?
    • That should be possible.
  • Are there any workarounds?
    • Not really
  • Did you checked the Internal log?
    • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug report / Bug fix

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions