Monday, February 18, 2019

Get the middleware for managing exceptions to also log exceptions in a .NET Core 2.1 application.

Why not? Why write try/catches in the Controllers themselves anymore? BubbleUpExceptions here may just be expanded like so:

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Trifecta.Core.ExternalDependencies;
using Microsoft.Extensions.Configuration;
using Trifecta.Core.Utilities;
namespace Trifecta.RestApi.Helpers
{
   public class BubbleUpExceptions : ExceptionFilterAttribute
   {
      private ILogWriting _logWriting;
      private ITimekeeping _timekeeping;
      private string _whereToLogTo;
      
      public BubbleUpExceptions(IConfiguration configuration, ILogWriting logWriting,
            ITimekeeping timekeeping)
      {
         _logWriting = logWriting;
         _timekeeping = timekeeping;
         _whereToLogTo = configuration["FileFolderForLogs"];
      }
      
      public override void OnException(ExceptionContext context)
      {
         _logWriting.Log(context.Exception, _whereToLogTo, _timekeeping);
         context.Result = new JsonResult(context.Exception);
         context.HttpContext.Response.StatusCode =
               (int)HttpStatusCode.InternalServerError;
      }
   }
}

 
 

This means that Startup.cs has to change some too. Behold:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Trifecta.Core.ExternalDependencies;
using Trifecta.Infrastructure.ExternalDependencies;
using Trifecta.RestApi.Helpers;
namespace Trifecta.RestApi
{
   public class Startup
   {
      public IConfiguration Configuration { get; }
      
      public Startup(IConfiguration configuration)
      {
         Configuration = configuration;
      }
      
      public void ConfigureServices(IServiceCollection services)
      {
         services.AddCors();
         services.AddMvc(options =>
         {
            options.Filters.Add(new BubbleUpExceptions(
               Configuration,
               services.BuildServiceProvider().GetRequiredService<ILogWriting>(),
               services.BuildServiceProvider().GetRequiredService<ITimekeeping>()
            ));
         }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
         ConfigureIoC(services);
      }
      
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
         if (env.IsDevelopment())
         {
            app.UseDeveloperExceptionPage();
         }
         app.UseCors(builder =>
               builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
         app.UseMvc();
      }
      
      public void ConfigureIoC(IServiceCollection services)
      {
         services.AddTransient<ILogWriting, LogWriting>();
         services.AddTransient<ITimekeeping, Timekeeping>();
      }
   }
}

No comments:

Post a Comment