public static void RegisterApplicationLifetimeHandling( IContainer applicationContainer, IApplicationLifetime appLifetime, TraceAgent traceAgent) { appLifetime.ApplicationStarted.Register(() => Log.Information("Application started.")); appLifetime.ApplicationStopping.Register(() => { traceAgent?.OnCompleted(); traceAgent?.Completion.Wait(); Log.Information("Application stopping."); Log.CloseAndFlush(); }); appLifetime.ApplicationStopped.Register(applicationContainer.Dispose); Console.CancelKeyPress += (sender, eventArgs) => { appLifetime.StopApplication(); // Don't terminate the process immediately, wait for the Main thread to exit gracefully. eventArgs.Cancel = true; }; }
static void Main(string[] args) { var source = new TraceSource(); var agent = new TraceAgent(); using (source.Subscribe(agent)) using (var span = source.Begin("sample-trace", "test-app", "Main", "console")) { TestSpan(span); TestSpan(span); TestSpan(span); } agent.OnCompleted(); agent.Completion.Wait(); }