Skip to content

Commit

Permalink
Update NetCore2 ConsoleExample with LogManager.LoadConfiguration()
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Mar 11, 2018
1 parent added0b commit d5fe7c7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
35 changes: 22 additions & 13 deletions examples/NetCore2/ConsoleExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ class Program
{
static void Main(string[] args)
{
var servicesProvider = BuildDi();
var runner = servicesProvider.GetRequiredService<Runner>();

runner.DoAction("Action1");

Console.WriteLine("Press ANY key to exit");
Console.ReadLine();

NLog.LogManager.Shutdown(); // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
var logger = NLog.LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();
try
{
var servicesProvider = BuildDi();
var runner = servicesProvider.GetRequiredService<Runner>();

runner.DoAction("Action1");

Console.WriteLine("Press ANY key to exit");
Console.ReadLine();
}
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();
}
}


Expand All @@ -38,8 +51,6 @@ private static IServiceProvider BuildDi()

//configure NLog
loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
loggerFactory.ConfigureNLog("nlog.config");

return serviceProvider;
}
}
Expand All @@ -58,7 +69,5 @@ public void DoAction(string name)
{
_logger.LogDebug(20, "Doing hard work! {Action}", name);
}


}
}
14 changes: 5 additions & 9 deletions examples/NetCore2/ConsoleExample/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info" >


<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="target1" fileName="c:\temp\console-example.log"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
<target xsi:type="Console" name="target2"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />


<target xsi:type="File" name="fileTarget" fileName="c:\temp\console-example.log"
layout="${date}|${level:uppercase=true}|${message} ${exception:format=tostring}|${logger}|${all-event-properties}" />
<target xsi:type="Console" name="consoleTarget"
layout="${date}|${level:uppercase=true}|${message} ${exception:format=tostring}|${logger}|${all-event-properties}" />
</targets>

<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target1,target2" />

<logger name="*" minlevel="Trace" writeTo="fileTarget,consoleTarget" />
</rules>
</nlog>
2 changes: 2 additions & 0 deletions src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ private static void SafeAddHiddenAssembly(string assemblyName, bool logOnExcepti
/// <param name="loggerFactory"></param>
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
/// <returns>Current configuration for chaining.</returns>
[Obsolete("Instead use NLog.LogManager.LoadConfiguration()")]
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, string configFileRelativePath)
{
ConfigureHiddenAssemblies();
Expand All @@ -120,6 +121,7 @@ public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFacto
/// <param name="loggerFactory"></param>
/// <param name="config">New NLog config.</param>
/// <returns>Current configuration for chaining.</returns>
[Obsolete("Instead assign property NLog.LogManager.Configuration")]
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, LoggingConfiguration config)
{
ConfigureHiddenAssemblies();
Expand Down

0 comments on commit d5fe7c7

Please sign in to comment.