Merge pull request #459 from JocaPC/master

Better way to configure NLog
This commit is contained in:
Jovan Popovic (MSFT)
2018-10-14 01:49:22 +02:00
committed by GitHub
3 changed files with 25 additions and 15 deletions
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using NLog;
using NLog.Web;
using System.IO;
@@ -9,15 +10,22 @@ namespace ProductCatalog
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseNLog()
.UseStartup<Startup>()
.Build();
NLogBuilder.ConfigureNLog("nlog.config");
try
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseNLog()
.UseStartup<Startup>()
.Build();
host.Run();
host.Run();
} finally
{
LogManager.Shutdown();
}
}
}
}
@@ -23,7 +23,8 @@ namespace ProductCatalog
{
public Startup(IHostingEnvironment env)
{
env.ConfigureNLog("nlog.config");
// Deprecated way to initialize NLog form nlog.config, but works if you don't copy file to /bin.
//env.ConfigureNLog("nlog.config");
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
@@ -86,12 +87,7 @@ namespace ProductCatalog
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddSerilog();
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
NLog.LogManager.ThrowExceptions = true;
NLog.LogManager.ThrowConfigExceptions = true;
app.UseSession();
app.UseStaticFiles();
app.UseMvc(routes =>
@@ -61,4 +61,10 @@
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>