Advertisement
PeterAS17

Untitled

Feb 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using NLog;
  7. using NLog.Targets;
  8. using NLog.Config;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Hosting;
  11. using Microsoft.Extensions.FileProviders;
  12. using Microsoft.AspNetCore.Localization;
  13. using System.Globalization;
  14.  
  15. namespace ION
  16. {
  17.     public class Startup
  18.     {
  19.         private static FileTarget logFile;
  20.         private static ConsoleTarget consoleLog;
  21.  
  22.         public Startup(IConfiguration configuration)
  23.         {
  24.             Configuration = configuration;
  25.         }
  26.  
  27.         public IConfiguration Configuration { get; }
  28.  
  29.         /// <summary>
  30.         /// This method gets called by the runtime. Use this method to add services to the container.
  31.         /// </summary>
  32.         /// <param name="services"></param>
  33.         public static void ConfigureServices(IServiceCollection services)
  34.         {
  35.             StartLogger();
  36.  
  37.             Utilities.Config.ReadConfig();
  38.  
  39.             services.Configure<CookiePolicyOptions>(options => {
  40.                 options.CheckConsentNeeded = context => true;
  41.                 options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None;
  42.             });
  43.             services.AddMvc(options => {
  44.                         options.EnableEndpointRouting = false;
  45.                         options.Filters.Add(new Utilities.Page.MvcPagesFilter());
  46.                     })
  47.                     .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
  48.                     .AddRazorRuntimeCompilation(options => {
  49.                         string customFilesPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Pages" + System.IO.Path.DirectorySeparatorChar + "Custom");
  50.  
  51.                         // Check if "Custom" path exists. If not, create it
  52.                         if (!System.IO.Directory.Exists(customFilesPath))
  53.                             System.IO.Directory.CreateDirectory(customFilesPath);
  54.  
  55.                         options.FileProviders.Add(new PhysicalFileProvider(customFilesPath));
  56.                     });
  57.  
  58.             services.AddSession();
  59.  
  60.             // Locale stuff
  61.             services.AddLocalization();
  62.             services.Configure<RequestLocalizationOptions>(options => {
  63.                 var supportedCultures = new[]
  64.                 {
  65.                     new CultureInfo("en"),
  66.                     new CultureInfo("fr"),
  67.                     new CultureInfo("pt"),
  68.                 };
  69.  
  70.                 options.DefaultRequestCulture = new RequestCulture(Utilities.Config.Get("site.language").ToString());
  71.                 options.SupportedCultures = supportedCultures;
  72.                 options.SupportedUICultures = supportedCultures;
  73.             });
  74.  
  75.         }
  76.  
  77.  
  78.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  79.         public static void Configure(IApplicationBuilder app,
  80.                                      IWebHostEnvironment env,
  81.                                      IHostApplicationLifetime appLifetime)
  82.         {
  83.             // Register custom events
  84.             var applicationLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
  85.             applicationLifetime.ApplicationStopping.Register(OnShutdown);
  86.  
  87.             app.UseDeveloperExceptionPage();
  88.             app.UseStatusCodePagesWithReExecute("/Status/{0}");
  89.             app.UseExceptionHandler("/Status");
  90.  
  91.             if (!env.IsDevelopment())
  92.             {
  93.                 app.UseHsts();
  94.             }
  95.  
  96.             StaticObjects.isDevelopmentRun = env.IsDevelopment();
  97.             StaticObjects.appLifetime = applicationLifetime;
  98.             applicationLifetime.ApplicationStarted.Register(Actions.AppLifetime.Started);
  99.             applicationLifetime.ApplicationStopped.Register(Actions.AppLifetime.Stopped);
  100.             applicationLifetime.ApplicationStopping.Register(Actions.AppLifetime.Stopping);
  101.  
  102.             DefaultFilesOptions options = new DefaultFilesOptions();
  103.             options.DefaultFileNames.Clear();
  104.             options.DefaultFileNames.Add("Index");
  105.             app.UseDefaultFiles(options);
  106.  
  107.             app.UseStaticFiles()
  108.                .UseHttpsRedirection()
  109.                .UseSession()
  110.                .UseRouting()
  111.                .UseEndpoints(routes =>
  112.                {
  113.                    routes.MapControllers();
  114.                    routes.MapRazorPages();
  115.                });
  116.             StaticObjects.rootFolder = new PhysicalFileProvider(System.IO.Directory.GetCurrentDirectory());
  117.         }
  118.  
  119.         public static void StartLogger()
  120.         {
  121.             LoggingConfiguration config = new LoggingConfiguration();
  122.  
  123.             logFile = new FileTarget("target_file_other")
  124.             {
  125.                 FileName = "ION.log",
  126.                 Layout = "{\"timestamp\":${ticks},\"type\":\"${level}\",\"frame\":{\"file\":\"${event-properties:file}\",\"line\":${event-properties:line}},\"message\":\"${message}\"}"
  127.             };
  128.             config.AddTarget(logFile);
  129.  
  130.  
  131.             consoleLog = new ConsoleTarget("target_console")
  132.             {
  133.                 Layout = "[${ticks} | ${level}] ${event-properties:file} @ ${event-properties:line}: ${message}"
  134.             };
  135.             config.AddTarget(consoleLog);
  136.  
  137.             config.AddRuleForAllLevels(logFile);
  138.             config.AddRuleForAllLevels(consoleLog);
  139.  
  140.             LogManager.Configuration = config;
  141.             StaticObjects.log = LogManager.GetLogger("logger");
  142.         }
  143.  
  144.         public static void OnShutdown()
  145.         {
  146.             logFile.Dispose();
  147.             consoleLog.Dispose();
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement