Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (51 loc) · 2.06 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
class Program
{
public 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();
}
private static IServiceProvider BuildDi()
{
var services = new ServiceCollection();
//Runner is the custom class
services.AddTransient<Runner>();
services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));
var serviceProvider = services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
//configure NLog
loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
NLog.LogManager.LoadConfiguration("nlog.config");
return serviceProvider;
}
class Runner
{
private readonly ILogger<Runner> _logger;
public Runner(ILogger<Runner> logger)
{
_logger = logger;
}
public void DoAction(string name)
{
_logger.LogTrace(20, "Doing hard work! {Action}", name);
_logger.LogDebug(20, "Doing hard work! {Action}", name);
_logger.LogInformation(20, "Doing hard work! {Action}", name);
_logger.LogWarning(20, "Doing hard work! {Action}", name);
_logger.LogError(20, "Doing hard work! {Action}", name);
_logger.LogCritical(20, "Doing hard work! {Action}", name);
}
}
}