-
Notifications
You must be signed in to change notification settings - Fork 150
Getting started with .NET Core 2 Console application
Rolf Kristensen edited this page Apr 28, 2019
·
25 revisions
ℹ️ See also example in GitHub
How to use for .NET Core with csproj (VS2017 only) - demonstrated with a Console application
In Visual Studio 2017, using .NET 4.6.1+ or .NET Core 2
Install:
- The package NLog.Extensions.Logging
- The package Microsoft.Extensions.DependencyInjection - or use another DI library.
- Update the NLog package if possible
e.g.
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="NLog" Version="4.6.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
</ItemGroup>
Create nlog.config (lowercase all) file in the root of your project.
We use this example:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
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}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target1,target2" />
</rules>
</nlog>
More details of the config file are here.
If you like to include other targets or layout renderers, check the Platform support table, as there is a limited set implemented. Check the column NetStandard
. To read more about NetStandard, see the docs from Microsoft
public class Runner
{
private readonly ILogger<Runner> _logger;
public Runner(ILogger<Runner> logger)
{
_logger = logger;
}
public void DoAction(string name)
{
_logger.LogDebug(20, "Doing hard work! {Action}", name);
}
}
private static ServiceProvider BuildDi()
{
return new ServiceCollection()
.AddLogging(builder => {
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddNLog(new NLogProviderOptions {
IncludeScopes = true
});
})
.AddTransient<Runner>()
.BuildServiceProvider();
}
First create the DI container, then get your Runner
and start running!
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();
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
On screen:
In file:
2017/10/16 23:08:46.479|DEBUG|Doing hard work! Action1 |ConsoleExample.Runner|Action=Action1, EventId_Id=20, EventId_Name=, EventId=20