Yoan Dinkov
Yoan Dinkov

Reputation: 541

NLog console doesn't work on dotnet publish

So I am trying to run NLog on dotnet core (let's say 2.1.202) so I specifically want it to work with the console output stream. And I want to get as many messages as possible (yeah, logging..) so I've implemented a simple Tracer : TraceListener. So far so good and even if I try to run it via dotnet run it still works pretty well (everything is printed at console as it should be). However, if I try to run the web application via

It fails. No log messages are actually being displayed and the project runs smoothly. So does anyone knows a way to print log messages on console via "publish" method.

Here is my Program.cs file (That's the only place where I mention Nlog, no specific services/configurations in Startup.cs)

public class Program
{
    public static void Main(string[] args)
    {
        var host = BuildWebHost(args);
        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        Trace.Listeners.Add(new Tracer(logger));

        try
        {
            Trace.WriteLine("init main");
            host.Run();
        }
        catch (Exception ex)
        {
            Trace.Fail(ex.ToString());
        }
        finally
        {
            NLog.LogManager.Shutdown();
        }
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .UseNLog()
            .Build();
}

To be fair I've checked the given example from NLog and it actually works (from publish) but still I cannot find my problem in my app.

So if somebody done something like that and knows what I am missing here to run logs via publish it would be very helpful. Cheers!

Upvotes: 0

Views: 1157

Answers (1)

Yoan Dinkov
Yoan Dinkov

Reputation: 541

So the problem was that there was internal second nlog.config (in a class library project) which was overriding my default nlog.config from the web project.

Shoutout to Julian for giving the idea to check internal nlog log - very helpful indeed.

Upvotes: 1

Related Questions