Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! from console");

            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", false, true)
                                .Build();

            using var serviceProvider = new ServiceCollection()
                                        .AddSingleton <IFooService, FooService>()
                                        .AddLogging(builder =>
            {
                builder.AddConfiguration(configuration.GetSection("Logging"));
                builder.AddConsole();
            })
                                        .BuildServiceProvider();

            ILogger <Program> logger     = serviceProvider.GetService <ILogger <Program> >();
            IFooService       fooService = serviceProvider.GetService <IFooService>();


            logger.LogInformation("1111logger information");
            logger.LogWarning("2222logger warning");

            fooService.DoWork();

            using (var listener = new LoggerTraceListener(logger))
            {
                System.Diagnostics.Trace.Listeners.Add(listener);
                TraceSources.Instance.InitLoggerTraceListener(listener);

                TraceLover.DoSomething();
                TraceSourceLover.DoSomething();
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! from console");

            using var listener        = new TextWriterTraceListener("c:\\temp\\ConsoleAppTraceListener.txt");
            using var consoleListener = new ConsoleTraceListener();
            Trace.Listeners.Add(listener);
            Trace.Listeners.Add(consoleListener);
            TraceSources.Instance.InitLoggerTraceListener(listener);
            TraceSources.Instance.InitLoggerTraceListener(consoleListener);

            TraceLover.DoSomething();
            TraceSourceLover.DoSomething();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! from console");

            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", false, true)
                                .Build();

            Serilog.Log.Logger = new Serilog.LoggerConfiguration()
                                 .Enrich.FromLogContext()
                                 //.WriteTo.Console() I prefer plugging through the config file
                                 .ReadFrom.Configuration(configuration)
                                 .CreateLogger();

            using var serviceProvider = new ServiceCollection()
                                        .AddLogging(builder => builder.AddSerilog())
                                        .AddSingleton <IFooService, FooService>()
                                        .BuildServiceProvider();

            var logger     = serviceProvider.GetService <ILogger <Program> >();
            var fooService = serviceProvider.GetService <IFooService>();

            try
            {
                Log.Information("Starting up");
                logger.LogInformation("1111logger information");
                logger.LogWarning("2222logger warning");

                fooService.DoWork();

                using (var listener = new LoggerTraceListener(logger))
                {
                    System.Diagnostics.Trace.Listeners.Add(listener);
                    TraceSources.Instance.InitLoggerTraceListener(listener);

                    TraceLover.DoSomething();
                    TraceSourceLover.DoSomething();
                }
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Application start-up failed");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! from console");

            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", false, true)
                                .Build();


            using (var textWriterTraceListener = new TextWriterTraceListener(@"C:\temp\ConsoleAppTraceSourceLogger2.log"))
                using (var consoleTraceListener = new ConsoleTraceListener())
                {
                    using var loggerFactory = LoggerFactory.Create(builder =>
                    {
                        builder
                        .AddConfiguration(configuration.GetSection("Logging"))
                        .AddTraceSource(new SourceSwitch("Something")
                        {
                            Level = SourceLevels.All
                        }, consoleTraceListener)
                        .AddTraceSource(new SourceSwitch("HouseKeeping")
                        {
                            Level = SourceLevels.All
                        }, textWriterTraceListener);
                        // writer: Console.Out));
                    });
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogInformation("LogInformation information");
                    logger.LogWarning("LogWarning warning");

                    MyAppTraceSources.HouseKeeping.TraceInformation("OK");

                    TraceLover.DoSomething();
                    TraceSourceLover.DoSomething();

                    //var ts = new TraceSource("HouseKeeping", SourceLevels.All);
                    //ts.Listeners.Add(consoleTraceListener);
                    //ts.Listeners.Add(textWriterTraceListener);
                    //ts.TraceInformation("HouseKeeping info");
                    //ts.TraceEvent(TraceEventType.Error, 0, "trace error");
                    //ts.TraceError("aaaaaaaaaaaaaaaaaaaaa");
                }
        }