public static void UseDefaultMiddleware(this IApplicationBuilder app,
                                                IWebHostEnvironment env, GlobalSettings globalSettings)
        {
            string GetHeaderValue(HttpContext httpContext, string header)
            {
                if (httpContext.Request.Headers.ContainsKey(header))
                {
                    return(httpContext.Request.Headers[header]);
                }
                return(null);
            }

            // Add version information to response headers
            app.Use(async(httpContext, next) =>
            {
                using (LogContext.PushProperty("IPAddress", httpContext.GetIpAddress(globalSettings)))
                    using (LogContext.PushProperty("UserAgent", GetHeaderValue(httpContext, "user-agent")))
                        using (LogContext.PushProperty("DeviceType", GetHeaderValue(httpContext, "device-type")))
                            using (LogContext.PushProperty("Origin", GetHeaderValue(httpContext, "origin")))
                            {
                                httpContext.Response.OnStarting((state) =>
                                {
                                    httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion());
                                    return(Task.FromResult(0));
                                }, null);
                                await next.Invoke();
                            }
            });
        }
Beispiel #2
0
        public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
        {
            var usePostgreSql = CoreHelpers.SettingHasValue(globalSettings.PostgreSql?.ConnectionString);
            var useEf         = usePostgreSql;

            if (useEf)
            {
                services.AddAutoMapper(typeof(EntityFrameworkRepos.UserRepository));
                services.AddDbContext <EntityFrameworkRepos.DatabaseContext>(options =>
                {
                    if (usePostgreSql)
                    {
                        options.UseNpgsql(globalSettings.PostgreSql.ConnectionString);
                    }
                });
                services.AddSingleton <IUserRepository, EntityFrameworkRepos.UserRepository>();
                //services.AddSingleton<ICipherRepository, EntityFrameworkRepos.CipherRepository>();
                //services.AddSingleton<IOrganizationRepository, EntityFrameworkRepos.OrganizationRepository>();
            }
            else
            {
                services.AddSingleton <IUserRepository, SqlServerRepos.UserRepository>();
                services.AddSingleton <ICipherRepository, SqlServerRepos.CipherRepository>();
                services.AddSingleton <IDeviceRepository, SqlServerRepos.DeviceRepository>();
                services.AddSingleton <IGrantRepository, SqlServerRepos.GrantRepository>();
                services.AddSingleton <IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
                services.AddSingleton <IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
                services.AddSingleton <ICollectionRepository, SqlServerRepos.CollectionRepository>();
                services.AddSingleton <IFolderRepository, SqlServerRepos.FolderRepository>();
                services.AddSingleton <ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
                services.AddSingleton <IGroupRepository, SqlServerRepos.GroupRepository>();
                services.AddSingleton <IU2fRepository, SqlServerRepos.U2fRepository>();
                services.AddSingleton <IInstallationRepository, SqlServerRepos.InstallationRepository>();
                services.AddSingleton <IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
                services.AddSingleton <ITransactionRepository, SqlServerRepos.TransactionRepository>();
                services.AddSingleton <IPolicyRepository, SqlServerRepos.PolicyRepository>();
                services.AddSingleton <ISsoConfigRepository, SqlServerRepos.SsoConfigRepository>();
                services.AddSingleton <ISsoUserRepository, SqlServerRepos.SsoUserRepository>();
                services.AddSingleton <ISendRepository, SqlServerRepos.SendRepository>();
                services.AddSingleton <ITaxRateRepository, SqlServerRepos.TaxRateRepository>();
                services.AddSingleton <IEmergencyAccessRepository, SqlServerRepos.EmergencyAccessRepository>();
                services.AddSingleton <IProviderRepository, SqlServerRepos.ProviderRepository>();
                services.AddSingleton <IProviderUserRepository, SqlServerRepos.ProviderUserRepository>();
                services.AddSingleton <IProviderOrganizationRepository, SqlServerRepos.ProviderOrganizationRepository>();
                services.AddSingleton <IProviderOrganizationProviderUserRepository, SqlServerRepos.ProviderOrganizationProviderUserRepository>();
            }

            if (globalSettings.SelfHosted)
            {
                if (useEf)
                {
                    // TODO
                }
                else
                {
                    services.AddSingleton <IEventRepository, SqlServerRepos.EventRepository>();
                }
                services.AddSingleton <IInstallationDeviceRepository, NoopRepos.InstallationDeviceRepository>();
                services.AddSingleton <IMetaDataRepository, NoopRepos.MetaDataRepository>();
            }
            else
            {
                services.AddSingleton <IEventRepository, TableStorageRepos.EventRepository>();
                services.AddSingleton <IInstallationDeviceRepository, TableStorageRepos.InstallationDeviceRepository>();
                services.AddSingleton <IMetaDataRepository, TableStorageRepos.MetaDataRepository>();
            }
        }
        public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
        {
            // Required for UserService
            services.AddWebAuthn(globalSettings);

            services.AddSingleton <IPaymentService, StripePaymentService>();
            services.AddSingleton <IMailService, HandlebarsMailService>();
            services.AddSingleton <ILicensingService, LicensingService>();

            if (CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ConnectionString) &&
                CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ApplicationCacheTopicName))
            {
                services.AddSingleton <IApplicationCacheService, InMemoryServiceBusApplicationCacheService>();
            }
            else
            {
                services.AddSingleton <IApplicationCacheService, InMemoryApplicationCacheService>();
            }

            var awsConfigured = CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret);

            if (!globalSettings.SelfHosted && awsConfigured &&
                CoreHelpers.SettingHasValue(globalSettings.Mail?.PostalApiKey))
            {
                services.AddSingleton <IMailDeliveryService, MultiServiceMailDeliveryService>();
            }
            else if (awsConfigured)
            {
                services.AddSingleton <IMailDeliveryService, AmazonSesMailDeliveryService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host))
            {
                services.AddSingleton <IMailDeliveryService, MailKitSmtpMailDeliveryService>();
            }
            else
            {
                services.AddSingleton <IMailDeliveryService, NoopMailDeliveryService>();
            }

            services.AddSingleton <IPushNotificationService, MultiServicePushNotificationService>();
            if (globalSettings.SelfHosted &&
                CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
                globalSettings.Installation?.Id != null &&
                CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
            {
                services.AddSingleton <IPushRegistrationService, RelayPushRegistrationService>();
            }
            else if (!globalSettings.SelfHosted)
            {
                services.AddSingleton <IPushRegistrationService, NotificationHubPushRegistrationService>();
            }
            else
            {
                services.AddSingleton <IPushRegistrationService, NoopPushRegistrationService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage?.ConnectionString))
            {
                services.AddSingleton <IBlockIpService, AzureQueueBlockIpService>();
            }
            else if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
            {
                services.AddSingleton <IBlockIpService, AmazonSqsBlockIpService>();
            }
            else
            {
                services.AddSingleton <IBlockIpService, NoopBlockIpService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Mail.ConnectionString))
            {
                services.AddSingleton <IMailEnqueuingService, AzureQueueMailService>();
            }
            else
            {
                services.AddSingleton <IMailEnqueuingService, BlockingMailEnqueuingService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString))
            {
                services.AddSingleton <IEventWriteService, AzureQueueEventWriteService>();
            }
            else if (globalSettings.SelfHosted)
            {
                services.AddSingleton <IEventWriteService, RepositoryEventWriteService>();
            }
            else
            {
                services.AddSingleton <IEventWriteService, NoopEventWriteService>();
            }

            if (CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
            {
                services.AddSingleton <IAttachmentStorageService, AzureAttachmentStorageService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Attachment.BaseDirectory))
            {
                services.AddSingleton <IAttachmentStorageService, LocalAttachmentStorageService>();
            }
            else
            {
                services.AddSingleton <IAttachmentStorageService, NoopAttachmentStorageService>();
            }

            if (CoreHelpers.SettingHasValue(globalSettings.Send.ConnectionString))
            {
                services.AddSingleton <ISendFileStorageService, AzureSendFileStorageService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Send.BaseDirectory))
            {
                services.AddSingleton <ISendFileStorageService, LocalSendStorageService>();
            }
            else
            {
                services.AddSingleton <ISendFileStorageService, NoopSendFileStorageService>();
            }

            if (globalSettings.SelfHosted)
            {
                services.AddSingleton <IReferenceEventService, NoopReferenceEventService>();
            }
            else
            {
                services.AddSingleton <IReferenceEventService, AzureQueueReferenceEventService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Captcha?.HCaptchaSecretKey) &&
                CoreHelpers.SettingHasValue(globalSettings.Captcha?.HCaptchaSiteKey))
            {
                services.AddSingleton <ICaptchaValidationService, HCaptchaValidationService>();
            }
            else
            {
                services.AddSingleton <ICaptchaValidationService, NoopCaptchaValidationService>();
            }
        }
