Example #1
0
        static void Main(string[] args)
        {
            ServicePointManager.DefaultConnectionLimit = 100;
            IWebHost host      = null;
            var      processor = new ConsoleLoggerProcessor();
            CustomConsoleLogProvider loggerProvider = new CustomConsoleLogProvider(processor);

            using var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(loggerProvider);
            var            logger = loggerFactory.CreateLogger("Configuration");
            IConfiguration conf   = null;

            try
            {
                // This is the only way that LoadArgs can print to console. Because LoadArgs is called by the HostBuilder before Logs.Configure is called
                conf = new DefaultConfiguration()
                {
                    Logger = logger
                }.CreateConfiguration(args);
                if (conf == null)
                {
                    return;
                }
                Logs.Configure(loggerFactory);
                new BTCPayServerOptions().LoadArgs(conf);
                Logs.Configure(null);
                /////

                host = new WebHostBuilder()
                       .UseKestrel()
                       .UseIISIntegration()
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseConfiguration(conf)
                       .ConfigureLogging(l =>
                {
                    l.AddFilter("Microsoft", LogLevel.Error);
                    l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
                    l.AddFilter("Microsoft.AspNetCore.Antiforgery.Internal", LogLevel.Critical);
                    l.AddProvider(new CustomConsoleLogProvider(processor));
                })
                       .UseStartup <Startup>()
                       .Build();
                host.StartWithTasksAsync().GetAwaiter().GetResult();
                var urls = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses;
                foreach (var url in urls)
                {
                    logger.LogInformation("Listening on " + url);
                }
                host.WaitForShutdown();
            }
            catch (ConfigException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Logs.Configuration.LogError(ex.Message);
                }
            }
            catch (Exception e) when(PluginManager.IsExceptionByPlugin(e))
            {
                var pluginDir = new DataDirectories().Configure(conf).PluginDir;

                PluginManager.DisablePlugin(pluginDir, e.Source);
            }
            finally
            {
                processor.Dispose();
                if (host == null)
                {
                    Logs.Configuration.LogError("Configuration error");
                }
                if (host != null)
                {
                    host.Dispose();
                }
                Serilog.Log.CloseAndFlush();
                loggerProvider.Dispose();
            }
        }
Example #2
0
        private const long MAX_DEBUG_LOG_FILE_SIZE = 2000000; // If debug log is in use roll it every N MB.

        static void Main(string[] args)
        {
            ServicePointManager.DefaultConnectionLimit = 100;
            IWebHost host      = null;
            var      processor = new ConsoleLoggerProcessor();
            CustomConsoleLogProvider loggerProvider = new CustomConsoleLogProvider(processor);
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(loggerProvider);
            var logger = loggerFactory.CreateLogger("Configuration");

            try
            {
                // This is the only way that LoadArgs can print to console. Because LoadArgs is called by the HostBuilder before Logs.Configure is called
                var conf = new DefaultConfiguration()
                {
                    Logger = logger
                }.CreateConfiguration(args);
                if (conf == null)
                {
                    return;
                }
                Logs.Configure(loggerFactory);
                new BTCPayServerOptions().LoadArgs(conf);
                Logs.Configure(null);
                /////

                host = new WebHostBuilder()
                       .UseKestrel()
                       .UseIISIntegration()
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseConfiguration(conf)
                       .ConfigureLogging(l =>
                {
                    l.AddFilter("Microsoft", LogLevel.Error);
                    l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
                    l.AddFilter("Microsoft.AspNetCore.Antiforgery.Internal", LogLevel.Critical);
                    l.AddFilter("AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerHandler", LogLevel.Error);
                    l.AddProvider(new CustomConsoleLogProvider(processor));

                    // Use Serilog for debug log file.
                    var debugLogFile = BTCPayServerOptions.GetDebugLog(conf);
                    if (string.IsNullOrEmpty(debugLogFile) != false)
                    {
                        return;
                    }
                    Serilog.Log.Logger = new LoggerConfiguration()
                                         .Enrich.FromLogContext()
                                         .MinimumLevel.Is(BTCPayServerOptions.GetDebugLogLevel(conf))
                                         .WriteTo.File(debugLogFile, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: MAX_DEBUG_LOG_FILE_SIZE, rollOnFileSizeLimit: true, retainedFileCountLimit: 1)
                                         .CreateLogger();

                    l.AddSerilog(Serilog.Log.Logger);
                })
                       .UseStartup <Startup>()
                       .Build();
                host.StartWithTasksAsync().GetAwaiter().GetResult();
                var urls = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses;
                foreach (var url in urls)
                {
                    logger.LogInformation("Listening on " + url);
                }
                host.WaitForShutdown();
            }
            catch (ConfigException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Logs.Configuration.LogError(ex.Message);
                }
            }
            finally
            {
                processor.Dispose();
                if (host == null)
                {
                    Logs.Configuration.LogError("Configuration error");
                }
                if (host != null)
                {
                    host.Dispose();
                }
                Serilog.Log.CloseAndFlush();
                loggerProvider.Dispose();
            }
        }