/// <summary>
 /// Write log events to a sub-logger, where further processing may occur. Events through
 /// the sub-logger will be constrained by filters and enriched by enrichers that are
 /// active in the parent. A sub-logger cannot be used to log at a more verbose level, but
 /// a less verbose level is possible.
 /// </summary>
 /// <param name="configureLogger">An action that configures the sub-logger.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public LoggerConfiguration Logger(
     Action<LoggerConfiguration> configureLogger)
 {
     if (configureLogger == null) throw new ArgumentNullException("configureLogger");
     var lc = new LoggerConfiguration();
     configureLogger(lc);
     return Sink(new CopyingSink((ILogEventSink)lc.CreateLogger()));
 }
        public void ShouldCreateInstanceOfLogger()
        {
            var loggerConfiguration = new LoggerConfiguration();

            loggerConfiguration.WriteTo.SizeRollingFile(new CompactJsonFormatter(), "c:\\logs\\Log-{{Date}}.json", retainedFileDurationLimit: TimeSpan.FromDays(3));

            var logger = loggerConfiguration.CreateLogger();

            Assert.IsNotNull(logger);
        }
        private static ILogger CreateLoggerThatCrashes()
        {
            var loggerConfig = new LoggerConfiguration()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:31234"))
                {
                    AutoRegisterTemplate = true,
                    TemplateName = "crash"
                });

            return loggerConfig.CreateLogger();
        }
示例#4
0
        // NOTE: GetLogger is a hard-coded setup of the Logger.  Prefer the Logger from Configuration.
        public Logger GetLogger()
        {
            string filePath = Path.Combine(AppContext.BaseDirectory, "Logs");
            string today    = DateTime.Now.ToString("yyyyMMdd");

            var configuration = new LoggerConfiguration()
                                .MinimumLevel?.Verbose()
                                ?.MinimumLevel?.Override("Microsoft", LogEventLevel.Warning)
                                ?.MinimumLevel?.Override("System", LogEventLevel.Warning)
                                ?.Enrich?.FromLogContext()
                                ?.WriteTo.Console()
                                ?.WriteTo.Debug()
                                ?.WriteTo.File(Path.Combine(filePath, $"{today}.log"));

            return(configuration?.CreateLogger()
                   ?? throw new ApplicationException($"Failed to generate a new {nameof(Logger)}"));
        }
        private void DoRegister()
        {
            _templateExistsReturnCode = 200;

            _options.AutoRegisterTemplate = true;
            var loggerConfig = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithMachineName()
                .WriteTo.ColoredConsole()
                .WriteTo.Elasticsearch(_options);

            var logger = loggerConfig.CreateLogger();
            using (logger as IDisposable)
            {
                logger.Error("Test exception. Should not contain an embedded exception object.");
            }
        }
        private Tuple<SerilogLogger, SerilogSink> SetUp(LogLevel logLevel)
        {
            var sink = new SerilogSink();

            var config = new LoggerConfiguration()
                .Enrich.WithMachineName()
                .Enrich.WithProcessId()
                .Enrich.WithThreadId()
                .WriteTo.Sink(sink);

            SetMinLevel(config, logLevel);

            var provider = new SerilogLoggerProvider(config.CreateLogger());
            var logger = (SerilogLogger)provider.CreateLogger(Name);

            return new Tuple<SerilogLogger, SerilogSink>(logger, sink);
        }
        public SendsTemplateTests()
        {
            _options.AutoRegisterTemplate = true;
            var loggerConfig = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithMachineName()
                .WriteTo.ColoredConsole()
                .WriteTo.Elasticsearch(_options);

            var logger = loggerConfig.CreateLogger();
            using (logger as IDisposable)
            {
                logger.Error("Test exception. Should not contain an embedded exception object.");
            }

            this._seenHttpPosts.Should().NotBeNullOrEmpty().And.HaveCount(1);
            this._seenHttpPuts.Should().NotBeNullOrEmpty().And.HaveCount(1);
            _templatePut = this._seenHttpPuts[0];
        }
        public TemplateMatchTests()
        {
            _options.AutoRegisterTemplate = true;
            _options.IndexFormat = "dailyindex-{0:yyyy.MM.dd}-mycompany";
            _options.TemplateName = "dailyindex-logs-template";
            var loggerConfig = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithMachineName()
                .WriteTo.ColoredConsole()
                .WriteTo.Elasticsearch(_options);

            var logger = loggerConfig.CreateLogger();
            using (logger as IDisposable)
            {
                logger.Error("Test exception. Should not contain an embedded exception object.");
            }

            this._seenHttpPosts.Should().NotBeNullOrEmpty().And.HaveCount(1);
            this._seenHttpPuts.Should().NotBeNullOrEmpty().And.HaveCount(1);
            this._seenHttpHeads.Should().NotBeNullOrEmpty().And.HaveCount(1);
            _templatePut = this._seenHttpPuts[0];
        }
示例#9
0
        /// <summary>
        /// Creates the serilog factory.
        /// </summary>
        /// <param name="loggerSettings">The logger settings.</param>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="useStaticLogger">if set to <c>true</c> [use static logger].</param>
        /// <returns>ILoggerFactory.</returns>
        /// <autogeneratedoc />
        public static ILoggerFactory CreateSerilogFactory(ISerilogLoggerSettings loggerSettings,
                                                          LoggerConfiguration loggerConfiguration = null, bool useStaticLogger = true)
        {
            if (loggerSettings == null)
            {
                throw new ArgumentNullException(nameof(loggerSettings));
            }

            if (loggerConfiguration == null)
            {
                loggerConfiguration = CreateDefaultLoggerConfiguration(loggerSettings);
            }

            // Set
            var logger = loggerConfiguration?.CreateLogger();

            if (useStaticLogger)
            {
                Log.Logger = logger;
            }

            return(new SerilogLoggerFactory(logger, useStaticLogger));
        }
        /// <summary>
        /// Configure dependency injected services
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to configure</param>
        public void ConfigureServices(IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            // needful
            services.AddSingleton <IApplication>(this);

            // configure configuration
            services.UseStandardConfig <UpdatesConfiguration>(configuration);
            services.UseStandardConfig <DatabaseConfiguration>(configuration);
            services.UseStandardConfig <GeneralConfiguration>(configuration);
            services.UseStandardConfig <FileLoggingConfiguration>(configuration);
            services.UseStandardConfig <ControlPanelConfiguration>(configuration);

            // enable options which give us config reloading
            services.AddOptions();

            // this is needful for the setup wizard
            services.AddLogging();

            // other stuff needed for for setup wizard and configuration
            services.AddSingleton <IConsole, IO.Console>();
            services.AddSingleton <IDatabaseConnectionFactory, DatabaseConnectionFactory>();
            services.AddSingleton <ISetupWizard, SetupWizard>();
            services.AddSingleton <IPlatformIdentifier, PlatformIdentifier>();
            services.AddSingleton <IAsyncDelayer, AsyncDelayer>();

            GeneralConfiguration      generalConfiguration;
            DatabaseConfiguration     databaseConfiguration;
            FileLoggingConfiguration  fileLoggingConfiguration;
            ControlPanelConfiguration controlPanelConfiguration;
            IPlatformIdentifier       platformIdentifier;

            // temporarily build the service provider in it's current state
            // do it here so we can run the setup wizard if necessary
            // also allows us to get some options and other services we need for continued configuration
            using (var provider = services.BuildServiceProvider())
            {
                // run the wizard if necessary
                var setupWizard         = provider.GetRequiredService <ISetupWizard>();
                var applicationLifetime = provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IApplicationLifetime>();
                var setupWizardRan      = setupWizard.CheckRunWizard(applicationLifetime.ApplicationStopping).GetAwaiter().GetResult();

                // load the configuration options we need
                var generalOptions = provider.GetRequiredService <IOptions <GeneralConfiguration> >();
                generalConfiguration = generalOptions.Value;

                // unless this is set, in which case, we leave
                if (setupWizardRan && generalConfiguration.SetupWizardMode == SetupWizardMode.Only)
                {
                    throw new OperationCanceledException("Exiting due to SetupWizardMode configuration!");                     // we don't inject a logger in the constuctor to log this because it's not yet configured
                }
                var dbOptions = provider.GetRequiredService <IOptions <DatabaseConfiguration> >();
                databaseConfiguration = dbOptions.Value;

                var loggingOptions = provider.GetRequiredService <IOptions <FileLoggingConfiguration> >();
                fileLoggingConfiguration = loggingOptions.Value;

                var controlPanelOptions = provider.GetRequiredService <IOptions <ControlPanelConfiguration> >();
                controlPanelConfiguration = controlPanelOptions.Value;

                platformIdentifier = provider.GetRequiredService <IPlatformIdentifier>();
            }

            // setup file logging via serilog
            if (!fileLoggingConfiguration.Disable)
            {
                services.AddLogging(builder =>
                {
                    // common app data is C:/ProgramData on windows, else /usr/shar
                    var logPath = !String.IsNullOrEmpty(fileLoggingConfiguration.Directory) ? fileLoggingConfiguration.Directory : ioManager.ConcatPath(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), VersionPrefix, "Logs");

                    logPath = ioManager.ConcatPath(logPath, "tgs-{Date}.log");

                    LogEventLevel?ConvertLogLevel(LogLevel logLevel)
                    {
                        switch (logLevel)
                        {
                        case LogLevel.Critical:
                            return(LogEventLevel.Fatal);

                        case LogLevel.Debug:
                            return(LogEventLevel.Debug);

                        case LogLevel.Error:
                            return(LogEventLevel.Error);

                        case LogLevel.Information:
                            return(LogEventLevel.Information);

                        case LogLevel.Trace:
                            return(LogEventLevel.Verbose);

                        case LogLevel.Warning:
                            return(LogEventLevel.Warning);

                        case LogLevel.None:
                            return(null);

                        default:
                            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid log level {0}", logLevel));
                        }
                    }

                    var logEventLevel       = ConvertLogLevel(fileLoggingConfiguration.LogLevel);
                    var microsoftEventLevel = ConvertLogLevel(fileLoggingConfiguration.MicrosoftLogLevel);

                    var formatter = new MessageTemplateTextFormatter("{Timestamp:o} {RequestId,13} [{Level:u3}] {SourceContext:l}: {Message} ({EventId:x8}){NewLine}{Exception}", null);

                    var configuration = new LoggerConfiguration()
                                        .Enrich.FromLogContext()
                                        .WriteTo.Async(w => w.RollingFile(formatter, logPath, shared: true, flushToDiskInterval: TimeSpan.FromSeconds(2)));

                    if (logEventLevel.HasValue)
                    {
                        configuration.MinimumLevel.Is(logEventLevel.Value);
                    }

                    if (microsoftEventLevel.HasValue)
                    {
                        configuration.MinimumLevel.Override("Microsoft", microsoftEventLevel.Value);
                    }

                    builder.AddSerilog(configuration.CreateLogger(), true);
                });
            }

            // configure bearer token validation
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(jwtBearerOptions =>
            {
                // this line isn't actually run until the first request is made
                // at that point tokenFactory will be populated
                jwtBearerOptions.TokenValidationParameters = tokenFactory.ValidationParameters;
                jwtBearerOptions.Events = new JwtBearerEvents
                {
                    // Application is our composition root so this monstrosity of a line is okay
                    OnTokenValidated = ctx => ctx.HttpContext.RequestServices.GetRequiredService <IClaimsInjector>().InjectClaimsIntoContext(ctx, ctx.HttpContext.RequestAborted)
                };
            });

            // f*****g prevents converting 'sub' to M$ bs
            // can't be done in the above lambda, that's too late
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            // add mvc, configure the json serializer settings
            services
            .AddMvc(options =>
            {
                var dataAnnotationValidator = options.ModelValidatorProviders.Single(validator => validator.GetType().Name == "DataAnnotationsModelValidatorProvider");
                options.ModelValidatorProviders.Remove(dataAnnotationValidator);
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(options =>
            {
                options.AllowInputFormatterExceptionMessages      = true;
                options.SerializerSettings.NullValueHandling      = NullValueHandling.Ignore;
                options.SerializerSettings.CheckAdditionalContent = true;
                options.SerializerSettings.MissingMemberHandling  = MissingMemberHandling.Error;
                options.SerializerSettings.ReferenceLoopHandling  = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Converters             = new[] { new VersionConverter() };
            });

            if (hostingEnvironment.IsDevelopment())
            {
                string GetDocumentationFilePath(string assemblyLocation) => ioManager.ConcatPath(ioManager.GetDirectoryName(assemblyLocation), String.Concat(ioManager.GetFileNameWithoutExtension(assemblyLocation), ".xml"));

                var assemblyDocumentationPath = GetDocumentationFilePath(assemblyInformationProvider.Path);
                var apiDocumentationPath      = GetDocumentationFilePath(typeof(ApiHeaders).Assembly.Location);
                services.AddSwaggerGen(genOptions => SwaggerConfiguration.Configure(genOptions, assemblyDocumentationPath, apiDocumentationPath));
            }

            // enable browser detection
            services.AddDetectionCore().AddBrowser();

            // CORS conditionally enabled later
            services.AddCors();

            void AddTypedContext <TContext>() where TContext : DatabaseContext <TContext>
            {
                services.AddDbContext <TContext>(builder =>
                {
                    if (hostingEnvironment.IsDevelopment())
                    {
                        builder.EnableSensitiveDataLogging();
                    }
                });
                services.AddScoped <IDatabaseContext>(x => x.GetRequiredService <TContext>());
            }

            // add the correct database context type
            var dbType = databaseConfiguration.DatabaseType;

            switch (dbType)
            {
            case DatabaseType.MySql:
            case DatabaseType.MariaDB:
                AddTypedContext <MySqlDatabaseContext>();
                break;

            case DatabaseType.SqlServer:
                AddTypedContext <SqlServerDatabaseContext>();
                break;

            default:
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid {0}: {1}!", nameof(DatabaseType), dbType));
            }

            // configure other database services
            services.AddSingleton <IDatabaseContextFactory, DatabaseContextFactory>();
            services.AddSingleton <IDatabaseSeeder, DatabaseSeeder>();

            // configure security services
            services.AddScoped <IAuthenticationContextFactory, AuthenticationContextFactory>();
            services.AddScoped <IClaimsInjector, ClaimsInjector>();
            services.AddSingleton <IIdentityCache, IdentityCache>();
            services.AddSingleton <ICryptographySuite, CryptographySuite>();
            services.AddSingleton <ITokenFactory, TokenFactory>();
            services.AddSingleton <IPasswordHasher <Models.User>, PasswordHasher <Models.User> >();

            // configure platform specific services
            if (platformIdentifier.IsWindows)
            {
                if (generalConfiguration.UseBasicWatchdogOnWindows)
                {
                    services.AddSingleton <IWatchdogFactory, WatchdogFactory>();
                }
                else
                {
                    services.AddSingleton <IWatchdogFactory, WindowsWatchdogFactory>();
                }

                services.AddSingleton <ISystemIdentityFactory, WindowsSystemIdentityFactory>();
                services.AddSingleton <ISymlinkFactory, WindowsSymlinkFactory>();
                services.AddSingleton <IByondInstaller, WindowsByondInstaller>();
                services.AddSingleton <IPostWriteHandler, WindowsPostWriteHandler>();

                services.AddSingleton <WindowsNetworkPromptReaper>();
                services.AddSingleton <INetworkPromptReaper>(x => x.GetRequiredService <WindowsNetworkPromptReaper>());
                services.AddSingleton <IHostedService>(x => x.GetRequiredService <WindowsNetworkPromptReaper>());
            }
            else
            {
                services.AddSingleton <IWatchdogFactory, WatchdogFactory>();
                services.AddSingleton <ISystemIdentityFactory, PosixSystemIdentityFactory>();
                services.AddSingleton <ISymlinkFactory, PosixSymlinkFactory>();
                services.AddSingleton <IByondInstaller, PosixByondInstaller>();
                services.AddSingleton <IPostWriteHandler, PosixPostWriteHandler>();
                services.AddSingleton <INetworkPromptReaper, PosixNetworkPromptReaper>();
            }

            // configure misc services
            services.AddSingleton <ISynchronousIOManager, SynchronousIOManager>();
            services.AddSingleton <IGitHubClientFactory, GitHubClientFactory>();
            services.AddSingleton <IProcessExecutor, ProcessExecutor>();
            services.AddSingleton <IByondTopicSender>(new ByondTopicSender
            {
                ReceiveTimeout = generalConfiguration.ByondTopicTimeout,
                SendTimeout    = generalConfiguration.ByondTopicTimeout
            });

            // configure component services
            services.AddSingleton <ICredentialsProvider, CredentialsProvider>();
            services.AddSingleton <IProviderFactory, ProviderFactory>();
            services.AddSingleton <IChatFactory, ChatFactory>();
            services.AddSingleton <IInstanceFactory, InstanceFactory>();

            // configure root services
            services.AddSingleton <InstanceManager>();
            services.AddSingleton <IInstanceManager>(x => x.GetRequiredService <InstanceManager>());
            services.AddSingleton <IHostedService>(x => x.GetRequiredService <InstanceManager>());

            services.AddSingleton <IJobManager, JobManager>();
        }
示例#11
0
        public static int Main(string[] args)
        {
            //Console.WriteLine(Process.GetCurrentProcess().Id);
            //while (!Debugger.IsAttached)
            //{
            //    Thread.Sleep(100);
            //}

            CommandLineApplication <ArgumentsModel> app = new CommandLineApplication <ArgumentsModel>();

            IHostBuilder hostBuilder = new HostBuilder()
                                       .ConfigureLogging(builder =>
            {
                builder.AddSerilog(dispose: true);
            })
                                       .ConfigureServices(services =>
            {
                services.AddSingleton(app.Model);
                services.AddSingleton <Dumper>();
            });

            app.FullName = "RabbitMQ Dumper";
            app.VersionOption("-v|--version", () => Assembly.GetEntryAssembly().GetName().Version.ToString());
            app.Conventions
            .UseAttributes()
            .SetRemainingArgsPropertyOnModel()
            .SetSubcommandPropertyOnModel()
            .SetParentPropertyOnModel()
            .UseOnValidateMethodFromModel()
            .UseOnValidationErrorMethodFromModel()
            .UseConstructorInjection()
            .UseDefaultHelpOption();

            CancellationTokenSource consoleCancelled = new CancellationTokenSource();

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                eventArgs.Cancel = true;
                consoleCancelled.Cancel();
            };

            app.OnExecute(() =>
            {
                LoggerConfiguration loggerConfiguration = new LoggerConfiguration()
                                                          .Enrich.FromLogContext()
                                                          .WriteTo.Console();

                if (app.Model.Verbose)
                {
                    loggerConfiguration.MinimumLevel.Debug();
                }

                Log.Logger = loggerConfiguration.CreateLogger();

                consoleCancelled.Token.Register(() =>
                {
                    Log.Logger.Warning("Ctrl+C pressed");
                });

                hostBuilder.ConfigureServices(services =>
                {
                    Type inputType  = TargetUtilities.GetSourceType(app.Model.InputType);
                    Type outputType = TargetUtilities.GetDestinationType(app.Model.OutputType);

                    services
                    .AddSingleton <ISource>(inputType)
                    .AddSingleton <IDestination>(outputType);
                }).ConfigureServices(services =>
                {
                    services.AddSingleton(new ConsoleLifetime(consoleCancelled.Token));
                });

                using (IHost host = hostBuilder.Build())
                {
                    Dumper dumper = host.Services.GetRequiredService <Dumper>();
                    return((int)dumper.Run());
                }
            });

            try
            {
                return(app.Execute(args));
            }
            catch (CommandParsingException e)
            {
                Console.Error.WriteLine($"There was an error processing the argumentsModel: {e.Message}");
                return((int)DumperExitCode.ParsingFailure);
            }
        }
