This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (87 loc) · 3.66 KB
/
Copy pathProgram.cs
File metadata and controls
104 lines (87 loc) · 3.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace SampleApp
{
public class Program
{
private readonly ILogger _logger;
public Program()
{
var loggingConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("logging.json", optional: false, reloadOnChange: true)
.Build();
// A Web App based program would configure logging via the WebHostBuilder.
// Create a logger factory with filters that can be applied across all logger providers.
var serviceCollection = new ServiceCollection()
.AddLogging(builder =>
{
builder
.AddConfiguration(loggingConfiguration.GetSection("Logging"))
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("SampleApp.Program", LogLevel.Debug)
.AddConsole();
#if NET461
builder.AddEventLog();
#elif NETCOREAPP2_2
#else
#error Target framework needs to be updated
#endif
});
// providers may be added to a LoggerFactory before any loggers are created
var serviceProvider = serviceCollection.BuildServiceProvider();
// getting the logger using the class's name is conventional
_logger = serviceProvider.GetRequiredService<ILogger<Program>>();
}
public static void Main(string[] args)
{
new Program().Execute(args);
}
public void Execute(string[] args)
{
_logger.LogInformation("Starting");
var startTime = DateTimeOffset.Now;
_logger.LogInformation(1, "Started at '{StartTime}' and 0x{Hello:X} is hex of 42", startTime, 42);
// or
_logger.ProgramStarting(startTime, 42);
using (_logger.PurchaseOrderScope("00655321"))
{
try
{
throw new Exception("Boom");
}
catch (Exception ex)
{
_logger.LogCritical(1, ex, "Unexpected critical error starting application");
_logger.LogError(1, ex, "Unexpected error");
_logger.LogWarning(1, ex, "Unexpected warning");
}
using (_logger.BeginScope("Main"))
{
_logger.LogInformation("Waiting for user input");
string input;
do
{
Console.WriteLine("Enter some test to log more, or 'quit' to exit.");
input = Console.ReadLine();
_logger.LogInformation("User typed '{input}' on the command line", input);
_logger.LogWarning("The time is now {Time}, it's getting late!", DateTimeOffset.Now);
}
while (input != "quit");
}
}
var endTime = DateTimeOffset.Now;
_logger.LogInformation(2, "Stopping at '{StopTime}'", endTime);
// or
_logger.ProgramStopping(endTime);
_logger.LogInformation("Stopping");
}
}
}