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

Configure environment settings #14554

Closed
Vasilut opened this issue Sep 24, 2019 · 3 comments
Closed

Configure environment settings #14554

Vasilut opened this issue Sep 24, 2019 · 3 comments

Comments

@Vasilut
Copy link

Vasilut commented Sep 24, 2019

Hi. I have a .net core api 2.2 app where I configured my nlog (ConfigureNLog method) based on my configuration:

                public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                var logConfig = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("env"))
                    ? "NLog.Development.config"
                    : "NLog.config";
                **hostingContext.HostingEnvironment.ConfigureNLog(logConfig);**
            })
            .UseNLog()
            .UseStartup<Startup>();
}

Regarding that some things changed in 3.0, how I can achieve that configuration ? (I searched into the docs but I cannot find anything relevant).
I tried using logging.ConfigureNLog(logConfig); (but I got an obsolete warning):

             public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureLogging((hostingContext, logging) =>
                {
                    logging.ClearProviders();
                    var logConfig = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("env"))
                        ? "NLog.Development.config"
                        : "NLog.config";
                    logging.ConfigureNLog(logConfig);
                    //NLogBuilder.ConfigureNLog(logConfig); -other choice maybe ? 
                })
                .UseNLog()
                .UseStartup<Startup>();
            });

Thanks.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Sep 25, 2019

Have you tried moving to CreateHostBuilder?
cc @guardrex

@Rick-Anderson
Copy link
Contributor

This is a NLog issue. See NLog/NLog.Web#350

@snakefoot
Copy link

snakefoot commented Sep 25, 2019

@Vasilut Alternative you could setup logging before building host:

var logConfig = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("env"))
                        ? "NLog.Development.config"
                        : "NLog.config";
var logger = NLog.Web.NLogBuilder.ConfigureNLog(logConfig).GetCurrentClassLogger();
logger.Debug("init");

CreateWebHostBuilder(args).Build().Run(); 

See also Program.cs example from NLog Wiki

NLog works fine with NetCore3. But one have to adapt to the now obsolete interfaces in NetCore3, and start using new ones - IHostBuilder.UseNLog()

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

No branches or pull requests

3 participants