Skip to content

Commit 1a37c9f

Browse files
Merge pull request #459 from JocaPC/master
Better way to configure NLog
2 parents e55d245 + c1809de commit 1a37c9f

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using NLog;
34
using NLog.Web;
45
using System.IO;
56

@@ -9,15 +10,22 @@ public class Program
910
{
1011
public static void Main(string[] args)
1112
{
12-
var host = new WebHostBuilder()
13-
.UseKestrel()
14-
.UseContentRoot(Directory.GetCurrentDirectory())
15-
.UseIISIntegration()
16-
.UseNLog()
17-
.UseStartup<Startup>()
18-
.Build();
13+
NLogBuilder.ConfigureNLog("nlog.config");
14+
try
15+
{
16+
var host = new WebHostBuilder()
17+
.UseKestrel()
18+
.UseContentRoot(Directory.GetCurrentDirectory())
19+
.UseIISIntegration()
20+
.UseNLog()
21+
.UseStartup<Startup>()
22+
.Build();
1923

20-
host.Run();
24+
host.Run();
25+
} finally
26+
{
27+
LogManager.Shutdown();
28+
}
2129
}
2230
}
2331
}

samples/demos/belgrade-product-catalog-demo/Startup.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class Startup
2323
{
2424
public Startup(IHostingEnvironment env)
2525
{
26-
env.ConfigureNLog("nlog.config");
26+
// Deprecated way to initialize NLog form nlog.config, but works if you don't copy file to /bin.
27+
//env.ConfigureNLog("nlog.config");
2728

2829
var builder = new ConfigurationBuilder()
2930
.SetBasePath(env.ContentRootPath)
@@ -86,12 +87,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8687
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
8788
loggerFactory.AddDebug();
8889
loggerFactory.AddSerilog();
89-
90-
//add NLog to ASP.NET Core
91-
loggerFactory.AddNLog();
92-
NLog.LogManager.ThrowExceptions = true;
93-
NLog.LogManager.ThrowConfigExceptions = true;
94-
90+
9591
app.UseSession();
9692
app.UseStaticFiles();
9793
app.UseMvc(routes =>

samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@
6161
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
6262
</ItemGroup>
6363

64+
<ItemGroup>
65+
<Content Update="nlog.config">
66+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
67+
</Content>
68+
</ItemGroup>
69+
6470
</Project>

0 commit comments

Comments
 (0)