diff --git a/NLog.Extensions.Logging.sln b/NLog.Extensions.Logging.sln index 631b3ddf..ecb4856c 100644 --- a/NLog.Extensions.Logging.sln +++ b/NLog.Extensions.Logging.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26730.16 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29324.140 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C21FD102-21B1-46DB-AD62-86692558AD01}" EndProject @@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLog.Extensions.Logging.Tes EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HostingExample", "examples\NetCore2\HostingExample\HostingExample.csproj", "{07D358DF-D77A-434B-B034-95785DF7106F}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleExampleJsonConfig", "examples\NetCore2\ConsoleExampleJsonConfig\ConsoleExampleJsonConfig.csproj", "{7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -56,6 +58,10 @@ Global {07D358DF-D77A-434B-B034-95785DF7106F}.Debug|Any CPU.Build.0 = Debug|Any CPU {07D358DF-D77A-434B-B034-95785DF7106F}.Release|Any CPU.ActiveCfg = Release|Any CPU {07D358DF-D77A-434B-B034-95785DF7106F}.Release|Any CPU.Build.0 = Release|Any CPU + {7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -67,6 +73,7 @@ Global {0DC000BA-2DF8-48E5-A7BC-D76CB9D3FC61} = {FBD2E07B-F25B-4D2F-AEF6-6D1E10F1E523} {DC42BF57-6316-4FCA-AD33-48FFDAFB4712} = {FBD2E07B-F25B-4D2F-AEF6-6D1E10F1E523} {07D358DF-D77A-434B-B034-95785DF7106F} = {BD106966-02BE-4137-B9DC-4ECE56B4C204} + {7C28B706-21F3-45EE-A9C3-B39AC5BB8AB5} = {BD106966-02BE-4137-B9DC-4ECE56B4C204} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {46DF0C22-7B6A-4A64-BC63-7B2F6A14F334} diff --git a/examples/NetCore2/ConsoleExample/ConsoleExample.csproj b/examples/NetCore2/ConsoleExample/ConsoleExample.csproj index c5eefde4..5c5fe601 100644 --- a/examples/NetCore2/ConsoleExample/ConsoleExample.csproj +++ b/examples/NetCore2/ConsoleExample/ConsoleExample.csproj @@ -18,11 +18,8 @@ - - PreserveNewest - - Always + PreserveNewest diff --git a/examples/NetCore2/ConsoleExample/Program.cs b/examples/NetCore2/ConsoleExample/Program.cs index 2da7fa26..691a7e4e 100644 --- a/examples/NetCore2/ConsoleExample/Program.cs +++ b/examples/NetCore2/ConsoleExample/Program.cs @@ -17,10 +17,8 @@ private static void Main() { var config = new ConfigurationBuilder() .SetBasePath(System.IO.Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build(); - LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog")); var servicesProvider = BuildDi(config); using (servicesProvider as IDisposable) diff --git a/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj b/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj new file mode 100644 index 00000000..7bf290d5 --- /dev/null +++ b/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj @@ -0,0 +1,26 @@ + + + + PackageReference + + Exe + netcoreapp2.0 + false + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/examples/NetCore2/ConsoleExampleJsonConfig/Program.cs b/examples/NetCore2/ConsoleExampleJsonConfig/Program.cs new file mode 100644 index 00000000..2da7fa26 --- /dev/null +++ b/examples/NetCore2/ConsoleExampleJsonConfig/Program.cs @@ -0,0 +1,81 @@ +using System; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog; +using NLog.Extensions.Logging; + +namespace ConsoleExample +{ + internal static class Program + { + private static void Main() + { + var logger = LogManager.GetCurrentClassLogger(); + + try + { + var config = new ConfigurationBuilder() + .SetBasePath(System.IO.Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .Build(); + + LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog")); + + var servicesProvider = BuildDi(config); + using (servicesProvider as IDisposable) + { + var runner = servicesProvider.GetRequiredService(); + runner.DoAction("Action1"); + + Console.WriteLine("Press ANY key to exit"); + Console.ReadKey(); + } + } + catch (Exception ex) + { + // NLog: catch any exception and log it. + 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) + LogManager.Shutdown(); + } + } + + private static IServiceProvider BuildDi(IConfiguration config) + { + return new ServiceCollection() + .AddTransient() // Runner is the custom class + .AddLogging(loggingBuilder => + { + // configure Logging with NLog + loggingBuilder.ClearProviders(); + loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + loggingBuilder.AddNLog(config); + }) + .BuildServiceProvider(); + } + } + + public class Runner + { + private readonly ILogger _logger; + + public Runner(ILogger logger) + { + _logger = logger; + } + + public void DoAction(string name) + { + _logger.LogDebug(20, "Doing hard work! {Action}", name); + _logger.LogInformation(21, "Doing hard work! {Action}", name); + _logger.LogWarning(22, "Doing hard work! {Action}", name); + _logger.LogError(23, "Doing hard work! {Action}", name); + _logger.LogCritical(24, "Doing hard work! {Action}", name); + } + } +} \ No newline at end of file diff --git a/examples/NetCore2/ConsoleExample/appsettings.json b/examples/NetCore2/ConsoleExampleJsonConfig/appsettings.json similarity index 92% rename from examples/NetCore2/ConsoleExample/appsettings.json rename to examples/NetCore2/ConsoleExampleJsonConfig/appsettings.json index 352cad2f..153fb877 100644 --- a/examples/NetCore2/ConsoleExample/appsettings.json +++ b/examples/NetCore2/ConsoleExampleJsonConfig/appsettings.json @@ -9,7 +9,7 @@ "NLog": { "autoreload": true, "internalLogLevel": "Info", - "internalLogFile": "c:/temp/console-example-internal.log", + "internalLogFile": "c:/temp/console-example-internal2.log", "throwConfigExceptions": true, "targets": { "console": { @@ -21,7 +21,7 @@ "target": { "wrappedFile": { "type": "File", - "fileName": "c:/temp/console-example.log", + "fileName": "c:/temp/console-example2.log", "layout": { "type": "JsonLayout", "Attributes": [ diff --git a/examples/NetCore2/HostingExample/HostingExample.csproj b/examples/NetCore2/HostingExample/HostingExample.csproj index 8ea457af..415784fb 100644 --- a/examples/NetCore2/HostingExample/HostingExample.csproj +++ b/examples/NetCore2/HostingExample/HostingExample.csproj @@ -19,7 +19,7 @@ - Always + PreserveNewest