Пример #1
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();
                });
            }
        }
Пример #2
0
        public static void 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
            {
                CreateHostBuilder(args).Build().Run();

                // Previous logger factory (created in host builder) is now disposed ...
                SamplesLoggingHelper.LoggerConfig(configActionNLog: () =>
                {
                    LoggerFactoryProvider.LoggerFactory = NLogHelper.CreateLoggerFactory();
                }, configActionSerilog: () =>
                {
                    LoggerFactoryProvider.LoggerFactory = SerilogHelper.CreateLoggerFactory();
                });

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