示例#1
0
        public void Test_WithoutLoggers_Works()
        {
            // Demonstrate that logging doesn't need to be configured for normal operation of the business logic if it is properly designed
            var test = new BusinessLogicMock <object>();

            Assert.AreEqual(test.SecondLevel(123), 123);
            Assert.AreNotEqual(test.SecondLevel(123), 0);
        }
示例#2
0
        public static async Task Main(string[] args)
        {
            SamplesLoggingHelper.LoggerInit(args, configActionNLog: () =>
            {
                NLogHelper.CreateLogger("NLog.config");
                LoggerFactoryProvider.LoggerFactory = NLogHelper.CreateLoggerFactory();
            }, configActionSerilog: () =>
            {
                SerilogHelper.CreateLogger(configure => configure.AddJsonFile("serilog.json", optional: false, reloadOnChange: true));
                LoggerFactoryProvider.LoggerFactory = SerilogHelper.CreateLoggerFactory();
            });

            Here(l => l.Entering(args));

            try
            {
                // Set correlationId for the current activity, it is preserved for the current thread and
                // across async/await
                using (Logger.BeginScope(new[] { new KeyValuePair <string, object>(Constants.CorrelationId, 12345678) }))
                {
                    List <Task <int> > tasks = new List <Task <int> >();
                    for (int i = 0; i < 10; i++)
                    {
                        tasks.Add(BusinessLogicMock <object> .GetTaskInstance(LoggerFactoryProvider.LoggerFactory));
                    }

                    // Business logic call sample
                    await Task.WhenAll(tasks);
                }

                Here(l => l.Exiting());
            }
            catch (Exception ex)
            {
                Here(l => l.LogError(ex, ex.Message));
                throw ex;
            }
            finally
            {
                SamplesLoggingHelper.LoggerConfig(configActionNLog: () =>
                {
                    NLogHelper.CloseAndFlushLogger();
                }, configActionSerilog: () =>
                {
                    SerilogHelper.CloseAndFlushLogger();
                });
            }
        }