-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathProgram.cs
92 lines (73 loc) · 2.93 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
using System;
using System.Threading.Tasks;
using Serilog.Core;
namespace Serilog.Enrichers.Sensitive.Demo
{
class Program
{
static async Task Main(string[] args)
{
var logger = new LoggerConfiguration()
.Enrich.WithSensitiveDataMasking(MaskingMode.InArea, new IMaskingOperator[]
{
new EmailAddressMaskingOperator(),
new IbanMaskingOperator(),
new CreditCardMaskingOperator(false)
})
.WriteTo.Console()
.CreateLogger();
logger.Information("Hello, world");
using (logger.EnterSensitiveArea())
{
// An e-mail address in text
logger.Information("This is a secret email address: [email protected]");
// Works for properties too
logger.Information("This is a secret email address: {Email}", "[email protected]");
// IBANs are also masked
logger.Information("Bank transfer from Felix Leiter on NL02ABNA0123456789");
// IBANs are also masked
logger.Information("Bank transfer from Felix Leiter on {BankAccount}", "NL02ABNA0123456789");
// Credit card numbers too
logger.Information("Credit Card Number: 4111111111111111");
// even works in an embedded XML string
var x = new
{
Key = 12345, XmlValue = "<MyElement><CreditCard>4111111111111111</CreditCard></MyElement>"
};
logger.Information("Object dump with embedded credit card: {x}", x);
}
// But outside the sensitive area nothing is masked
logger.Information("Felix can be reached at [email protected]");
// Now, show that this works for async contexts too
logger.Information("Now, show the Async works");
var t1 = LogAsSensitiveAsync(logger);
var t2 = LogAsUnsensitiveAsync(logger);
await Task.WhenAll(t1, t2).ConfigureAwait(false);
Console.ReadLine();
}
private static async Task LogAsSensitiveAsync(Logger logger)
{
using (logger.EnterSensitiveArea())
{
await Task.Delay(new Random().Next(1000,2000)).ConfigureAwait(false);
// Put in a delay, so we are sure these run basically simultaneiously
// An e-mail address in text
logger.Information("Sensitive: This is a secret email address: [email protected]");
// Works for properties too
await Task.Delay(new Random().Next(1000, 2000)).ConfigureAwait(false);
logger.Information("Sensitive: This is a secret email address: {Email}",
}
}
private static async Task LogAsUnsensitiveAsync(Logger logger)
{
// Put in a delay, so we are sure these run basically simultaneiously
// An e-mail address in text
await Task.Delay(new Random().Next(1000, 2000)).ConfigureAwait(false);
logger.Information("This is a secret email address: [email protected]");
// Works for properties too
await Task.Delay(new Random().Next(1000, 2000)).ConfigureAwait(false);
logger.Information("This is a secret email address: {Email}", "[email protected]");
}
}
}