-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
164 lines (133 loc) · 4.85 KB
/
Program.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using ShoppingCart = LoggerCodeGen.ShoppingCart;
namespace LoggerCodeGen
{
class Program
{
static void Main(string[] args)
{
var container = new ServiceCollection();
container.AddLogging(logging => logging.AddConsole());
container.AddTransient<StepOne.PaymentCalculator>();
container.AddTransient<StepTwo.PaymentCalculator>();
container.AddLogger<StepTwo.IPaymentCalculatorLogger>();
container.AddTransient<StepThree.PaymentCalculator>();
container.AddLogger<StepThree.IPaymentCalculatorLogger>();
container.AddTransient<StepFour.PaymentCalculator>();
container.AddLogger<StepFour.IPaymentCalculatorLogger>();
var services = container.BuildServiceProvider();
var shoppingCart = new ShoppingCart
{
Region = "Antarctica"
};
services
.GetRequiredService<StepOne.PaymentCalculator>()
.DetermineTaxes(shoppingCart);
services
.GetRequiredService<StepTwo.PaymentCalculator>()
.DetermineTaxes(shoppingCart);
services
.GetRequiredService<StepThree.PaymentCalculator>()
.DetermineTaxes(shoppingCart);
services
.GetRequiredService<StepFour.PaymentCalculator>()
.DetermineTaxes(shoppingCart);
}
}
public class ShoppingCart
{
public string Region { get; set; }
}
}
namespace StepOne
{
// initial project which has typed loggers in constructor arguments
public class PaymentCalculator
{
public PaymentCalculator(ILogger<PaymentCalculator> logger)
{
Logger = logger;
}
public ILogger<PaymentCalculator> Logger { get; }
internal void DetermineTaxes(ShoppingCart shoppingCart)
{
Logger.LogInformation("User is in region {TaxRegionCode}", shoppingCart.Region);
}
}
}
namespace StepTwo
{
// after a control-dot codefix on the `ILogger<PaymentCalculator> logger` warning "Create strongly typed logger interface"
public interface IPaymentCalculatorLogger : ILogger<PaymentCalculator>
{
}
public class PaymentCalculator
{
public PaymentCalculator(IPaymentCalculatorLogger logger)
{
Logger = logger;
}
public IPaymentCalculatorLogger Logger { get; }
internal void DetermineTaxes(ShoppingCart shoppingCart)
{
Logger.LogInformation("User is in region {TaxRegionCode}", shoppingCart.Region);
}
}
}
namespace StepThree
{
// after a control-dot codefix on the `Logger.LogInformation(...);` call site
// warning "Create strongly typed log methods"
//
// possibly on "entire project" or "entire solution" if log messages already have EventId argument
// or one at a time to fix method name on each call site as you go
public interface IPaymentCalculatorLogger : ILogger<PaymentCalculator>
{
[LogInformation("User is in region {TaxRegionCode}")]
void UserRegion(string tagRegionCode);
}
public class PaymentCalculator
{
public PaymentCalculator(IPaymentCalculatorLogger logger)
{
Logger = logger;
}
public IPaymentCalculatorLogger Logger { get; }
internal void DetermineTaxes(ShoppingCart shoppingCart)
{
Logger.UserRegion(shoppingCart.Region);
}
}
}
namespace StepFour
{
// base interface technically option from that point forward, and can be removed by user
// personally - I would keep it in place. there may be places which accept ILogger where it could be passed.
//
// but more importantly I would like to use .LogXxxxx syntax when I'm roughing-in any new messages I'm adding.
// why? I don't want to leave the class I'm working on in order to add a log message, and I might adjust the
// text a few times before I'm ready to send a PR.
//
// when I'm ready to make a PR and done adding/removing/editing new messages
// I would control-dot "Create strongly typed log methods" on "entire solution"
[LogCategory("StepFour.PaymentCalculator")]
public interface IPaymentCalculatorLogger
{
[LogInformation(1086, "User is in region {TaxRegionCode}")]
void UserRegion(string tagRegionCode);
}
public class PaymentCalculator
{
public PaymentCalculator(IPaymentCalculatorLogger logger)
{
Logger = logger;
}
public IPaymentCalculatorLogger Logger { get; }
internal void DetermineTaxes(ShoppingCart shoppingCart)
{
Logger.UserRegion(shoppingCart.Region);
}
}
}