public static void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IWriteToFileText writeToFileText) { app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { var errorFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = errorFeature.Error; var isConnectionTrusted = context.Request.IsTrusted(); var problemDetails = ProblemDetailsFactory.Build(exception, isConnectionTrusted); var logger = LoggerConfigurations.Configure(loggerFactory, writeToFileText); var logString = $"Processing request {context.Request.Path},\n Status Code: {problemDetails.Status.Value},\n Details:{exception.Demystify().ToString() ?? problemDetails.Detail}\n"; if (!(exception is BusinessLogicException)) { logger.LogError(logString); } context.Response.StatusCode = problemDetails.Status.Value; context.Response.WriteJson(problemDetails, "application/problem+json"); }); }); }
public static ILogger Configure(ILoggerFactory loggerFactory, IWriteToFileText writeToFileText) { loggerFactory.AddFile(writeToFileText, Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")); var logger = loggerFactory.CreateLogger("FileLogger"); return(logger); }
// 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, IWriteToFileText writeToFileText) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { ExceptionHandlerConfigurations.Configure(app, loggerFactory, writeToFileText); } app.UseStaticFiles(); if (string.IsNullOrWhiteSpace(env.WebRootPath)) { env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"); } app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(env.WebRootPath, "uploads")), RequestPath = new PathString("/uploads") }); app.UseForwardedHeaders(new ForwardedHeadersOptionsht { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseHttpsRedirection(); app.UseHsts(); app.AddCors(options => options.AddPolicy("AllowCors", builder => { builder .SetIsOriginAllowed(x => { return ( x.ToLower().IndexOf("localhost") >= 0 || x.ToLower().IndexOf("produrl") >= 0 ); }) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); })); // 2. Enable authentication middleware app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "api/{controller}/{action=Index}/{id?}"); }); }
public FileLoggerProvider(IWriteToFileText writeToFileText, string path) { _writeToFileText = writeToFileText; _path = path; }
public static ILoggerFactory AddFile(this ILoggerFactory factory, IWriteToFileText writeToFileText, string filePath) { factory.AddProvider(new FileLoggerProvider(writeToFileText, filePath)); return(factory); }
public FileLogger(IWriteToFileText writeToFileText, string path) { _writeToFileText = writeToFileText; _filePath = path; }