NLog with DNX Core 5.0

2.4k views Asked by At

I am attempting to implement NLog logging using ASP.Net 5 and MVC 6. Be default, both DNX 451 and DNX Core 50 are included in the project template.

I am attempting to implement NLog Logging by following the example here.

However, in the sample app, there is the following line -

#if !DNXCORE50
            factory.AddNLog(new global::NLog.LogFactory());
#endif

And if I run the app, this line never gets hit because the mvc application has dnx core 50 installed by default.

Is there any loggers that are available for DNX Core 50? If not, what purpose does dnx core serve in the default mvc app - is it actually needed?

Edit: If I remove the #if !DNXCORE50.... line above, I get a the following error -

DNX Core 5.0 error - The type or namespace name 'NLog' could not be found in the global namespace'
3

There are 3 answers

0
Matt DeKrey On BEST ANSWER

DNX Core 5.0 is only necessary if you want the cloud-optimized cross-platform version of the .Net framework; if you still plan on using the MVC app within only a Windows environment, you can remove your dnxcore50 framework reference from your project.json.

1
Lukasz Pyrzyk On

NLog for .NET Core (DNX environment) is currently available in version 4.4.0-alpha1.

Steps:

  • Create NLog.config

    <?xml version="1.0" encoding="utf-8" ?>
    

    <targets>
        <target xsi:type="ColoredConsole" name="ToConsole" />
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="ToConsole" />
    </rules>
    

  • Load and parse configuration

    private static ILogger _logger;
    public static void LoggerSetup()
    {
        var reader = XmlReader.Create("NLog.config");
        var config = new XmlLoggingConfiguration(reader, null); //filename is not required.
        LogManager.Configuration = config;
        _logger = LogManager.GetCurrentClassLogger();
    }
    
    public static void Main(string[] args)
    {
        LoggerSetup();
        // Log anything you want
    }
    
1
user01CU812 On

When dealing with the MVC tooling in MVC6 (dnx stuff), the answer to this is very fluid.

In order to get NLog to work with my web app, I had to do a couple steps: -> Big thanks to two NLog discussions(here and here)

I just needed to add the configuration setup in my Startup.cs's constructor:

public Startup(IHostingEnvironment env)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();

    // Set up logging configuration
    // from: https://github.com/NLog/NLog/issues/641
    //  and: https://github.com/NLog/NLog/issues/1172
    var reader = XmlTextReader.Create(File.OpenRead(Path.Combine(builder.GetBasePath(),"NLog.config"))); //stream preferred above byte[] / string.
    LogManager.Configuration = new XmlLoggingConfiguration(reader, null); //filename is not required.
    log.Info("NLogger starting");

    Configuration = builder.Build();
}

I consider this a bit of a stop-gap as Microsoft is introducing a new Logging interface (that I hope will end up being like SLF4J.org is in Java). Unfortunately, documentation on that is a bit thin at the time I'm writing this. NLog is working diligently on getting themselves an implementation of the new dnx ILoggingProvider interface.

Additional information about my project setup

  1. My NLog.config file is located in the project root folder, next to the project.json and appsettings.json. I had to do a little digging inside AddJsonFile() to see how they handled pathing.
  2. I used yeoman.io and their aspnet generator to set up the web project.
  3. Version of NLog, thanks to Lukasz Pyrzyk above:

    "NLog": "4.4.0-alpha1"