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

Simplify setting up the NLog config #184

Closed
304NotModified opened this issue Sep 30, 2017 · 6 comments
Closed

Simplify setting up the NLog config #184

304NotModified opened this issue Sep 30, 2017 · 6 comments
Labels
ASP.NET Core ASP.NET Core - all versions feature

Comments

@304NotModified
Copy link
Member

304NotModified commented Sep 30, 2017

Simplify setting up NLog for ASP.NET Core 2

Currently setting up takes a lot of steps. It should be as easy as possible - but also flexible enough. Also the current design not really suitable for ASP.NET Core 2

Current config (code set-up only):

using NLog.Extensions.Logging;
using NLog.Web;

public Startup(IHostingEnvironment env)
{
    env.ConfigureNLog("nlog.config");
}

public void ConfigureServices(IServiceCollection Services)
{
    //call this in case you need aspnet-user-authtype/aspnet-user-identity
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

    //add NLog to ASP.NET Core
    loggerFactory.AddNLog();

    //add NLog.Web
    app.AddNLogWeb();

   //note: remove the old loggerFactory, like loggerFactory.AddConsole and  loggerFactory.AddDebug

Redesign the API design for ASP.NET Core 2

(maybe also replace ConfigureNLog(this IHostingEnvironment env)

@304NotModified
Copy link
Member Author

304NotModified commented Oct 2, 2017

Proposal, is this easy enough?

This uses the "UseNLog" pattern.

  public class Program
    {
        public static void Main(string[] args)
        {
            // NLog: setup the logger first to catch all errors
            LogManager.Configuration = new XmlLoggingConfiguration("NLog.config");
            var logger = LogManager.GetCurrentClassLogger();
            try
            {
                BuildWebHost(args).Run();
            }
            catch (Exception e)
            {
                //NLog: catch setup errors
                logger.Error(e, "Stopped program because of exception");
                throw;
            }
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args) //note: this also adds the default MS loggers
                .UseStartup<Startup>() 
                .UseNLog() // NLog: setup NLog for Dependency injection
                .Build();
    }

@grokky1, @snakefoot please your opinion, thanks!

@304NotModified
Copy link
Member Author

304NotModified commented Oct 2, 2017

i'm doubting to create a helper for

LogManager.Configuration = new XmlLoggingConfiguration("NLog.config");
var logger = LogManager.GetCurrentClassLogger();

e.g. replace it with

var logger = NLogAspNetCore.InitLogger("NLog.config");

But maybe it's needed (for #187 at least)

@304NotModified 304NotModified changed the title Simplify config of loggingConfig Simplify setting up the NLog config Oct 2, 2017
@grokky1
Copy link
Contributor

grokky1 commented Oct 2, 2017

Core2 is much simpler than before, but honestly, some stuff is still confusing, and there's too many places to tweak to get stuff done.

So, I like your convenience functions (UseNLog and InitLogger), because at least logging will be easy to setup! 👍 Also UseNLog() is named similarly to other stuff in the webhost builder so that's good. (@xperiandri : I think we discussed this a little while ago as well?)

As far as your comment //note: this also adds the default MS loggers - the default builder adds MS loggers, even in production (debug and console), but these can be removed. Maybe a note in the docs for users who might want to do that, because most users won't even realise this.

@304NotModified
Copy link
Member Author

@grokky1 thanks for the info

but these can be removed.

Thanks! Needed that info :)

If have now (locally) var logger = NLogStart.InitLogger("NLog.config");, but not sure about the name of the class (NLogStart). What do you think?

@grokky1
Copy link
Contributor

grokky1 commented Oct 4, 2017

Pity we can't use NLog because that's the name of the namespace.

But I suppose it's fine. In truth we will only use it once in the app's lifecycle.

@304NotModified
Copy link
Member Author

fixed by #191

@snakefoot snakefoot added feature ASP.NET Core ASP.NET Core - all versions and removed ASP.NET Core 2 labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASP.NET Core ASP.NET Core - all versions feature
Projects
None yet
Development

No branches or pull requests

3 participants