Skip to content

Commit f019341

Browse files
authored
Simplify the ConsoleExample (#803)
1 parent e3cc36d commit f019341

File tree

4 files changed

+19
-50
lines changed

4 files changed

+19
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- Adds support for loading [NLog Configuration from appsettings.json](https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json)
2727

2828
Notice the standard [NLog NuGet package](https://www.nuget.org/packages/NLog) is enough for using NLog Logger with simple console application on the .NET Core platform.
29-
Just add `NLog.config` file to the project, and follow the [tutorial](https://github.com/NLog/NLog/wiki/Tutorial#configure-nlog-targets-for-output) for using `GetCurrentClassLogger()`.
29+
Just configure NLog using [Fluent Setup API](https://github.com/NLog/NLog/wiki/Fluent-Configuration-API) or add `NLog.config` file to the project, and follow the [tutorial](https://github.com/NLog/NLog/wiki/Tutorial#configure-nlog-targets-for-output) for using `GetCurrentClassLogger()`.
3030

3131
## NLog.Extensions.Hosting
3232

examples/NetCore2/ConsoleExample/ConsoleExample.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
1211
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
1312
</ItemGroup>
1413

examples/NetCore2/ConsoleExample/Program.cs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using Microsoft.Extensions.Configuration;
32
using Microsoft.Extensions.DependencyInjection;
43
using Microsoft.Extensions.Logging;
5-
using NLog;
64
using NLog.Extensions.Logging;
75

86
namespace ConsoleExample
@@ -11,41 +9,21 @@ internal static class Program
119
{
1210
private static void Main()
1311
{
14-
var config = new ConfigurationBuilder().Build();
15-
16-
var logger = LogManager.Setup()
17-
.SetupExtensions(ext => ext.RegisterConfigSettings(config))
18-
.GetCurrentClassLogger();
19-
20-
try
21-
{
22-
using var servicesProvider = new ServiceCollection()
23-
.AddTransient<Runner>() // Runner is the custom class
24-
.AddLogging(loggingBuilder =>
25-
{
26-
// configure Logging with NLog
27-
loggingBuilder.ClearProviders();
28-
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
29-
loggingBuilder.AddNLog(config);
30-
}).BuildServiceProvider();
31-
32-
var runner = servicesProvider.GetRequiredService<Runner>();
33-
runner.DoAction("Action1");
34-
35-
Console.WriteLine("Press ANY key to exit");
36-
Console.ReadKey();
37-
}
38-
catch (Exception ex)
39-
{
40-
// NLog: catch any exception and log it.
41-
logger.Error(ex, "Stopped program because of exception");
42-
throw;
43-
}
44-
finally
45-
{
46-
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
47-
LogManager.Shutdown();
48-
}
12+
// Disposing the ServiceProvider will also flush / dispose / shutdown the NLog Logging Provider
13+
using var servicesProvider = new ServiceCollection()
14+
.AddTransient<Runner>() // Runner is the custom class
15+
.AddLogging(loggingBuilder =>
16+
{
17+
// configure Logging with NLog
18+
loggingBuilder.ClearProviders();
19+
loggingBuilder.AddNLog();
20+
}).BuildServiceProvider();
21+
22+
var runner = servicesProvider.GetRequiredService<Runner>();
23+
runner.DoAction("Action1");
24+
25+
Console.WriteLine("Press ANY key to exit");
26+
Console.ReadKey();
4927
}
5028
}
5129

src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,9 @@ For ASP.NET Core, check: https://www.nuget.org/packages/NLog.Web.AspNetCore
1818
<PackageReleaseNotes>
1919
ChangeLog:
2020

21-
- Updated to NLog v6.0
22-
- Removed support for NetStandard 1.3 + 1.5
23-
- Updated .NET Framework 4.6.2 with NET8-nuget-dependencies
24-
- Enabled nullable references
25-
- Avoid boxing when extracting LogEvent properties from struct
26-
- Reduce allocation when creating LogEvent with properties by using ReadOnlySpan
27-
- Enabled &lt;IsAotCompatible&gt;
28-
- Added ${host-environment} for NLog.Extensions.Hosting
29-
- Added ${host-rootdir} for NLog.Extensions.Hosting
30-
- Added ${host-appname} for NLog.Extensions.Hosting
31-
- Added RegisterHostSettings for NLog.Extensions.Hosting
21+
- Updated to NLog v6.0.1 (#802) (@snakefoot)
22+
- .NET Framework v4.6.2 with UseNLog for IHostApplicationBuilder (#801) (@snakefoot)
23+
- Fixed namespace for HostRootDirLayoutRenderer (#799) (@snakefoot)
3224

3325
Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANGELOG.MD
3426

0 commit comments

Comments
 (0)