NLog with DNX Core 5.0

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

I am trying to implement NLog Logging following the example here .

However, the sample application has the following line -

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

      

And if I run the app, that line never gets hit because the default dnx core 50 is set for the mvc app.

Are there any registrars available for the DNX Core 50? If not, what is the purpose of the dnx kernel in a standard mvc application - is it really necessary?

Edit: If I delete the line #if !DNXCORE50....

above, I get the following error:

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

      

+2


source to share


3 answers


DNX Core 5.0 is only needed if you need a cloud-optimized cross-platform version of the .Net framework; if you still plan to use your MVC application only on Windows environment, you can remove the framework reference dnxcore50

from your project.json.



+4


source


NLog for .NET Core (DNX framework) is currently available in 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>
    
          

  • Loading and analyzing the 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


source


When working with MVC tools in MVC6 (dnx stuff), the answer to this question is very fluid.

To get NLog to work with my web application I had to take a couple of steps: -> Many thanks to two discussions of NLog ( here and here )

I just need to add a config setting to my Startup.cs 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 find this a bit lagging behind as Microsoft introduces a new Logging interface (which I hope ends up with SLF4J.org being in Java). Unfortunately, the documentation on the subject is a little thin at the time I write this. NLog is working hard to implement the new dnx ILoggingProvider interface.

More information on setting up my project

  • My NLog.config file is in my project root folder, next to project.json and appsettings.json. I had to dig a little inside AddJsonFile () to see how they handle paths.
  • I used yeoman.io and their aspnet generator to set up a web project.
  • NLog version, thanks to Lukasz Pirzik above:

    "NLog": "4.4.0-alpha1"

0


source







All Articles