Beispiel #4
0
        public static ILoggingBuilder AddSerilog(
            this ILoggingBuilder builder,
            WebHostBuilderContext context,
            Func <LogEvent, bool> filter = null)
        {
            if (context.HostingEnvironment.IsDevelopment())
            {
                return(builder);
            }

            bool inclusionPredicate(LogEvent e)
            {
                if (filter == null)
                {
                    return(true);
                }
                var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null;

                if (eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false)
                {
                    return(true);
                }
                return(filter(e));
            }

            var globalSettings = new GlobalSettings();

            ConfigurationBinder.Bind(context.Configuration.GetSection("GlobalSettings"), globalSettings);

            var config = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .Filter.ByIncludingOnly(inclusionPredicate);

            if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
                CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key))
            {
                config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri),
                                               globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7))
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }
            else if (CoreHelpers.SettingHasValue(globalSettings?.Sentry.Dsn))
            {
                config.WriteTo.Sentry(globalSettings.Sentry.Dsn)
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
            {
                if (globalSettings.LogRollBySizeLimit.HasValue)
                {
                    config.WriteTo.File($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/log.txt",
                                        rollOnFileSizeLimit: true, fileSizeLimitBytes: globalSettings.LogRollBySizeLimit);
                }
                else
                {
                    config.WriteTo
                    .RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
                }
                config
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }

            var serilog = config.CreateLogger();

            builder.AddSerilog(serilog);

            return(builder);
        }
        public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
        {
            services.AddSingleton <IMailService, HandlebarsMailService>();
            services.AddSingleton <ILicensingService, LicensingService>();
            services.AddSingleton <IApplicationCacheService, InMemoryApplicationCacheService>();

            if (CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))
            {
                services.AddSingleton <IMailDeliveryService, SendGridMailDeliveryService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host))
            {
                services.AddSingleton <IMailDeliveryService, SmtpMailDeliveryService>();
            }
            else
            {
                services.AddSingleton <IMailDeliveryService, NoopMailDeliveryService>();
            }

            services.AddSingleton <IPushNotificationService, MultiServicePushNotificationService>();
            if (globalSettings.SelfHosted &&
                CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
                globalSettings.Installation?.Id != null &&
                CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
            {
                services.AddSingleton <IPushRegistrationService, RelayPushRegistrationService>();
            }
            else if (!globalSettings.SelfHosted)
            {
                services.AddSingleton <IPushRegistrationService, NotificationHubPushRegistrationService>();
            }
            else
            {
                services.AddSingleton <IPushRegistrationService, NoopPushRegistrationService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
            {
                services.AddSingleton <IBlockIpService, AzureQueueBlockIpService>();
            }
            else
            {
                services.AddSingleton <IBlockIpService, NoopBlockIpService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString))
            {
                services.AddSingleton <IEventWriteService, AzureQueueEventWriteService>();
            }
            else if (globalSettings.SelfHosted)
            {
                services.AddSingleton <IEventWriteService, RepositoryEventWriteService>();
            }
            else
            {
                services.AddSingleton <IEventWriteService, NoopEventWriteService>();
            }

            if (CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
            {
                services.AddSingleton <IAttachmentStorageService, AzureAttachmentStorageService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Attachment.BaseDirectory))
            {
                services.AddSingleton <IAttachmentStorageService, LocalAttachmentStorageService>();
            }
            else
            {
                services.AddSingleton <IAttachmentStorageService, NoopAttachmentStorageService>();
            }
        }
        public static ILoggingBuilder AddSerilog(
            this ILoggingBuilder builder,
            WebHostBuilderContext context,
            Func <LogEvent, bool> filter = null)
        {
            if (context.HostingEnvironment.IsDevelopment())
            {
                return(builder);
            }

            bool inclusionPredicate(LogEvent e)
            {
                if (filter == null)
                {
                    return(true);
                }
                var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null;

                if (eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false)
                {
                    return(true);
                }
                return(filter(e));
            }

            var globalSettings = new GlobalSettings();

            ConfigurationBinder.Bind(context.Configuration.GetSection("GlobalSettings"), globalSettings);

            var config = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .Filter.ByIncludingOnly(inclusionPredicate);

            if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
                CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key))
            {
                config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri),
                                               globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7))
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }
            else if (CoreHelpers.SettingHasValue(globalSettings?.Sentry.Dsn))
            {
                config.WriteTo.Sentry(globalSettings.Sentry.Dsn)
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }
            else if (CoreHelpers.SettingHasValue(globalSettings?.Syslog.Destination))
            {
                // appending sitename to project name to allow eaiser identification in syslog.
                var appName = $"{globalSettings.SiteName}-{globalSettings.ProjectName}";
                if (globalSettings.Syslog.Destination.Equals("local", StringComparison.OrdinalIgnoreCase))
                {
                    config.WriteTo.LocalSyslog(appName);
                }
                else if (Uri.TryCreate(globalSettings.Syslog.Destination, UriKind.Absolute, out var syslogAddress))
                {
                    // Syslog's standard port is 514 (both UDP and TCP). TLS does not have a standard port, so assume 514.
                    int port = syslogAddress.Port >= 0
                        ? syslogAddress.Port
                        : 514;

                    if (syslogAddress.Scheme.Equals("udp"))
                    {
                        config.WriteTo.UdpSyslog(syslogAddress.Host, port, appName);
                    }
                    else if (syslogAddress.Scheme.Equals("tcp"))
                    {
                        config.WriteTo.TcpSyslog(syslogAddress.Host, port, appName);
                    }
                    else if (syslogAddress.Scheme.Equals("tls"))
                    {
                        // TLS v1.1, v1.2 and v1.3 are explicitly selected (leaving out TLS v1.0)
                        const SslProtocols protocols = SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;

                        if (CoreHelpers.SettingHasValue(globalSettings.Syslog.CertificateThumbprint))
                        {
                            config.WriteTo.TcpSyslog(syslogAddress.Host, port, appName,
                                                     secureProtocols: protocols,
                                                     certProvider: new CertificateStoreProvider(StoreName.My, StoreLocation.CurrentUser,
                                                                                                globalSettings.Syslog.CertificateThumbprint));
                        }
                        else
                        {
                            config.WriteTo.TcpSyslog(syslogAddress.Host, port, appName,
                                                     secureProtocols: protocols,
                                                     certProvider: new CertificateFileProvider(globalSettings.Syslog.CertificatePath,
                                                                                               globalSettings.Syslog?.CertificatePassword ?? string.Empty));
                        }
                    }
                }
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
            {
                if (globalSettings.LogRollBySizeLimit.HasValue)
                {
                    config.WriteTo.File($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/log.txt",
                                        rollOnFileSizeLimit: true, fileSizeLimitBytes: globalSettings.LogRollBySizeLimit);
                }
                else
                {
                    config.WriteTo
                    .RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
                }
                config
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }

            var serilog = config.CreateLogger();

            builder.AddSerilog(serilog);

            return(builder);
        }
        public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
        {
            if (globalSettings.SelfHosted)
            {
                services.AddSingleton <IMailService, MarkdownMailService>();
            }
            else
            {
                services.AddSingleton <IMailService, RazorMailService>();
            }

            services.AddSingleton <ILicensingService, LicensingService>();

            if (CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))
            {
                services.AddSingleton <IMailDeliveryService, SendGridMailDeliveryService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host) &&
                     CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Username) &&
                     CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Password))
            {
                services.AddSingleton <IMailDeliveryService, SmtpMailDeliveryService>();
            }
            else
            {
                services.AddSingleton <IMailDeliveryService, NoopMailDeliveryService>();
            }

            if (globalSettings.SelfHosted &&
                CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
                globalSettings.Installation?.Id != null &&
                CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
            {
                services.AddSingleton <IPushNotificationService, RelayPushNotificationService>();
                services.AddSingleton <IPushRegistrationService, RelayPushRegistrationService>();
            }
#if NET461
            else if (!globalSettings.SelfHosted)
            {
                services.AddSingleton <IPushNotificationService, NotificationHubPushNotificationService>();
                services.AddSingleton <IPushRegistrationService, NotificationHubPushRegistrationService>();
            }
#endif
            else
            {
                services.AddSingleton <IPushNotificationService, NoopPushNotificationService>();
                services.AddSingleton <IPushRegistrationService, NoopPushRegistrationService>();
            }

            if (!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
            {
                services.AddSingleton <IBlockIpService, AzureQueueBlockIpService>();
            }
            else
            {
                services.AddSingleton <IBlockIpService, NoopBlockIpService>();
            }

            if (CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
            {
                services.AddSingleton <IAttachmentStorageService, AzureAttachmentStorageService>();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.Attachment.BaseDirectory))
            {
                services.AddSingleton <IAttachmentStorageService, LocalAttachmentStorageService>();
            }
            else
            {
                services.AddSingleton <IAttachmentStorageService, NoopAttachmentStorageService>();
            }
        }
 public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 {
     return(CoreHelpers.FromEpocMilliseconds(reader.GetInt64()));
 }
 public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
 {
     writer.WriteNumberValue(CoreHelpers.ToEpocMilliseconds(value));
 }
        public static ILoggerFactory AddSerilog(
            this ILoggerFactory factory,
            IApplicationBuilder appBuilder,
            IHostingEnvironment env,
            IApplicationLifetime appLifetime,
            GlobalSettings globalSettings,
            Func <LogEvent, bool> filter = null)
        {
            if (env.IsDevelopment())
            {
                return(factory);
            }

            bool inclusionPredicate(LogEvent e)
            {
                if (filter == null)
                {
                    return(true);
                }
                var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null;

                if (eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false)
                {
                    return(true);
                }
                return(filter(e));
            }

            var config = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .Filter.ByIncludingOnly(inclusionPredicate);

            if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
                CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key))
            {
                config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri),
                                               globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7))
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }
            else if (CoreHelpers.SettingHasValue(globalSettings?.Sentry.Dsn))
            {
                config.WriteTo.Sentry(globalSettings.Sentry.Dsn)
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName)
                .Destructure.With <HttpContextDestructingPolicy>()
                .Filter.ByExcluding(e => e.Exception?.CheckIfCaptured() == true);

                appBuilder.AddSentryContext();
            }
            else if (CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
            {
                config.WriteTo.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt")
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Project", globalSettings.ProjectName);
            }

            var serilog = config.CreateLogger();

            factory.AddSerilog(serilog);
            appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);

            return(factory);
        }