示例#12
0
        public static void Execute <TSpider>(params string[] args)
        {
            var logfile = Environment.GetEnvironmentVariable("DOTNET_SPIDER_ID");

            logfile = string.IsNullOrWhiteSpace(logfile) ? "dotnet-spider.log" : $"/logs/{logfile}.log";
            Environment.SetEnvironmentVariable("LOGFILE", logfile);

            if (Log.Logger == null)
            {
                var configure = new LoggerConfiguration()
#if DEBUG
                                .MinimumLevel.Verbose()
#else
                                .MinimumLevel.Information()
#endif
                                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                                .Enrich.FromLogContext()
                                .WriteTo.Console().WriteTo
                                .RollingFile(logfile);

                Log.Logger = configure.CreateLogger();
            }

            Framework.SetMultiThread();

            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
            configurationBuilder.AddCommandLine(args, Framework.SwitchMappings);
            configurationBuilder.AddEnvironmentVariables();
            var configuration = configurationBuilder.Build();

            var id        = configuration["DOTNET_SPIDER_ID"] ?? Guid.NewGuid().ToString("N");
            var name      = configuration["DOTNET_SPIDER_NAME"] ?? id;
            var arguments = Environment.GetCommandLineArgs();

            var builder = new SpiderHostBuilder();

            builder.ConfigureLogging(b =>
            {
#if DEBUG
                b.SetMinimumLevel(LogLevel.Debug);
#else
                b.SetMinimumLevel(LogLevel.Information);
#endif
                b.AddSerilog();
            });

            var config = configuration["DOTNET_SPIDER_CONFIG"];
            builder.ConfigureAppConfiguration(x =>
            {
                if (!string.IsNullOrWhiteSpace(config) && File.Exists(config))
                {
                    // 添加 JSON 配置文件
                    x.AddJsonFile(config);
                }
                else
                {
                    if (File.Exists("appsettings.json"))
                    {
                        x.AddJsonFile("appsettings.json");
                    }
                }

                x.AddCommandLine(Environment.GetCommandLineArgs(), Framework.SwitchMappings);
                x.AddEnvironmentVariables();
            });

            builder.ConfigureServices(services =>
            {
                services.AddThroughMessageQueue();
                services.AddLocalDownloadCenter();
                services.AddDownloaderAgent(x =>
                {
                    x.UseFileLocker();
                    x.UseDefaultAdslRedialer();
                    x.UseDefaultInternetDetector();
                });
                services.AddStatisticsCenter(x =>
                {
                    // 添加内存统计服务
                    x.UseMemory();
                });
            });

            var spiderType = typeof(TSpider);
            builder.Register(spiderType);
            var provider = builder.Build();
            var instance = provider.Create(spiderType);
            if (instance != null)
            {
                instance.Name = name;
                instance.Id   = id;
                instance.RunAsync(arguments).ConfigureAwait(true).GetAwaiter().GetResult();
            }
            else
            {
                Log.Logger.Error("Create spider object failed", 0, ConsoleColor.DarkYellow);
            }
        }
示例#13
0
        private static void Main(string[] args)
        {
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddEnvironmentVariables("ESHOP_");

            Configuration = configurationBuilder.Build();

            Console.Title = "Elastic";
            var config = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .Enrich.FromLogContext().Enrich.WithThreadId().Enrich.WithProcessName().Enrich.WithProperty("Endpoint", "Elastic")
                         .WriteTo.Console(outputTemplate: "[{Level}] {Message}{NewLine}{Exception}",
                                          restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning);

            if (!string.IsNullOrEmpty(Configuration["SeqConnection"]))
            {
                config.WriteTo.Seq(Configuration["SeqConnection"]);
            }

            Log.Logger = config.CreateLogger();

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            NServiceBus.Logging.LogManager.Use <SerilogFactory>();

            var client = GetElastic();

            // Scan working directory for assemblies containing messages
            var assemblies = new Assembly[] { Assembly.GetExecutingAssembly() }.Concat(
                DIExtensions.GetAssembliesInDirectory(selector: (file) => file.Name.StartsWith("Aggregates") || file.Name.StartsWith("eShop"))).ToList();

            _container = new Container(x =>
            {
                x.For <IValidatorFactory>().Use <StructureMapValidatorFactory>();
                x.For <IMessageSession>().Use(() => Aggregates.Bus.Instance);
                x.For <IElasticClient>().Use(client);
                x.For <Infrastructure.IUnitOfWork>().Use <UnitOfWork>();
                x.For <Aggregates.UnitOfWork.IApplication>().Add(b => (Aggregates.UnitOfWork.IApplication)b.GetInstance <Infrastructure.IUnitOfWork>());

                x.Scan(y =>
                {
                    // Note do not use structuremap's assembly scanning it will load EVERY package in nuget
                    foreach (var a in assemblies)
                    {
                        y.Assembly(a);
                    }

                    y.WithDefaultConventions();
                    y.AddAllTypesOf <ISetup>();
                });
            });
            // Setup any app stuff that might exist
            AppSetup.InitiateSetup(_container.GetAllInstances <ISetup>());
            AppSetup.SetupApplication().Wait();

            // Start the bus
            _bus = InitBus().Result;

            Console.WriteLine("Press CTRL+C to exit...");
            Console.CancelKeyPress += (sender, eArgs) =>
            {
                QuitEvent.Set();
                eArgs.Cancel = true;
            };
            QuitEvent.WaitOne();

            _bus.Stop().Wait();
        }
