Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use NLog in a .Net Core Console App #111

Closed
spottedmahn opened this issue May 11, 2017 · 18 comments
Closed

How to use NLog in a .Net Core Console App #111

spottedmahn opened this issue May 11, 2017 · 18 comments

Comments

@spottedmahn
Copy link

If I'm building a .net core console app, is this the version of NLog I should use?

Thanks,
Mike D.

@304NotModified
Copy link
Member

Yes it is!

@spottedmahn
Copy link
Author

Thanks @304NotModified! Any examples on how to configure it?

I was performing this:

var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
loggerFactory.AddNLog();
loggerFactory.ConfigureNLog("nlog.config");

but I was getting System.IO.FileNotFoundException: 'NLog.Web.AspNetCore'. I ended adding a nuget reference to NLog.Web.AspNetCore and now it is working.

Doesn't seem right though; having a dependency on NLog.Web.AspNetCore in a console app. Maybe I was doing something wrong?

@304NotModified
Copy link
Member

304NotModified commented May 11, 2017

NLog.Web.AspNetCore should not be needed. I will try to create an example

@spottedmahn spottedmahn changed the title .Net Core Console App How to use NLog in a .Net Core Console App May 11, 2017
@spottedmahn
Copy link
Author

@304NotModified can you clarify why there should be a dependency on NLog.Web.AspNetCore in a Console App? It's console app not a web app. Just curious. Thanks for your assistance here!!

@304NotModified
Copy link
Member

oops, ment NOT needed. Of course having a ASP.NET Core dependency in a console app would be ridiculous.

Please note that earlier versions (the alphas) of NLog.Extensions.Logging where ASP.NET Core only as MS build it then only for ASP.NET Core

@spottedmahn
Copy link
Author

I see, thanks @304NotModified.

I figured out what I was doing wrong. From the nlog.config I got from my web project, it had:

  <extensions>
    <!--enable NLog.Web for ASP.NET Core-->
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

Once I delete that, the exception went away. I created a sample app here to debug it.

@azaytsev-csr
Copy link

This example is for a full .net framework though, is there one for .net core 1.1?

@azaytsev-csr
Copy link

@304NotModified Both of these say we need to reference Web packages? i thought this thread says 'console app'. Are there any examples without that?

@sonofaforester
Copy link

Why is this bug closed?

@304NotModified
Copy link
Member

there is no bug here. Maybe needs some docs, feel free to add to the wiki

@304NotModified
Copy link
Member

And example of the console is here: (using Autofac)

https://github.com/NLog/NLog.Extensions.Logging/blob/master/examples/net-fullframework-example/src/net-app-using-nlog/Program.cs

bit hidden. We need to restructure the example folder I guess

@sonofaforester
Copy link

Thanks for the reply, unfortunately that is a .Net Framework 4.5.1 example. The title says he's looking for a .NET Core Console app example. I am too. I'll gladly add to the wiki if I can get something working, but so far no go.

@sonofaforester
Copy link

So the problem I was having is that it doesn't seem to automatically find the NLog.config file like it did in .Net Framework. Here's some sample code that works, with the caveat that I get a segmentation fault when the program exits. I'm using NLog 5.0.0-beta10.

using System;
using NLog;
using NLog.Config;

namespace nlogging
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            LogManager.Configuration = new XmlLoggingConfiguration($"{AppContext.BaseDirectory}NLog.config");

            var logger = LogManager.GetLogger("Example");
            logger.Info("log info");
            logger.Debug("log debug");
        }
    }
}

@304NotModified
Copy link
Member

So the problem I was having is that it doesn't seem to automatically find the NLog.config

That's indeed a known issue.

Also in the .NET Core way you should use DI and .ConfigureNLog method

@304NotModified
Copy link
Member

There is a tutorial now (really for console!)

see here: https://github.com/NLog/NLog.Extensions.Logging/wiki/Getting-started-with-.NET-Core-2---Console-application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants