Skip to content

Internal logging reports logger writing to target that failed to initialize #5674

@innominateAtWork

Description

@innominateAtWork

NLog version: 5.3.4
Platform: .NET8
Current NLog config: None

The code below uses the fluent interface to setup up logging. There is a bug in the code in that the [RequiredParameter] on the custom target is not set. However, the internal logging still reports that it will be writing to custom target, but it does not actually write there.

2025-01-23 19:37:23.4108 Debug Targets configured when LogLevel >= Trace for Logger: ConsoleApp1.Program
2025-01-23 19:37:23.4108 Debug Logger ConsoleApp1.Program [Trace] => MyCustom
2025-01-23 19:37:23.4108 Debug Logger ConsoleApp1.Program [Debug] => MyCustom
2025-01-23 19:37:23.4108 Debug Logger ConsoleApp1.Program [Info] => MyCustom

Ideally the internal logging would look something more like below. At minimum it should not be listing MyCustom as a target it will write to.

2025-01-23 19:37:23.4108 Debug Targets configured when LogLevel >= Trace for Logger: ConsoleApp1.Program
2025-01-23 19:37:23.4108 Warn Logger ConsoleApp1.Program [Trace] => None (Initialization failed for MyCustom)
2025-01-23 19:37:23.4108 Warn Logger ConsoleApp1.Program [Debug] => None (Initialization failed for MyCustom)
2025-01-23 19:37:23.4108 Warn Logger ConsoleApp1.Program [Info] => None (Initialization failed for MyCustom)

Sample code

using NLog.Config;
using NLog.Targets;
using NLog;

LogManager.Setup()
.LoadConfiguration(builder =>
{
    //Note: RequiredParameter "MyParameter" is not set
    var myTarget = new MyCustomTarget();
    var errorLogsBuilder = builder.ForLogger().WriteTo(myTarget);
})
.SetupInternalLogger(internalBuilder =>
{
    internalBuilder.LogToConsole(true);
    internalBuilder.SetMinimumLogLevel(LogLevel.Trace);
});

var logger = LogManager.GetCurrentClassLogger();
Console.WriteLine("Before Info Logged");
logger.Info("Info Logged");
Console.WriteLine("After Info Logged");


[Target("MyCustomTarget")]
public class MyCustomTarget : Target
{
    [RequiredParameter]
    public string? MyParameter { get; set; }


    protected override void InitializeTarget()
    {
        Console.WriteLine("InitializeTarget");
        base.InitializeTarget();
    }

    protected override void Write(LogEventInfo logEvent)
    {
        Console.WriteLine("Write - " + logEvent.FormattedMessage);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions