コード例 #1
0
        static void Main(string[] args)
        {
            var logger = new LoggerConfiguration()
                         .Enrich.WithSensitiveDataMasking()
                         .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}", "*****@*****.**");

                // 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");
            }

            // But outside the sensitive area nothing is masked
            logger.Information("Felix can be reached at [email protected]");

            Console.ReadLine();
        }
コード例 #2
0
        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}", "*****@*****.**");

                // 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();
        }