I just moved from .net core 2 to .net core 3.1 We am facing issues finding the appropriate way to writing and using Nlog in my startup.cs file.
I have my program file as:
public static void Main(string[] args)
{
// NLog: setup the logger first to catch all errors
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config")
.GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateWebHostBuilder(args)
.Build()
.Run();
}
catch (Exception ex)
{
//NLog: catch setup errors
logger.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
options.Limits.KeepAliveTimeout = TimeSpan.MaxValue;
});
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
In .net core 2.0 we were passing in constructor as:
private readonly ILogger<Startup> _logger;
public Startup(IWebHostEnvironment env, ILogger<Startup> logger)
{
_logger = logger;
}
And then in my ConfigureServices and Configure method I was using this logger to write logs. But with .net core 3 this has been changed. Now I am planning to use Nlog but I am not sure how can I write logs with Nlog in my startup file. Any inputs please.