Пример #1
0
        public static void WriteInfo(string message)
        {
            TotalLog total = new TotalLog()
            {
                Descripton = message
            };

            totalRepo.InsertData(total);
        }
Пример #2
0
        public static void WriteError(Exception e)
        {
            TotalLog total = new TotalLog()
            {
                Descripton = string.Format("{0}: {1} \n{2}", e.Data, e.Message, e.StackTrace)
            };

            totalRepo.InsertData(total);
        }
Пример #3
0
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              ApplicationContext _contextDb,
                              TotalLog totalLog,
                              SimpleLogger logger)
        {
            logger.Log(LogLevel.IMPORTANT_INFO,
                       Source.WEBSITE,
                       "Запуск сервера сайта");



            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error/Error");
            }

            bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

            if (!isWindows)
            {
                app.UseHsts();
//                app.UseHttpsRedirection();
            }


            var supportedCultures = new[]
            {
                new CultureInfo("en"),
                new CultureInfo("ru"),
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });


            app.UseStaticFiles();
            app.UseCookiePolicy();

            //my
            app.UseAuthentication();
            var wsOptions = new WebSocketOptions()
            {
                KeepAliveInterval = TimeSpan.FromSeconds(1),
                ReceiveBufferSize = 4 * 1024
            };

            app.UseWebSockets(wsOptions);

            if (!env.IsDevelopment())
            {
                //Костыльное отлавливание ошибок
                app.Use(async(context, next) =>
                {
                    try
                    {
                        await next.Invoke();
                    }
                    catch (Exception exception)
                    {
                        logger.Log(LogLevel.FATAL, Source.WEBSITE, "Сайт навернулся", ex: exception);
                        string message = "Oh! Something went wrong ((";
                        context.Response.Redirect($"/StaticMessage/Failure?message={message}");
                    }
                });
            }


            //сохранение в удобном виде для настроек локализации
            app.Use((context, next) =>
            {
                var userLanguages = context.Request.Headers["Accept-Language"].ToString();
                var firstLang     = userLanguages.Split(',').FirstOrDefault();

                string lang = "";
                if (firstLang != null && firstLang.ToLower().Contains("ru"))
                {
                    lang = "ru";
                }
                else
                {
                    lang = "en";
                }


                //switch culture
                Thread.CurrentThread.CurrentCulture   = new System.Globalization.CultureInfo(lang);
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

                //save for later use
                context.Items["ClientLang"]    = lang;
                context.Items["ClientCulture"] = Thread.CurrentThread.CurrentUICulture.Name;

                // Call the next delegate/middleware in the pipeline
                return(next());
            });


            //запись accountId если удалось найти в куки
            app.Use((context, next) =>
            {
                string idStr = context.User.FindFirst(x => x.Type == "userId")?.Value;

                if (int.TryParse(idStr, out int id))
                {
                    context.Items["accountId"] = id;
                }

                return(next());
            });



            app.Use(async(context, next) =>
            {
                totalLog.Log(context);
                await next.Invoke();
            });



            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Main}/{action=Index}/{id?}");
            });
        }