示例#14
0
        /// <summary>
        /// 创建日志
        /// </summary>
        public static Logger CreateLog <T>(IConfiguration configuration = null, string appName = null, string apiKey = null,
                                           string serverUrl             = null,
                                           string outputTemplate        = null)
        {
            appName ??= configuration?["appName"] ?? "none";
            apiKey ??= configuration?["exceptionless:key"];
            serverUrl ??= configuration?["exceptionless:url"];
            outputTemplate ??= configuration?["outputTemplate"] ?? "{Timestamp:HH:mm:ss} {Level:u3} {userName} 【{Message:lj}】 {SourceContext:l}{NewLine}{Exception}";

            var directory = typeof(T).Assembly.GetName().Name;
            var path      = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                path = $"c:\\logs\\{directory}\\";
            }
            else
            {
                path = "logs";
            }

            var conf = new LoggerConfiguration()
#if DEBUG
                       .MinimumLevel.Verbose()
#else
                       .MinimumLevel.Information()
#endif
                       .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                       .MinimumLevel.Override("System", LogEventLevel.Warning)
                       .Enrich.WithProperty("appName", appName)
                       .WriteTo.Console()
                       .WriteTo.Debug()
                       .WriteTo.Logger(fileLogger =>
            {
                fileLogger
                .Filter.ByIncludingOnly(xx =>
                {
                    return(xx.Level > LogEventLevel.Warning);
                })
                .WriteTo.Async(x =>
                {
                    x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.err"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate);
                });
            })
                       .WriteTo.Logger(fileLogger =>
            {
                fileLogger
                .Filter.ByIncludingOnly(xx =>
                {
                    return(xx.Level == LogEventLevel.Warning);
                })
                .WriteTo.Async(x =>
                {
                    x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.wrn"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate);
                });
            })
                       .WriteTo.Logger(fileLogger =>
            {
                fileLogger
                .MinimumLevel.Verbose()
                .Filter.ByIncludingOnly(xx => xx.Level < LogEventLevel.Warning)
                .WriteTo.Async(x =>
                {
                    x.File(Path.Combine(path, $"log-{Process.GetCurrentProcess().Id}-.inf"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: null, outputTemplate: outputTemplate);
                });
                if (configuration != null)
                {
                    fileLogger.ReadFrom.Configuration(configuration);
                }
            });

            if (!string.IsNullOrEmpty(apiKey) && !string.IsNullOrEmpty(serverUrl))
            {
                Exceptionless.ExceptionlessClient.Default.Startup(apiKey);
                Exceptionless.ExceptionlessClient.Default.Configuration.ServerUrl = serverUrl;
                conf.WriteTo.Exceptionless();
            }
            if (configuration != null)
            {
                conf.ReadFrom.Configuration(configuration);
            }
            return(conf.CreateLogger());
        }
示例#15
0
        private static void Execute(ToolOptions opts)
        {
            try
            {
                var config = new LoggerConfiguration();
                config.WriteTo.ColoredConsole(outputTemplate: "{Message:lj}{NewLine}{Exception}");

                if (opts.Verbose)
                {
                    config.MinimumLevel.Debug();
                }
                else
                {
                    config.MinimumLevel.Information();
                }

                Log.Logger = config.CreateLogger();

                ISafeguardConnection connection;
                if (!string.IsNullOrEmpty(opts.Username))
                {
                    var password = HandlePassword(opts.ReadPassword);
                    connection = Safeguard.Connect(opts.Appliance, opts.IdentityProvider, opts.Username, password,
                                                   opts.ApiVersion, opts.Insecure);
                }
                else if (!string.IsNullOrEmpty(opts.CertificateFile))
                {
                    var password = HandlePassword(opts.ReadPassword);
                    if (opts.CertificateAsData)
                    {
                        var bytes = File.ReadAllBytes(opts.CertificateFile);
                        connection = Safeguard.Connect(opts.Appliance, bytes, password, opts.ApiVersion, opts.Insecure);
                    }
                    else
                    {
                        connection = Safeguard.Connect(opts.Appliance, opts.CertificateFile, password, opts.ApiVersion,
                                                       opts.Insecure);
                    }
                }
                else if (!string.IsNullOrEmpty(opts.Thumbprint))
                {
                    connection = Safeguard.Connect(opts.Appliance, opts.Thumbprint, opts.ApiVersion, opts.Insecure);
                }
                else if (opts.Anonymous)
                {
                    connection = Safeguard.Connect(opts.Appliance, opts.ApiVersion, opts.Insecure);
                }
                else
                {
                    throw new Exception("Must specify Anonymous, Username, CertificateFile, or Thumbprint");
                }

                Log.Debug($"Access Token Lifetime Remaining: {connection.GetAccessTokenLifetimeRemaining()}");

                var responseBody = opts.Csv
                    ? connection.InvokeMethodCsv(opts.Service, opts.Method, opts.RelativeUrl, opts.Body)
                    : connection.InvokeMethod(opts.Service, opts.Method, opts.RelativeUrl, opts.Body);
                //Log.Information(responseBody); // if JSON is nested too deep Serilog swallows a '}' -- need to file issue with them
                Console.WriteLine(responseBody);

                connection.LogOut();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Fatal exception occurred");
                Environment.Exit(1);
            }
        }
示例#16
0
        public static void Initialize(ILogger bootstrappingLogger, string prefix, Action <string, IServiceCollection, ITestRunReporterContext> beforeContainerBuild)
        {
            if (_instance != null)
            {
                throw new InvalidOperationException($"The Initialize method has already been called. ");
            }
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }
            if (beforeContainerBuild == null)
            {
                throw new ArgumentNullException(nameof(beforeContainerBuild));
            }

            Environment.SetEnvironmentVariable("TEST_OUTPUT_FOLDER", Directory.GetCurrentDirectory(), EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("TEST_DEPLOYMENT_FOLDER", Directory.GetCurrentDirectory(), EnvironmentVariableTarget.Process);

            //
            // TODO: When this gets too big, look at Factories
            //

            var services = new ServiceCollection();

            RegisterDeviceSettings(bootstrappingLogger, prefix, services);
            RegisterBrowserSettings(bootstrappingLogger, prefix, services);

            var instrumentationSettings = ConfigureSettings <IInstrumentationSettings, InstrumentationSettings>(bootstrappingLogger, prefix, "InstrumentationSettings", "common.json", "instrumentationSettings", services);

            RegisterSettings <RemoteWebDriverSettings>(bootstrappingLogger, prefix, "RemoteWebDriverSettings", "common-localhost-selenium.json", "remoteWebDriverSettings", services, registerInstance: true);
            RegisterSettings <EnvironmentSettings>(bootstrappingLogger, prefix, "EnvironmentSettings", "internet.json", "environmentSettings", services, registerInstance: true);
            ConfigureSettings <IControlSettings, ControlSettings>(bootstrappingLogger, prefix, "ControlSettings", "common.json", "controlSettings", services);

            // Clear the variables so they do not creep into the rest of our implementation
            Environment.SetEnvironmentVariable("TEST_OUTPUT_FOLDER", null, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("TEST_DEPLOYMENT_FOLDER", null, EnvironmentVariableTarget.Process);

            // Singletons: statics that are instantiated once for the lifetime of the entire test run
            services.AddSingleton <IDriverSessionFactory, DriverSessionFactory>();

            var testRunReporterContext = new TestRunReporterContext()
            {
                InstrumentationSettings = instrumentationSettings,
                RootReportingFolder     = instrumentationSettings.RootReportingFolder,
                TestRunIdentity         = DateTime.Now.ToString("yyyyMMdd-HHmmss")
            };

            services.AddSingleton <ITestRunReporterContext>(testRunReporterContext);

            // Scoped: per test
            services.AddScoped(isp =>
            {
                var serilogContext = BuildSerilogConfiguration();

                var logPath = isp.GetRequiredService <ITestCaseReporterContext>().LogFilePath;

                LoggerConfiguration loggerConfiguration = new LoggerConfiguration()
                                                          .ReadFrom
                                                          .Configuration(serilogContext)
                                                          .Enrich
                                                          .FromLogContext();

                if (isp.GetRequiredService <IInstrumentationSettings>().LogFilePerTest)
                {
                    loggerConfiguration.WriteTo
                    .File(logPath);
                }
                ;

                ILogger logger = loggerConfiguration.CreateLogger();

                return(logger);
            });

            services.AddScoped <ICommandExecutor>(isp =>
            {
                var remoteWebDriverSettings = isp.GetRequiredService <RemoteWebDriverSettings>();

                var commandExecutor = new HttpCommandExecutor(new Uri(remoteWebDriverSettings.RemoteUri), TimeSpan.FromSeconds(remoteWebDriverSettings.HttpCommandExecutorTimeoutInSeconds));

                return(commandExecutor);
            });

            services.AddScoped(isp =>
            {
                var factory                 = isp.GetRequiredService <IDriverSessionFactory>();
                var browserProperties       = isp.GetRequiredService <IBrowserProperties>();
                var remoteWebDriverSettings = isp.GetRequiredService <RemoteWebDriverSettings>();
                var environmentSettings     = isp.GetRequiredService <EnvironmentSettings>();
                var controlSettings         = isp.GetRequiredService <IControlSettings>();
                var deviceSettings          = isp.GetRequiredService <IDeviceProperties>();
                var logger              = isp.GetRequiredService <ILogger>();
                var testCaseReporter    = isp.GetRequiredService <ITestCaseReporter>();
                var httpCommandExecutor = isp.GetRequiredService <ICommandExecutor>();

                var driverSession = factory.Create(deviceSettings, browserProperties, remoteWebDriverSettings, environmentSettings, controlSettings, logger, testCaseReporter, httpCommandExecutor);
                return(driverSession);
            });

            beforeContainerBuild(prefix, services, testRunReporterContext);

            var reportingContextManager = new ReportingContextRegistrationManager(bootstrappingLogger, services, testRunReporterContext);

            reportingContextManager.AssertIsNotPartiallyConfigured();
            if (!reportingContextManager.IsConfigured)
            {
                reportingContextManager.PopulateDefaultReportingContexts();
            }

            _instance = services.BuildServiceProvider();
        }
示例#17
0
        public static void Run()
        {
            Test.Log("Caller oke");

            SelfLog.Enable(Console.Out);
            SelfLog.WriteLine("This is a self log");

#if true
            var depContext = DependencyContext.Load(typeof(SerilogExamples).Assembly);

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
                                .Build();

            var serilogCfgSection = configuration.GetSection("Serilog");

            var config = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration, sectionName: "Serilog", dependencyContext: depContext);

            string enricherTransform;

            if (serilogCfgSection.TryParse <string>(out enricherTransform, key: "TransformTestEnricher", trans => trans != null))
            {
                config = config.Destructure.ByTransforming <TestEnricher>(o => enricherTransform);
            }

            Log.Logger = config.CreateLogger();
#endif

#if false
            var levelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Debug);
            var template    = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {LoggerName} {ClientIp} {Message:lj}{Username:j}{NewLine}{Exception}";
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Error)
                         .Enrich.WithThreadId()
                         .Enrich.WithClientIp()
                         .Enrich.FromLogContext()
                         .Enrich.WithTestEnricher()
                         .Destructure.ByTransforming <TestEnricher>(o => "He is enricher")
#if false
                         .Destructure.With <DestructorPolicy>()
#endif
                         .WriteTo.CustomSink()
                         .WriteTo.Console(levelSwitch: levelSwitch, outputTemplate: template, formatProvider: CultureInfo.CurrentCulture)
                         .WriteTo.Async(cfg => cfg.File("logs/async.txt"), bufferSize: 1000, blockWhenFull: true, monitor: new MonitorConfiguration())
                         .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Hour, outputTemplate: template)
#if false
                         .WriteTo.Logger(cfg =>
                                         cfg.Filter.ByExcluding(Matching.WithProperty <string>("Username", username => username.Contains("z")))
                                         .Enrich.WithProperty("LoggerName", "VCL")
                                         .WriteTo.Console(outputTemplate: template))
#endif
                         .CreateLogger();
#endif

            using (LogContext.PushProperty("A", 1))
            {
                Log.Information("Carries property A = 1");

                using (LogContext.PushProperty("A", 2))
                    using (LogContext.PushProperty("B", 1))
                        using (LogContext.PushProperty("ClientIp", "11.1..1.1.1.11"))
                        {
                            Log.Information("Carries A = 2 and B = 1 {A}"); // wrong use of property
                        }

                Log.Information("Carries property A = 1, again");
            }

            var test            = new TestEnricher();
            var enricherContext = Log.ForContext <TestEnricher>()
                                  .ForContext("ContextVal", "OKEOKE");
            enricherContext.Information("Connected to {@Enricher} {SourceContext} {ContextVal}", test); // wrong use of property

            var count = 456;
            Log.Information("Retrieved {Count} records", count);

            var sensorInput = new { Latitude = 25, Longitude = 134 };
            Log.Information("Processing {@SensorInputDes} {SensorInputRaw}", sensorInput, sensorInput);

            var fruit = new[] { "Apple", "Pear", "Orange" };
            Log.Information("In my bowl I have {Fruit}", fruit);

            var fruits = new Dictionary <string, int> {
                { "Apple", 1 }, { "Pear", 5 }
            };
            Log.Information("In my bowl I have {Fruit}", fruits);

            Log.Information("Hello, world!");

            var unknown = new[] { 1, 2, 3 };
            Log.Information("Received {$Data}", unknown);

            int a = 10, b = 0;
            try
            {
                Log.Debug("Dividing {A} by {B}", a, b);
                Console.WriteLine(a / b);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Something went wrong");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        public void ThrowAndLogAndCatchBulkOutput(string exceptionMessage)
        {
            var loggerConfig = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithMachineName()
                .WriteTo.ColoredConsole()
                .WriteTo.Elasticsearch(_options);

            var logger = loggerConfig.CreateLogger();
            using (logger as IDisposable)
            {
                try
                {
                    try
                    {
                        throw new Exception("inner most exception");
                    }
                    catch (Exception e)
                    {
                        var innerException = new NastyException("nasty inner exception", e);
                        throw new Exception(exceptionMessage, innerException);
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e, "Test exception. Should contain an embedded exception object.");
                }
                logger.Error("Test exception. Should not contain an embedded exception object.");
            }

            var postedEvents = GetPostedLogEvents(expectedCount: 2);
            Console.WriteLine("BULK OUTPUT BEGIN ==========");
            foreach (var post in _seenHttpPosts)
                Console.WriteLine(post);
            Console.WriteLine("BULK OUTPUT END ============");

            var firstEvent = postedEvents[0];
            firstEvent.Exceptions.Should().NotBeNull().And.HaveCount(3);
            firstEvent.Exceptions[0].Message.Should().NotBeNullOrWhiteSpace()
                .And.Be(exceptionMessage);
            var realException = firstEvent.Exceptions[0];
            realException.ExceptionMethod.Should().NotBeNull();
            realException.ExceptionMethod.Name.Should().NotBeNullOrWhiteSpace();
            realException.ExceptionMethod.AssemblyName.Should().NotBeNullOrWhiteSpace();
            realException.ExceptionMethod.AssemblyVersion.Should().NotBeNullOrWhiteSpace();
            realException.ExceptionMethod.ClassName.Should().NotBeNullOrWhiteSpace();
            realException.ExceptionMethod.Signature.Should().NotBeNullOrWhiteSpace();
            realException.ExceptionMethod.MemberType.Should().BeGreaterThan(0);

            var nastyException = firstEvent.Exceptions[1];
            nastyException.Depth.Should().Be(1);
            nastyException.Message.Should().Be("nasty inner exception");
            nastyException.HelpUrl.Should().Be("help url");
            nastyException.StackTraceString.Should().Be("stack trace string");
            nastyException.RemoteStackTraceString.Should().Be("remote stack trace string");
            nastyException.RemoteStackIndex.Should().Be(1);
            nastyException.HResult.Should().Be(123123);
            nastyException.Source.Should().Be("source");
            nastyException.ClassName.Should().Be("classname nasty exception");
            //nastyException.WatsonBuckets.Should().BeEquivalentTo(new byte[] {1,2,3});

            var secondEvent = postedEvents[1];
            secondEvent.Exceptions.Should().BeNullOrEmpty();
        }
示例#19
0
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Starting RoadRegistry.Product.ProjectionHost");

            AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
                                                            Log.Debug(eventArgs.Exception, "FirstChanceException event raised in {AppDomain}.", AppDomain.CurrentDomain.FriendlyName);

            AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
                                                          Log.Fatal((Exception)eventArgs.ExceptionObject, "Encountered a fatal exception, exiting program.");

            var host = new HostBuilder()
                       .ConfigureHostConfiguration(builder => {
                builder
                .AddEnvironmentVariables("DOTNET_")
                .AddEnvironmentVariables("ASPNETCORE_");
            })
                       .ConfigureAppConfiguration((hostContext, builder) =>
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                if (hostContext.HostingEnvironment.IsProduction())
                {
                    builder
                    .SetBasePath(Directory.GetCurrentDirectory());
                }

                builder
                .AddJsonFile("appsettings.json", true, false)
                .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName.ToLowerInvariant()}.json", true, false)
                .AddJsonFile($"appsettings.{Environment.MachineName.ToLowerInvariant()}.json", true, false)
                .AddEnvironmentVariables()
                .AddCommandLine(args);
            })
                       .ConfigureLogging((hostContext, builder) =>
            {
                Serilog.Debugging.SelfLog.Enable(Console.WriteLine);

                var loggerConfiguration = new LoggerConfiguration()
                                          .ReadFrom.Configuration(hostContext.Configuration)
                                          .Enrich.FromLogContext()
                                          .Enrich.WithMachineName()
                                          .Enrich.WithThreadId()
                                          .Enrich.WithEnvironmentUserName();

                Log.Logger = loggerConfiguration.CreateLogger();

                builder.AddSerilog(Log.Logger);
            })
                       .ConfigureServices((hostContext, builder) =>
            {
                var blobOptions = new BlobClientOptions();
                hostContext.Configuration.Bind(blobOptions);

                switch (blobOptions.BlobClientType)
                {
                case nameof(S3BlobClient):
                    var s3Options = new S3BlobClientOptions();
                    hostContext.Configuration.GetSection(nameof(S3BlobClientOptions)).Bind(s3Options);

                    // Use MINIO
                    if (hostContext.Configuration.GetValue <string>("MINIO_SERVER") != null)
                    {
                        if (hostContext.Configuration.GetValue <string>("MINIO_ACCESS_KEY") == null)
                        {
                            throw new Exception("The MINIO_ACCESS_KEY configuration variable was not set.");
                        }

                        if (hostContext.Configuration.GetValue <string>("MINIO_SECRET_KEY") == null)
                        {
                            throw new Exception("The MINIO_SECRET_KEY configuration variable was not set.");
                        }

                        builder.AddSingleton(new AmazonS3Client(
                                                 new BasicAWSCredentials(
                                                     hostContext.Configuration.GetValue <string>("MINIO_ACCESS_KEY"),
                                                     hostContext.Configuration.GetValue <string>("MINIO_SECRET_KEY")),
                                                 new AmazonS3Config
                        {
                            RegionEndpoint = RegionEndpoint.USEast1,                 // minio's default region
                            ServiceURL     = hostContext.Configuration.GetValue <string>("MINIO_SERVER"),
                            ForcePathStyle = true
                        }
                                                 )
                                             );
                    }
                    else         // Use AWS
                    {
                        if (hostContext.Configuration.GetValue <string>("AWS_ACCESS_KEY_ID") == null)
                        {
                            throw new Exception("The AWS_ACCESS_KEY_ID configuration variable was not set.");
                        }

                        if (hostContext.Configuration.GetValue <string>("AWS_SECRET_ACCESS_KEY") == null)
                        {
                            throw new Exception("The AWS_SECRET_ACCESS_KEY configuration variable was not set.");
                        }

                        builder.AddSingleton(new AmazonS3Client(
                                                 new BasicAWSCredentials(
                                                     hostContext.Configuration.GetValue <string>("AWS_ACCESS_KEY_ID"),
                                                     hostContext.Configuration.GetValue <string>("AWS_SECRET_ACCESS_KEY"))
                                                 )
                                             );
                    }
                    builder.AddSingleton <IBlobClient>(sp =>
                                                       new S3BlobClient(
                                                           sp.GetService <AmazonS3Client>(),
                                                           s3Options.Buckets[WellknownBuckets.UploadsBucket]
                                                           )
                                                       );

                    break;

                case nameof(FileBlobClient):
                    var fileOptions = new FileBlobClientOptions();
                    hostContext.Configuration.GetSection(nameof(FileBlobClientOptions)).Bind(fileOptions);

                    builder.AddSingleton <IBlobClient>(sp =>
                                                       new FileBlobClient(
                                                           new DirectoryInfo(fileOptions.Directory)
                                                           )
                                                       );
                    break;

                default:
                    throw new Exception(blobOptions.BlobClientType + " is not a supported blob client type.");
                }

                builder
                .AddSingleton <IClock>(SystemClock.Instance)
                .AddSingleton <Scheduler>()
                .AddHostedService <EventProcessor>()
                .AddSingleton(new RecyclableMemoryStreamManager())
                .AddSingleton(new EnvelopeFactory(
                                  EventProcessor.EventMapping,
                                  new EventDeserializer((eventData, eventType) =>
                                                        JsonConvert.DeserializeObject(eventData, eventType, EventProcessor.SerializerSettings)))
                              )
                .AddSingleton <Func <ProductContext> >(
                    () =>
                    new ProductContext(
                        new DbContextOptionsBuilder <ProductContext>()
                        .UseSqlServer(
                            hostContext.Configuration.GetConnectionString(WellknownConnectionNames.ProductProjections),
                            options => options.EnableRetryOnFailure()
                            ).Options)
                    )
                .AddSingleton(sp => new ConnectedProjection <ProductContext>[]
                {
                    new OrganizationRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new GradeSeparatedJunctionRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadNetworkInfoProjection(),
                    new RoadNodeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentEuropeanRoadAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentLaneAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentNationalRoadAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentNumberedRoadAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentSurfaceAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding),
                    new RoadSegmentWidthAttributeRecordProjection(sp.GetRequiredService <RecyclableMemoryStreamManager>(), WindowsAnsiEncoding)
                })
                .AddSingleton(sp =>
                              Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector.Resolve
                              .WhenEqualToHandlerMessageType(
                                  sp.GetRequiredService <ConnectedProjection <ProductContext>[]>()
                                  .SelectMany(projection => projection.Handlers)
                                  .ToArray()
                                  )
                              )
                .AddSingleton(sp => AcceptStreamMessage.WhenEqualToMessageType(sp.GetRequiredService <ConnectedProjection <ProductContext>[]>(), EventProcessor.EventMapping))
                .AddSingleton <IStreamStore>(sp =>
                                             new MsSqlStreamStore(
                                                 new MsSqlStreamStoreSettings(
                                                     sp
                                                     .GetService <IConfiguration>()
                                                     .GetConnectionString(WellknownConnectionNames.Events)
                                                     )
                {
                    Schema = WellknownSchemas.EventSchema
                }))
                .AddSingleton <IRunnerDbContextMigratorFactory>(new ProductContextMigrationFactory());
            })
                       .Build();

            var migratorFactory   = host.Services.GetRequiredService <IRunnerDbContextMigratorFactory>();
            var configuration     = host.Services.GetRequiredService <IConfiguration>();
            var streamStore       = host.Services.GetRequiredService <IStreamStore>();
            var loggerFactory     = host.Services.GetRequiredService <ILoggerFactory>();
            var logger            = host.Services.GetRequiredService <ILogger <Program> >();
            var blobClientOptions = new BlobClientOptions();

            configuration.Bind(blobClientOptions);

            try
            {
                await WaitFor.SeqToBecomeAvailable(configuration).ConfigureAwait(false);

                logger.LogSqlServerConnectionString(configuration, WellknownConnectionNames.Events);
                logger.LogSqlServerConnectionString(configuration, WellknownConnectionNames.ProductProjections);
                logger.LogSqlServerConnectionString(configuration, WellknownConnectionNames.ProductProjectionsAdmin);
                logger.LogBlobClientCredentials(blobClientOptions);

                await DistributedLock <Program> .RunAsync(async() =>
                {
                    await WaitFor.SqlStreamStoreToBecomeAvailable(streamStore, logger).ConfigureAwait(false);
                    await migratorFactory.CreateMigrator(configuration, loggerFactory)
                    .MigrateAsync(CancellationToken.None).ConfigureAwait(false);
                    await host.RunAsync().ConfigureAwait(false);
                },
                                                          DistributedLockOptions.LoadFromConfiguration(configuration),
                                                          logger)
                .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                logger.LogCritical(e, "Encountered a fatal exception, exiting program.");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
示例#20
0
 public void CreateLoggerThrowsIfCalledMoreThanOnce()
 {
     var loggerConfiguration = new LoggerConfiguration();
     loggerConfiguration.CreateLogger();
     Assert.Throws<InvalidOperationException>(() => loggerConfiguration.CreateLogger());
 }
示例#21
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            LConfig = new LoggerConfiguration()
                      .Enrich.FromLogContext()
                      .Enrich.WithThreadId()
                      .Enrich.WithProcessId()
                      .MinimumLevel.Debug()
                      .WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss}<{ThreadId:d2}> [{Level:u3}] {Message}{NewLine}{Exception}");
            L = Log.Logger = LConfig.CreateLogger();
            Type[]                BenchmarkOptionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Options))).ToArray();
            MethodInfo            parseArgumentsMethod = typeof(ParserExtensions).GetMethods().Where(m => m.IsGenericMethod && m.Name == "ParseArguments" && m.GetGenericArguments().Count() == BenchmarkOptionTypes.Count()).First();
            Parser                p      = new Parser();
            ParserResult <object> result = (ParserResult <object>)parseArgumentsMethod.MakeGenericMethod(BenchmarkOptionTypes).Invoke(p, new object[] { p, args });

            result.WithNotParsed((IEnumerable <Error> errors) =>
            {
                HelpText help            = GetAutoBuiltHelpText(result);
                help.MaximumDisplayWidth = Console.WindowWidth;
                help.Copyright           = string.Empty;
                help.Heading             = new HeadingInfo("jemalloc.NET", Version.ToString(3));
                help.AddPreOptionsLine(string.Empty);
                if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError))
                {
                    Log.Information(help);
                    Exit(ExitResult.SUCCESS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError))
                {
                    HelpVerbRequestedError error = (HelpVerbRequestedError)errors.First(e => e.Tag == ErrorType.HelpVerbRequestedError);
                    if (error.Type != null)
                    {
                        help.AddVerbs(error.Type);
                    }
                    Log.Information(help);
                    Exit(ExitResult.SUCCESS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.HelpRequestedError))
                {
                    help.AddVerbs(BenchmarkOptionTypes);
                    L.Information(help);
                    Exit(ExitResult.SUCCESS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.NoVerbSelectedError))
                {
                    help.AddVerbs(BenchmarkOptionTypes);
                    help.AddPreOptionsLine("No category selected. Select a category from the options below:");
                    L.Information(help);
                    Exit(ExitResult.INVALID_OPTIONS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.MissingRequiredOptionError))
                {
                    MissingRequiredOptionError error = (MissingRequiredOptionError)errors.First(e => e is MissingRequiredOptionError);
                    help.AddOptions(result);
                    help.AddPreOptionsLine($"A required option or value is missing. The options and values for this benchmark category are: ");
                    L.Information(help);
                    Exit(ExitResult.INVALID_OPTIONS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.MissingValueOptionError))
                {
                    MissingValueOptionError error = (MissingValueOptionError)errors.First(e => e.Tag == ErrorType.MissingValueOptionError);
                    help.AddOptions(result);
                    help.AddPreOptionsLine($"A required option or value is missing. The options and values for this benchmark category are: ");
                    L.Information(help);
                    Exit(ExitResult.INVALID_OPTIONS);
                }
                else if (errors.Any(e => e.Tag == ErrorType.UnknownOptionError))
                {
                    UnknownOptionError error = (UnknownOptionError)errors.First(e => e.Tag == ErrorType.UnknownOptionError);
                    help.AddOptions(result);
                    help.AddPreOptionsLine($"Unknown option: {error.Token}.");
                    L.Information(help);
                    Exit(ExitResult.INVALID_OPTIONS);
                }
                else
                {
                    help.AddPreOptionsLine($"An error occurred parsing the program options: {string.Join(' ', errors.Select(e => e.Tag.ToString()).ToArray())}");
                    help.AddVerbs(BenchmarkOptionTypes);
                    L.Information(help);
                    Exit(ExitResult.INVALID_OPTIONS);
                }
            })
            .WithParsed((Options o) =>
            {
                foreach (PropertyInfo prop in o.GetType().GetProperties())
                {
                    BenchmarkOptions.Add(prop.Name, prop.GetValue(o));
                }
                if (o.ColdStart)
                {
                    JemBenchmarkJobAttribute.ColdStartOverride = true;
                }
                if (o.TargetCount > 0)
                {
                    JemBenchmarkJobAttribute.TargetCountOverride = o.TargetCount;
                }
            })
            .WithParsed <MallocBenchmarkOptions>(o =>
            {
                BenchmarkOptions.Add("Category", Category.MALLOC);
                if (o.Create)
                {
                    BenchmarkOptions.Add("Operation", Operation.CREATE);
                }
                else if (o.Fill)
                {
                    BenchmarkOptions.Add("Operation", Operation.FILL);
                }

                if (!BenchmarkOptions.ContainsKey("Operation"))
                {
                    Log.Error("You must select an operation to benchmark with --fill.");
                    Exit(ExitResult.SUCCESS);
                }
                else
                {
                    Benchmark(o);
                }
            })

            .WithParsed <NativeArrayBenchmarkOptions>(o =>
            {
                BenchmarkOptions.Add("Category", Category.NARRAY);
                if (o.Create)
                {
                    BenchmarkOptions.Add("Operation", Operation.CREATE);
                }
                else if (o.Fill)
                {
                    BenchmarkOptions.Add("Operation", Operation.FILL);
                }
                else if (o.Math)
                {
                    BenchmarkOptions.Add("Operation", Operation.MATH);
                }

                if (!BenchmarkOptions.ContainsKey("Operation"))
                {
                    Log.Error("You must select an operation to benchmark with --create or --fill.");
                    Exit(ExitResult.SUCCESS);
                }
                else
                {
                    Benchmark(o);
                }
            })

            .WithParsed <HugeNativeArrayBenchmarkOptions>(o =>
            {
                BenchmarkOptions.Add("Category", Category.HUGEARRAY);
                if (o.Create)
                {
                    BenchmarkOptions.Add("Operation", Operation.CREATE);
                }
                else if (o.Fill)
                {
                    BenchmarkOptions.Add("Operation", Operation.FILL);
                }
                else if (o.Math)
                {
                    BenchmarkOptions.Add("Operation", Operation.MATH);
                }

                if (!BenchmarkOptions.ContainsKey("Operation"))
                {
                    Log.Error("You must select an operation to benchmark with --create or --fill or --math.");
                    Exit(ExitResult.SUCCESS);
                }
                else
                {
                    Benchmark(o);
                }
            });
        }
示例#22
0
        public static void Main(string[] args)
        {
            var dictionary = new Dictionary <string, string>();

            for (int idx = 0; idx < args.Length; idx += 2)
            {
                dictionary.Add(args[idx], args[idx + 1]);
            }

            dictionary.TryGetValue("--file", out string file);

            Console.WriteLine($"File: {file ?? _defaultStoreFile}");

            foreach (var kvp in dictionary)
            {
                MainConfiguration.Add(kvp.Key.Replace("-", ""), kvp.Value);
            }

            MainConfiguration.Add("currentPath", Directory.GetCurrentDirectory());

            if (!MainConfiguration.ContainsKey("file"))
            {
                MainConfiguration.Add("file", file ?? _defaultStoreFile);
            }

            var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

            Configuration = new ConfigurationBuilder()
                            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                            .AddJsonFile($"appsettings.{env}.json", optional: true)
                            .AddJsonFile("authentication.json", optional: true, reloadOnChange: true)
                            .AddInMemoryCollection(MainConfiguration)
                            .AddEnvironmentVariables()
                            .Build();

            var logConfig = new LoggerConfiguration()
                            .ReadFrom.Configuration(Configuration)
                            .WriteTo.RollingFile(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log-{Date}.txt"));

            if (env == "Production")
            {
                logConfig = logConfig.MinimumLevel.Error();
            }
            else
            {
                logConfig = logConfig.MinimumLevel.Information();
            }

            Log.Logger = logConfig.CreateLogger();

            try
            {
                Log.Information("Starting Fake JSON Server");
                BuildWebHost(args).Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
示例#23
0
        static DatadogLogging()
        {
            // No-op for if we fail to construct the file logger
            var defaultRateLimiter = new LogRateLimiter(DefaultLogMessageRateLimit);

            InternalLogger =
                new LoggerConfiguration()
                .WriteTo.Sink <NullSink>()
                .CreateLogger();

            SharedLogger = new DatadogSerilogLogger(InternalLogger, defaultRateLimiter);

            try
            {
                if (GlobalSettings.Source.DebugEnabled)
                {
                    LoggingLevelSwitch.MinimumLevel = LogEventLevel.Debug;
                }

                var maxLogSizeVar = EnvironmentHelpers.GetEnvironmentVariable(ConfigurationKeys.MaxLogFileSize);
                if (long.TryParse(maxLogSizeVar, out var maxLogSize))
                {
                    // No verbose or debug logs
                    MaxLogFileSize = maxLogSize;
                }

                string logDirectory = null;
                try
                {
                    logDirectory = GetLogDirectory();
                }
                catch
                {
                    // Do nothing when an exception is thrown for attempting to access the filesystem
                }

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                if (logDirectory == null)
                {
                    return;
                }

                // Ends in a dash because of the date postfix
                var managedLogPath = Path.Combine(logDirectory, $"dotnet-tracer-managed-{DomainMetadata.ProcessName}-.log");

                var loggerConfiguration =
                    new LoggerConfiguration()
                    .Enrich.FromLogContext()
                    .MinimumLevel.ControlledBy(LoggingLevelSwitch)
                    .WriteTo.File(
                        managedLogPath,
                        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{Properties}{NewLine}",
                        rollingInterval: RollingInterval.Day,
                        rollOnFileSizeLimit: true,
                        fileSizeLimitBytes: MaxLogFileSize);

                try
                {
                    loggerConfiguration.Enrich.WithProperty("MachineName", DomainMetadata.MachineName);
                    loggerConfiguration.Enrich.WithProperty("Process", $"[{DomainMetadata.ProcessId} {DomainMetadata.ProcessName}]");
                    loggerConfiguration.Enrich.WithProperty("AppDomain", $"[{DomainMetadata.AppDomainId} {DomainMetadata.AppDomainName}]");
                    loggerConfiguration.Enrich.WithProperty("TracerVersion", TracerConstants.AssemblyVersion);
                }
                catch
                {
                    // At all costs, make sure the logger works when possible.
                }

                InternalLogger = loggerConfiguration.CreateLogger();
                SharedLogger   = new DatadogSerilogLogger(InternalLogger, defaultRateLimiter);

                var             rate        = GetRateLimit();
                ILogRateLimiter rateLimiter = rate == 0
                    ? new NullLogRateLimiter()
                    : new LogRateLimiter(rate);

                SharedLogger = new DatadogSerilogLogger(InternalLogger, rateLimiter);
            }
            catch
            {
                // Don't let this exception bubble up as this logger is for debugging and is non-critical
            }
        }
示例#24
0
        internal static async Task Main(string[] args)
        {
            Console.WriteLine(@"
                                   __  __           __        
 /'\_/`\                          /\ \/\ \         /\ \       
/\      \     __      ____    ____\ \ \_\ \  __  __\ \ \____  
\ \ \__\ \  /'__`\   /',__\  /',__\\ \  _  \/\ \/\ \\ \ '__`\ 
 \ \ \_/\ \/\ \L\.\_/\__, `\/\__, `\\ \ \ \ \ \ \_\ \\ \ \L\ \
  \ \_\\ \_\ \__/.\_\/\____/\/\____/ \ \_\ \_\ \____/ \ \_,__/
   \/_/ \/_/\/__/\/_/\/___/  \/___/   \/_/\/_/\/___/   \/___/ 
                                                              
                                                              
");

            var version = Assembly.GetEntryAssembly()?.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion;

            Console.WriteLine($"GitHub modification en masse - {version ?? "1.0"}");

            var levelSwitch = new LoggingLevelSwitch();

            var loggerConfiguration = new LoggerConfiguration()
                                      .MinimumLevel.ControlledBy(levelSwitch)
                                      .WriteTo.Console();

            var rootCommand = new RootCommand
            {
                new Option <string?>("--token", () => null, "Set output to be verbose"),
                new Option <string?>(new[] { "--org", "-o" }, () => null, "Set organisation to use for all requests"),
                new Option <string>(new[] { "--product-header" }, () => "mass-hub", "Optionally set a custom product header used when interacting with GitHub API"),
                new Option <bool>("--verbose", () => false, "Set output to be verbose"),
                new Option <string?>("--log-file", () => null, "Path to log file"),
            };

            rootCommand.Description = "MassHub - GitHub Management en masse";

            rootCommand.Handler = CommandHandler.Create <string?, string?, string, bool, string?>(async(token, org, productHeader, verbose, logFile) =>
            {
                if (verbose)
                {
                    levelSwitch.MinimumLevel = LogEventLevel.Debug;
                }

                if (!string.IsNullOrWhiteSpace(logFile))
                {
                    loggerConfiguration
                    .WriteTo.File(logFile !);
                }

                Log.Logger = loggerConfiguration.CreateLogger();

                while (string.IsNullOrWhiteSpace(token))
                {
                    Log.Debug("GitHub token not provided during argument compile");

                    Console.WriteLine("GitHub token not provided, please provide a token now to use for authentication, find a token at: https://github.com/settings/tokens");

                    token = Console.ReadLine();
                }

                while (string.IsNullOrWhiteSpace(org))
                {
                    Log.Debug("GitHub organisation not provided during argument compile");

                    Console.WriteLine("GitHub organisation not provided, please provide the name of the organisation to use for all requests");

                    org = Console.ReadLine();
                }

                Log.Debug("Starting GitHub client with provided options");

                Log.Information("Contacting GitHub with provided token under product header {Header}", productHeader);

                await RunGitHubService(token !, productHeader, org !);
            });

            await rootCommand.InvokeAsync(args);
        }
示例#25
0
        public static SlackWebhookClient CreateClient()
        {
            var loggerConfiguration = new LoggerConfiguration().WriteTo.Trace();
            var loggerFactory       = LoggerFactory.Create(builder => builder.AddSerilog(loggerConfiguration.CreateLogger()));
            var logger        = loggerFactory.CreateLogger <HttpClient>();
            var configuration = new ConfigurationBuilder().AddUserSecrets <SlackWebhookClientAttachmentTests>().Build();

            return(new SlackWebhookClient(new HttpClient(new LoggingHandler(logger))
            {
                BaseAddress = new Uri(configuration["SlackWebhookURL"])
            }));
        }
示例#26
0
        public static ILogger CreateLogger(EnvironmentSettings settings, string devLogFolder, string opsLogFolder)
        {
            var config = new LoggerConfiguration()
                         .WriteTo.File(new CompactJsonFormatter(), Path.Combine(devLogFolder, "events-.json"),
                                       restrictedToMinimumLevel: (LogEventLevel)settings.FileLogSettings.MinimumLogLevel,
                                       retainedFileCountLimit: settings.FileLogSettings.RetainSettings.InfoFileCount,
                                       rollingInterval: RollingInterval.Day,
                                       encoding: Encoding.UTF8)

                         .WriteTo.File(Path.Combine(devLogFolder, "2-info-.txt"),
                                       restrictedToMinimumLevel: LogEventLevel.Information,
                                       retainedFileCountLimit: settings.FileLogSettings.RetainSettings.InfoFileCount,
                                       outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                       rollingInterval: RollingInterval.Day,
                                       formatProvider: CultureInfo.InvariantCulture,
                                       encoding: Encoding.UTF8)

                         .WriteTo.File(Path.Combine(devLogFolder, "3-warning-.txt"),
                                       restrictedToMinimumLevel: LogEventLevel.Warning,
                                       retainedFileCountLimit: settings.FileLogSettings.RetainSettings.ImportantFileCount,
                                       outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                       rollingInterval: RollingInterval.Day,
                                       formatProvider: CultureInfo.InvariantCulture,
                                       encoding: Encoding.UTF8)

                         .WriteTo.File(Path.Combine(devLogFolder, "4-error-.txt"),
                                       restrictedToMinimumLevel: LogEventLevel.Error,
                                       retainedFileCountLimit: settings.FileLogSettings.RetainSettings.ImportantFileCount,
                                       outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                       rollingInterval: RollingInterval.Day,
                                       formatProvider: CultureInfo.InvariantCulture,
                                       encoding: Encoding.UTF8)

                         .WriteTo.File(Path.Combine(devLogFolder, "5-fatal-.txt"),
                                       restrictedToMinimumLevel: LogEventLevel.Fatal,
                                       retainedFileCountLimit: settings.FileLogSettings.RetainSettings.ImportantFileCount,
                                       outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                       rollingInterval: RollingInterval.Day,
                                       formatProvider: CultureInfo.InvariantCulture,
                                       encoding: Encoding.UTF8);

            if (settings.FileLogSettings.MinimumLogLevel <= LogSeverity.Debug)
            {
                config.WriteTo.File(Path.Combine(devLogFolder, "1-debug-.txt"),
                                    restrictedToMinimumLevel: LogEventLevel.Debug,
                                    retainedFileCountLimit: settings.FileLogSettings.RetainSettings.LowFileCount,
                                    outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                    rollingInterval: RollingInterval.Day,
                                    formatProvider: CultureInfo.InvariantCulture,
                                    encoding: Encoding.UTF8);
            }

            if (settings.FileLogSettings.MinimumLogLevel <= LogSeverity.Verbose)
            {
                config.WriteTo.File(Path.Combine(devLogFolder, "0-verbose-.txt"),
                                    restrictedToMinimumLevel: LogEventLevel.Verbose,
                                    retainedFileCountLimit: settings.FileLogSettings.RetainSettings.LowFileCount,
                                    outputTemplate: "{Timestamp:HH:mm:ss.fff zzz} [{Level:u3}] {Message:l} {NewLine}{Exception}",
                                    rollingInterval: RollingInterval.Day,
                                    formatProvider: CultureInfo.InvariantCulture,
                                    encoding: Encoding.UTF8);
            }

            config.WriteTo.Sink(new ConsoleSink("{Timestamp:HH:mm:ss.fff} [{Level}] {Message} {Properties}{NewLine}{Exception}"),
                                (LogEventLevel)settings.ConsoleLogSettings.MinimumLogLevel);

            config = config.MinimumLevel.Is(Debugger.IsAttached ? LogEventLevel.Verbose : LogEventLevel.Debug);

            if (!string.IsNullOrEmpty(settings.SeqSettings.Url))
            {
                config = config.WriteTo.Seq(settings.SeqSettings.Url, apiKey: settings.SeqSettings.ApiKey);
            }

            return(config.CreateLogger());
        }
 /// <summary>
 /// Register the Serilog service with a custom configuration.
 /// </summary>
 public static IServiceCollection AddSerilogServices(this IServiceCollection services, LoggerConfiguration configuration)
 {
     Log.Logger = configuration.CreateLogger();
     AppDomain.CurrentDomain.ProcessExit += (s, e) => Log.CloseAndFlush();
     return(services.AddSingleton(Log.Logger));
 }
示例#28
0
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var jobOptions = new JobRunnerOptions(args);

            Console.Title = jobOptions.JobName != null ? $"Exceptionless {jobOptions.JobName} Job" : "Exceptionless Jobs";

            string environment = Environment.GetEnvironmentVariable("EX_AppMode");

            if (String.IsNullOrWhiteSpace(environment))
            {
                environment = "Production";
            }

            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
                         .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables("EX_")
                         .AddEnvironmentVariables("ASPNETCORE_")
                         .AddCommandLine(args)
                         .Build();

            var options = AppOptions.ReadFromConfiguration(config);

            var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config);

            if (!String.IsNullOrEmpty(options.ExceptionlessApiKey))
            {
                loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Information);
            }

            Log.Logger = loggerConfig.CreateLogger();
            var configDictionary = config.ToDictionary("Serilog");

            Log.Information("Bootstrapping Exceptionless {JobName} job(s) in {AppMode} mode ({InformationalVersion}) on {MachineName} with settings {@Settings}", jobOptions.JobName ?? "All", environment, options.InformationalVersion, Environment.MachineName, configDictionary);

            var builder = Host.CreateDefaultBuilder()
                          .UseEnvironment(environment)
                          .UseSerilog()
                          .ConfigureWebHostDefaults(webBuilder => {
                webBuilder
                .UseConfiguration(config)
                .Configure(app => {
                    app.UseSerilogRequestLogging(o => o.GetLevel = (context, duration, ex) => {
                        if (ex != null || context.Response.StatusCode > 499)
                        {
                            return(LogEventLevel.Error);
                        }

                        return(duration < 1000 && context.Response.StatusCode < 400 ? LogEventLevel.Debug : LogEventLevel.Information);
                    });
                })
                .Configure(app => {
                    Bootstrapper.LogConfiguration(app.ApplicationServices, options, app.ApplicationServices.GetService <ILogger <Program> >());

                    if (!String.IsNullOrEmpty(options.ExceptionlessApiKey) && !String.IsNullOrEmpty(options.ExceptionlessServerUrl))
                    {
                        app.UseExceptionless(ExceptionlessClient.Default);
                    }

                    app.UseHealthChecks("/health", new HealthCheckOptions {
                        Predicate = hcr => !String.IsNullOrEmpty(jobOptions.JobName) ? hcr.Tags.Contains(jobOptions.JobName) : hcr.Tags.Contains("AllJobs")
                    });

                    app.UseHealthChecks("/ready", new HealthCheckOptions {
                        Predicate = hcr => hcr.Tags.Contains("Critical")
                    });

                    app.UseWaitForStartupActionsBeforeServingRequests();
                    app.Use((context, func) => context.Response.WriteAsync($"Running Job: {jobOptions.JobName}"));
                });
            })
                          .ConfigureServices((ctx, services) => {
                AddJobs(services, jobOptions);
                services.AddAppOptions(options);

                Bootstrapper.RegisterServices(services);
                Insulation.Bootstrapper.RegisterServices(services, options, true);
            });

            if (!String.IsNullOrEmpty(options.MetricOptions.Provider))
            {
                ConfigureMetricsReporting(builder, options.MetricOptions);
            }

            return(builder);
        }
示例#29
0
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            string environment = Environment.GetEnvironmentVariable("AppMode");

            if (String.IsNullOrWhiteSpace(environment))
            {
                environment = "Production";
            }

            string currentDirectory = Directory.GetCurrentDirectory();
            var    config           = new ConfigurationBuilder()
                                      .SetBasePath(currentDirectory)
                                      .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
                                      .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true)
                                      .AddEnvironmentVariables()
                                      .AddCommandLine(args)
                                      .Build();

            var settings = Settings.ReadFromConfiguration(config, environment);

            var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config);

            if (!String.IsNullOrEmpty(settings.ExceptionlessApiKey))
            {
                loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Verbose);
            }

            Log.Logger = loggerConfig.CreateLogger();

            Log.Information("Bootstrapping {AppMode} mode API ({InformationalVersion}) on {MachineName} using {@Settings} loaded from {Folder}", environment, Settings.Current.InformationalVersion, Environment.MachineName, Settings.Current, currentDirectory);

            bool useApplicationInsights = !String.IsNullOrEmpty(Settings.Current.ApplicationInsightsKey);

            var builder = WebHost.CreateDefaultBuilder(args)
                          .UseEnvironment(environment)
                          .UseKestrel(c => {
                c.AddServerHeader = false;
                if (Settings.Current.MaximumEventPostSize > 0)
                {
                    c.Limits.MaxRequestBodySize = Settings.Current.MaximumEventPostSize;
                }
            })
                          .UseSerilog(Log.Logger)
                          .SuppressStatusMessages(true)
                          .UseConfiguration(config)
                          .ConfigureServices(s => {
                if (useApplicationInsights)
                {
                    s.AddSingleton <ITelemetryInitializer, ExceptionlessTelemetryInitializer>();
                    s.AddHttpContextAccessor();
                    s.AddApplicationInsightsTelemetry();
                }
                s.AddSingleton(settings);
            })
                          .UseStartup <Startup>();

            if (useApplicationInsights)
            {
                builder.UseApplicationInsights(Settings.Current.ApplicationInsightsKey);
            }

            if (settings.EnableMetricsReporting)
            {
                settings.MetricsConnectionString = MetricsConnectionString.Parse(settings.MetricsConnectionString?.ConnectionString);
                ConfigureMetricsReporting(builder);
            }

            return(builder);
        }
示例#30
0
        public static int Main(string[] args)
        {
            var loggerConfiguration = new LoggerConfiguration()
                                      .MinimumLevel.Debug()
                                      .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                                      .Enrich.FromLogContext()
                                      .WriteTo.Console();

            var appConfiguration = new ConfigurationBuilder()
                                   .SetBasePath(Directory.GetCurrentDirectory())
                                   .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                   .AddJsonFile(
                $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json",
                optional: true)
                                   .Build();

            string logToFile = appConfiguration["Logging:LogToFile"];

            if (!string.IsNullOrWhiteSpace(logToFile))
            {
                loggerConfiguration.WriteTo.File(logToFile,
                                                 fileSizeLimitBytes: 1_000_000,
                                                 rollOnFileSizeLimit: true,
                                                 shared: true,
                                                 flushToDiskInterval: TimeSpan.FromSeconds(1));
            }

            Log.Logger = loggerConfiguration.CreateLogger();

            try
            {
                Log.Information("Starting web host");

                var host = BuildWebHost(args);
                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;
                    try
                    {
                        var context = services.GetRequiredService <ApplicationDbContext>();
                        DbInitializer.Initialize(context);
                    }
                    catch (Exception e)
                    {
                        Log.Fatal(e, "An Error occurred While seeding the database.");
                        return(1);
                    }
                }
                host.Run();

                return(0);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        static DatadogLogging()
        {
            // No-op for if we fail to construct the file logger
            SharedLogger =
                new LoggerConfiguration()
                .WriteTo.Sink <NullSink>()
                .CreateLogger();
            try
            {
                if (GlobalSettings.Source.DebugEnabled)
                {
                    LoggingLevelSwitch.MinimumLevel = LogEventLevel.Verbose;
                }

                var maxLogSizeVar = Environment.GetEnvironmentVariable(ConfigurationKeys.MaxLogFileSize);
                if (long.TryParse(maxLogSizeVar, out var maxLogSize))
                {
                    // No verbose or debug logs
                    MaxLogFileSize = maxLogSize;
                }

                var logDirectory = GetLogDirectory();

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                if (logDirectory == null)
                {
                    return;
                }

                var currentProcess = Process.GetCurrentProcess();
                // Ends in a dash because of the date postfix
                var managedLogPath = Path.Combine(logDirectory, $"dotnet-tracer-{currentProcess.ProcessName}-.log");

                var loggerConfiguration =
                    new LoggerConfiguration()
                    .Enrich.FromLogContext()
                    .MinimumLevel.ControlledBy(LoggingLevelSwitch)
                    .WriteTo.File(
                        managedLogPath,
                        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{Properties}{NewLine}",
                        rollingInterval: RollingInterval.Day,
                        rollOnFileSizeLimit: true,
                        fileSizeLimitBytes: MaxLogFileSize);

                try
                {
                    var currentAppDomain = AppDomain.CurrentDomain;
                    loggerConfiguration.Enrich.WithProperty("MachineName", currentProcess.MachineName);
                    loggerConfiguration.Enrich.WithProperty("ProcessName", currentProcess.ProcessName);
                    loggerConfiguration.Enrich.WithProperty("PID", currentProcess.Id);
                    loggerConfiguration.Enrich.WithProperty("AppDomainName", currentAppDomain.FriendlyName);
                }
                catch
                {
                    // At all costs, make sure the logger works when possible.
                }

                SharedLogger = loggerConfiguration.CreateLogger();
            }
            catch
            {
                // Don't let this exception bubble up as this logger is for debugging and is non-critical
            }
            finally
            {
                // Log some information to correspond with the app domain
                SharedLogger.Information(FrameworkDescription.Create().ToString());
            }
        }
        public static void Configure(IWebHostEnvironment env, LoggerOptions options = null)
        {
            options ??= new LoggerOptions
            {
                File = new FileOptions
                {
                    MinimumLogEventLevel = Serilog.Events.LogEventLevel.Debug,
                },
                Elasticsearch = new ElasticsearchOptions
                {
                    IsEnabled            = false,
                    MinimumLogEventLevel = Serilog.Events.LogEventLevel.Debug,
                },
            };

            var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;

            var logsPath = Path.Combine(env.ContentRootPath, "logs");

            Directory.CreateDirectory(logsPath);
            var loggerConfiguration = new LoggerConfiguration();

            loggerConfiguration = loggerConfiguration
                                  .MinimumLevel.Debug()
                                  .Enrich.WithMachineName()
                                  .Enrich.WithEnvironmentUserName()
                                  .Enrich.WithProperty("Assembly", assemblyName)
                                  .Enrich.WithProperty("Application", env.ApplicationName)
                                  .Enrich.WithProperty("EnvironmentName", env.EnvironmentName)
                                  .Enrich.WithProperty("ContentRootPath", env.ContentRootPath)
                                  .Enrich.WithProperty("WebRootPath", env.WebRootPath)
                                  .Filter.ByIncludingOnly((logEvent) =>
            {
                if (logEvent.Level >= options.File.MinimumLogEventLevel ||
                    logEvent.Level >= options.Elasticsearch.MinimumLogEventLevel)
                {
                    var sourceContext = logEvent.Properties.ContainsKey("SourceContext")
                             ? logEvent.Properties["SourceContext"].ToString()
                             : null;

                    if (sourceContext.Contains("ClassifiedAds."))
                    {
                        return(true);
                    }
                }

                return(false);
            })
                                  .WriteTo.File(Path.Combine(logsPath, "log.txt"),
                                                fileSizeLimitBytes: 10 * 1024 * 1024,
                                                rollOnFileSizeLimit: true,
                                                shared: true,
                                                flushToDiskInterval: TimeSpan.FromSeconds(1),
                                                outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
                                                restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

            if (options.Elasticsearch != null && options.Elasticsearch.IsEnabled)
            {
                loggerConfiguration = loggerConfiguration
                                      .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(options.Elasticsearch.Host))
                {
                    MinimumLogEventLevel        = options.Elasticsearch.MinimumLogEventLevel,
                    AutoRegisterTemplate        = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
                    IndexFormat = options.Elasticsearch.IndexFormat + "-{0:yyyy.MM.dd}",
                    // BufferBaseFilename = Path.Combine(env.ContentRootPath, "logs", "buffer"),
                    InlineFields     = true,
                    EmitEventFailure = EmitEventFailureHandling.WriteToFailureSink,
                    FailureSink      = new FileSink(Path.Combine(logsPath, "elasticsearch-failures.txt"), new JsonFormatter(), null)
                });
            }

            Log.Logger = loggerConfiguration.CreateLogger();
        }
示例#33
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              IApiVersionDescriptionProvider provider)
        {
            app.UseHealthCheck("/HealthCheck", null, new TimeSpan(0, 0, 10));

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            //-------------------------------------------------------serilog 配置
            MVCLogOptions mvcLogOptions = new MVCLogOptions()
            {
                LogPath    = "D:\\LogFiles_AuthAdmin_API",//Configuration[nameof(AuthLogOptions.LogPath)],
                PathFormat = "{Date}.log"
            };
            var serilog = new LoggerConfiguration()
                          .MinimumLevel.Debug()
                          .Enrich.FromLogContext()
                          .WriteTo.RollingFile(Path.Combine(mvcLogOptions.LogPath, mvcLogOptions.PathFormat),
                                               outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message}{NewLine}{Exception}");

            MVCLogOptions.EnsurePreConditions(mvcLogOptions);

            loggerFactory.AddSerilog(serilog.CreateLogger());

            // Ensure any buffered events are sent at shutdown 日志的生命周期
            IApplicationLifetime appLifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));

            if (appLifetime != null)
            {
                appLifetime.ApplicationStopped.Register(Serilog.Log.CloseAndFlush);
            }
            app.UseAuthLog(mvcLogOptions);//这个中间件用作记录请求中的过程日志
            //---------------------------------------------------serilog 配置

            //app.UsePetapoco();//use  Petapocomiddleware
            app.UseDapper(); //----dapper

            if (env.IsDevelopment())
            {
                //开发环境异常处理
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //生产环境异常处理
                app.UseExceptionHandler("/Shared/Error");
            }
            //使用静态文件
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
            });

            ////Session
            //app.UseSession(new SessionOptions() { IdleTimeout = TimeSpan.FromMinutes(30) });


            SeedData.Initialize(app.ApplicationServices); //EF初始化数据

            app.UseMvcWithDefaultRoute();

            app.UseSwagger()
            .UseSwaggerUI(options =>
            {
                // build a swagger endpoint for each discovered API version
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
                }
            });
            //往zookeeper注册服务
            AuthAPIRegister.registerAuthAPIHostPort(Configuration.GetSection("UrlConfig").GetValue <string>("ZooKeeperList"));

            //注册到eureka,springcloud服务发现的注册
            //app.UseDiscoveryClient();
        }
 public SerilogLoggerFactory(LoggerConfiguration loggerConfiguration)
     : this(loggerConfiguration.CreateLogger())
 {
 }
示例#35
0
        public override void Initialize()
        {
            _configuration = AudioSenseiConfiguration.LoadOrCreate(Path.Combine(ApplicationDataPath, "config.json"));

            string directory      = Path.Combine(ApplicationDataPath, "logs");
            string latestLogPath  = Path.Combine(directory, "latest.log");
            string rollingLogPath = Path.Combine(directory, $"log-{DateTimeOffset.Now.ToString(_configuration.General.LogTimeFormat)}.log.gz");

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            try
            {
                if (File.Exists(latestLogPath))
                {
                    File.Delete(latestLogPath);
                }
            }
            catch (Exception ex)
            {
                latestLogPath = Path.Combine(directory, $"{Environment.ProcessId}-latest.log");
                if (File.Exists(latestLogPath))
                {
                    File.Delete(latestLogPath);
                }
                Log.Information(ex, $"Failed to delete the latest log, using {latestLogPath} instead");
            }

            var loggerConfiguration = new LoggerConfiguration()
                                      .MinimumLevel.Is(_configuration.General.LoggerMinimumLevel)
                                      .WriteTo.File(latestLogPath, outputTemplate: _configuration.General.LogTemplate)
                                      .WriteTo.File(rollingLogPath, outputTemplate: _configuration.General.LogTemplate, buffered: true, hooks: new GZipHooks());

            if (_configuration.General.EnableMessageBoxSink)
            {
                loggerConfiguration.WriteTo.MessageBox(restrictedToMinimumLevel: _configuration.General.MessageBoxSinkMinimumLevel);
            }

            var logger = loggerConfiguration.CreateLogger();

            Avalonia.Logging.Logger.Sink = new AvaloniaSerilogSink(logger);
            Log.Logger    = logger;
            Program.Exit += Log.CloseAndFlush;

            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                string message;
                if (args.ExceptionObject is Exception ex)
                {
                    message = $"Caught an unhandled {ex.GetType().Name} exception";
                    if (args.IsTerminating)
                    {
                        Log.Fatal(ex, $"{message}, terminating the program");
                        Program.TriggerExit();
                    }
                    else
                    {
                        Log.Error($"{message}, not terminating the program");
                    }
                }
                else
                {
                    message = "Caught an unidentified unhandled exception";

                    if (args.IsTerminating)
                    {
                        Log.Fatal($"{message}, terminating the program");
                        Program.TriggerExit();
                    }
                    else
                    {
                        Log.Error($"{message}, not terminating the program");
                    }
                }
            };

            AppDomain.CurrentDomain.ProcessExit += (sender, args) => Program.TriggerExit();

#if LINUX
            new Thread(() =>
            {
                UnixSignal[] signals =
                {
                    new UnixSignal(Signum.SIGINT),  // CTRL + C pressed
                    new UnixSignal(Signum.SIGTERM), // Sending KILL
                    new UnixSignal(Signum.SIGUSR1),
                    new UnixSignal(Signum.SIGUSR2),
                    new UnixSignal(Signum.SIGHUP)   // Terminal is closed
                };
                // Blocking operation with infinite expectation of any signal
                UnixSignal.WaitAny(signals, -1);
                Program.TriggerExit();
            })
            {
                IsBackground = true, Priority = ThreadPriority.BelowNormal
            }.Start();
#endif

            Log.Information("Opening a new instance of AudioSensei");

            AvaloniaXamlLoader.Load(this);
        }
示例#36
0
文件: Startup.cs 项目: vlod11/stu-api
        public void ConfigureServices(IServiceCollection services)
        {
            // TODO: move logging configuraitions in
            string elastisearchUri;

            if (_configuration["usedocker"] == "true")
            {
                services.AddDockerDbContext(_configuration);
                elastisearchUri = _configuration["ElasticConfiguration:UriDocker"];
            }
            else
            {
                services.AddDebugDbContext(_configuration);
                elastisearchUri = _configuration["ElasticConfiguration:Uri"];
            }

            var serilogConfiguration = new LoggerConfiguration()
                                       .Enrich.FromLogContext()
                                       .Enrich.WithExceptionDetails()
                                       .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elastisearchUri))
            {
                AutoRegisterTemplate = true,
            });

            Log.Logger = serilogConfiguration.CreateLogger();
            AppDomain.CurrentDomain.DomainUnload += (o, e) => Log.CloseAndFlush();

            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddDebug();
                loggingBuilder.AddSerilog();
            });

            services.AddRouting(options => options.LowercaseUrls = true);

            services.Configure <SendGridOptions>(_configuration.GetSection("SendGrid"));
            services.Configure <FilesOptions>(_configuration.GetSection("Files"));
            services.Configure <UrlsOptions>(_configuration.GetSection("Urls"));

            services.Configure <MvcOptions>(options =>
            {
                options.Filters.Add(new CorsAuthorizationFilterFactory("EnableCORS"));
            });

            services.AddCors(options =>
            {
                options.AddPolicy("EnableCORS", builder =>
                {
                    builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build();
                });
            });

            services.AddRepositories();
            services.AddTransient <IUnitOfWork, UnitOfWork>();

            services.AddTransient(typeof(SeedDatabase));

            services.AddServiceLayer();

            services.AddTransient <IEmailTemplatePicker, MemoryEmailTemplatePicker>();
            services.AddTransient <IFolderHelper, FolderHelper>();
            services.AddTransient <IDateHelper, DateHelper>();
            services.AddTransient <IEncryptHelper, EncryptHelper>();

            services.AddTransient <IServiceResultMapper, ServiceResultMapper>();
            services.AddTransient <ITokenService, TokenService>();

            services.AddSwagger(_configuration);

            Mapper.Reset();
            services.AddAutoMapper(typeof(Startup).Assembly);

            services.AddJwtAuth(_configuration);

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddApiVersioning();

            services.AddApiVersioning(
                o =>
            {
                o.AssumeDefaultVersionWhenUnspecified = true;
                o.DefaultApiVersion = new ApiVersion(1, 0);
            });
        }
示例#37
0
        public static AppBuilder AddConsoleSerilogLogging(this AppBuilder instance, bool includeDistributedTracing = false, bool addDebug = false)
        {
            LoggingConstants.IncludeDistributedTracing = includeDistributedTracing;

            var inMemoryConfigStore = ReflectionHelper
                                      .GetNonPublicInstancePropertyValue <Dictionary <string, string> >(instance, "InMemoryConfigStore");

            inMemoryConfigStore["Serilog:MinimumLevel:Default"]                  = "Information";
            inMemoryConfigStore["Serilog:MinimumLevel:Override:Microsoft"]       = "Warning";
            inMemoryConfigStore["Serilog:MinimumLevel:Override:System"]          = "Warning";
            inMemoryConfigStore["Serilog:MinimumLevel:Override:PivotalServices"] = "Warning";
            inMemoryConfigStore["Serilog:MinimumLevel:Override:Steeltoe"]        = "Warning";

            inMemoryConfigStore["Serilog:Using:0"] = "Serilog.Sinks.Console";
            inMemoryConfigStore["Serilog:Using:1"] = "Serilog.Sinks.Debug";

            inMemoryConfigStore["Serilog:WriteTo:0:Name"] = "Console";
            inMemoryConfigStore["Serilog:WriteTo:1:Name"] = "Debug";

            if (includeDistributedTracing)
            {
                inMemoryConfigStore["Serilog:WriteTo:0:Args:outputTemplate"] = "[{Level}]{CorrelationContext}=> RequestPath:{RequestPath} => {SourceContext} => {Message} {Exception}{NewLine}";
                inMemoryConfigStore["Serilog:WriteTo:1:Args:outputTemplate"] = "[{Level}]{CorrelationContext}=> RequestPath:{RequestPath} => {SourceContext} => {Message} {Exception}{NewLine}";

                inMemoryConfigStore["management:tracing:AlwaysSample"]        = "true";
                inMemoryConfigStore["management:tracing:UseShortTraceIds"]    = "false";
                inMemoryConfigStore["management:tracing:EgressIgnorePattern"] = "/api/v2/spans|/v2/apps/.*/permissions|/eureka/.*|/oauth/.*";
            }
            else
            {
                inMemoryConfigStore["Serilog:WriteTo:0:Args:outputTemplate"] = "[{Level}]RequestPath:{RequestPath} => {SourceContext} => {Message} {Exception}{NewLine}";
                inMemoryConfigStore["Serilog:WriteTo:1:Args:outputTemplate"] = "[{Level}]RequestPath:{RequestPath} => {SourceContext} => {Message} {Exception}{NewLine}";
            }

            var handlers = ReflectionHelper
                           .GetNonPublicInstanceFieldValue <List <Type> >(instance, "Handlers");

            handlers.Add(typeof(GlobalErrorHandler));

            if (includeDistributedTracing)
            {
                handlers.Add(typeof(InboundBeginRequestObserverHandler));
                handlers.Add(typeof(InboundEndRequestObserverHandler));
                handlers.Add(typeof(InboundErrorRequestObserverHandler));
                handlers.Add(typeof(ScopedLoggingHandler));
            }

            ReflectionHelper
            .GetNonPublicInstanceFieldValue <List <Action <HostBuilderContext, ILoggingBuilder> > >(instance, "ConfigureLoggingDelegates")
            .Add((builderContext, loggingBuilder) =>
            {
                loggingBuilder.ClearProviders();

                var loggerConfiguration = new LoggerConfiguration()
                                          .ReadFrom.Configuration(builderContext.Configuration)
                                          .Enrich.FromLogContext()
                                          .Filter.ByExcluding("Contains(@Message, 'cloudfoundryapplication')");

                var serilogOptions = new SerilogOptions(builderContext.Configuration);
                var levelSwitch    = new LoggingLevelSwitch(serilogOptions.MinimumLevel.Default);
                loggerConfiguration.MinimumLevel.ControlledBy(levelSwitch);

                var logger = loggerConfiguration.CreateLogger();

                Log.Logger = logger;

                loggingBuilder.Services.AddSingleton <IDynamicLoggerProvider>(sp => new SerilogDynamicProvider(sp.GetRequiredService <IConfiguration>(), serilogOptions, logger, levelSwitch));
                loggingBuilder.Services.AddSingleton <ILoggerFactory>(sp => new SerilogDynamicLoggerFactory(sp.GetRequiredService <IDynamicLoggerProvider>()));

                if (includeDistributedTracing)
                {
                    loggingBuilder.Services.AddDefaultDiagnosticsDependencies(builderContext.Configuration);
                }

                if (addDebug)
                {
                    loggingBuilder.AddDebug();
                }
            });

            instance.AddDefaultConfigurations();
            instance.AddConfigServer();

            return(instance);
        }
 public void ELKIntegration()
 {
     var configuration = new LoggerConfiguration().WriteTo.ELKBulk(new SinkOptions { Url = "http://vm-elk:8080/logs/", IndexTemplate = "test-", Period = TimeSpan.FromSeconds(1)});
     Debugging.SelfLog.Out = Console.Out;
     var logger = configuration.CreateLogger();
     Log.Logger = logger;
     Log.Information("Test record");
     Log.Error(new Exception("Test exception"), "Exception test");
     ((IDisposable)logger).Dispose();
 }
示例#39
0
        public void ConfigureServices(IServiceCollection services)
        {
            var formatElastic = Configuration.GetValue("FormatLogsInElasticFormat", false);

            // Logger configuration
            var logConf = new LoggerConfiguration()
                          .ReadFrom.Configuration(Configuration);

            if (formatElastic)
            {
                var logFormatter = new ExceptionAsObjectJsonFormatter(renderMessage: true);
                logConf.WriteTo.Console(logFormatter);
            }
            else
            {
                logConf.WriteTo.Console();
            }

            Log.Logger = logConf.CreateLogger();

            services.AddMvc().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            var auth0Section = Configuration.GetSection("Auth0");

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority = auth0Section.GetValue <string>("TenantDomain");
                options.Audience  = "https://api.gigdata.openplatforms.org";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"
                };
                options.Events = new JwtBearerEvents
                {
                    OnTokenValidated = async context =>
                    {
                        if (string.IsNullOrEmpty(context.Principal.Identity.Name))
                        {
                            return;
                        }

                        var cache      = context.HttpContext.RequestServices.GetRequiredService <IMemoryCache>();
                        var cacheEntry = cache.Get(context.Principal.Identity.Name);
                        if (cacheEntry != null)
                        {
                            return;
                        }

                        var cacheEntryOptions = new MemoryCacheEntryOptions()
                                                // Keep in cache for this time, reset time if accessed.
                                                .SetAbsoluteExpiration(TimeSpan.FromSeconds(60)).SetSize(1);
                        cache.Set(context.Principal.Identity.Name, true, cacheEntryOptions);

                        var userManager   = context.HttpContext.RequestServices.GetRequiredService <IUserManager>();
                        var documentStore = context.HttpContext.RequestServices.GetRequiredService <IDocumentStore>();
                        using var session = documentStore.OpenAsyncSession();

                        var auth0Client = context.HttpContext.RequestServices.GetRequiredService <Auth0ManagementApiHttpClient>();
                        var userInfo    = await auth0Client.GetUserProfile(context.Principal.Identity.Name);

                        var user  = await userManager.GetOrCreateUserIfNotExists(context.Principal.Identity.Name, session);
                        user.Name = userInfo.Name;

                        var userEmailState = UserEmailState.Unverified;
                        if (userInfo.EmailVerified)
                        {
                            userEmailState = UserEmailState.Verified;
                        }

                        var existingUserEmail = user.UserEmails.SingleOrDefault(ue =>
                                                                                string.Equals(ue.Email, userInfo.Email, StringComparison.InvariantCultureIgnoreCase));

                        if (existingUserEmail == null) //email does not exist at all
                        {
                            user.UserEmails.Add(new UserEmail(userInfo.Email.ToLowerInvariant(), userEmailState));
                        }
                        else if (existingUserEmail.UserEmailState != UserEmailState.Verified &&
                                 userEmailState == UserEmailState.Verified) //email has been verified through Auth0
                        {
                            existingUserEmail.SetEmailState(UserEmailState.Verified);
                        }

                        if (session.Advanced.HasChanges)
                        {
                            await session.SaveChangesAsync();
                        }
                    }
                };
            });

            var rabbitMqConnectionString = Configuration.GetConnectionString("RabbitMq");

            services.AddRebus(c =>
                              c
                              .Transport(t =>
                                         t.UseRabbitMqAsOneWayClient(rabbitMqConnectionString))
                              .Timeouts(t => t.StoreInMemory())
                              .Routing(r => r.TypeBased()
                                       .Map <FetchDataForPlatformConnectionMessage>("platformdatafetcher.input")
                                       .Map <PlatformConnectionUpdateNotificationMessage>("platformdatafetcher.input"))
                              );

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
示例#40
0
        public static IServiceProvider GetServiceProvider()
        {
            AppDomain.CurrentDomain.SetDataDirectory();

            string environment = Environment.GetEnvironmentVariable("AppMode");

            if (String.IsNullOrWhiteSpace(environment))
            {
                environment = "Production";
            }

            string currentDirectory = AppContext.BaseDirectory;
            var    config           = new ConfigurationBuilder()
                                      .SetBasePath(currentDirectory)
                                      .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
                                      .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true)
                                      .AddEnvironmentVariables()
                                      .Build();

            var settings = Settings.ReadFromConfiguration(config, environment);

            settings.DisableIndexConfiguration = true;

            var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config);

            if (!String.IsNullOrEmpty(Settings.Current.ExceptionlessApiKey) && !String.IsNullOrEmpty(Settings.Current.ExceptionlessServerUrl))
            {
                var client = ExceptionlessClient.Default;
                client.Configuration.SetDefaultMinLogLevel(LogLevel.Warn);
                client.Configuration.UseLogger(new SelfLogLogger());
                client.Configuration.SetVersion(Settings.Current.Version);
                client.Configuration.UseInMemoryStorage();

                if (String.IsNullOrEmpty(Settings.Current.InternalProjectId))
                {
                    client.Configuration.Enabled = false;
                }

                client.Configuration.ServerUrl = Settings.Current.ExceptionlessServerUrl;
                client.Startup(Settings.Current.ExceptionlessApiKey);

                loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Verbose);
            }

            Log.Logger = loggerConfig.CreateLogger();
            Log.Information("Bootstrapping {AppMode} mode job ({InformationalVersion}) on {MachineName} using {@Settings} loaded from {Folder}", environment, Settings.Current.InformationalVersion, Environment.MachineName, Settings.Current, currentDirectory);

            var services = new ServiceCollection();

            services.AddLogging(b => b.AddSerilog(Log.Logger));
            services.AddSingleton(settings);
            Core.Bootstrapper.RegisterServices(services);
            Bootstrapper.RegisterServices(services, true);

            var container = services.BuildServiceProvider();

            Core.Bootstrapper.LogConfiguration(container, settings, container.GetRequiredService <ILoggerFactory>());
            if (Settings.Current.EnableBootstrapStartupActions)
            {
                container.RunStartupActionsAsync().GetAwaiter().GetResult();
            }

            return(container);
        }
        protected void WhenLoggerCreated()
        {
            var loggerConfig = new LoggerConfiguration()
                .WriteTo.Trace()
                .MinimumLevel.Verbose();

            loggerConfig.WriteTo.AmazonKinesis(
                kinesisClient: Client,
                streamName: StreamName,
                period: ThrottleTime,
                bufferBaseFilename: BufferBaseFileName,
                onLogSendError: OnLogSendError
            );

            Logger = loggerConfig.CreateLogger();
        }