Section IV: OA rights management system (global exception handler)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using log4net;
using log4net.Config;
using log4net.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using OYW.OA.Application.Settings;
using OYW.OA.DTO;
using OYW.OA.DTO.Redis;
using OYW.OA.EFRepositories;
using OYW.OA.Infrastructure.Redis;
using OYW.OA.Web.Models;

namespace OYW.OA.Web
{
    public class Startup
    {
        public static ILoggerRepository loggerRepository { get; set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            loggerRepository = LogManager.CreateRepository("OYW.OA.Web");
            XmlConfigurator.Configure(loggerRepository, new FileInfo("log4net.config"));

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseExceptionHandler(x =>
            {
                x.Run(async context =>
                {
                    var ex = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>()?.Error;
                    ILog log = IocManager.Resolve<ILog>();
                    var msg = ex == null ? "发生错误。" : ex.Message;
                    log.Error(msg, ex);
                    context.Response.ContentType = "text/plain;charset=utf-8";
                    await context.Response.WriteAsync(msg);
                });
            });
             
        }
    }
}
x.Run(async context =>
                {
                    var ex = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>()?.Error;
                    ILog IocManager.Resolve log = <ILog> ();
                    var msg = ex == null "error.": ex.Message;?
                    log.Error(msg, ex);
                    context.Response.ContentType = "text/plain;charset=utf-8";
                    await context.Response.WriteAsync(msg);
                });
            });
             
        }
    }
}

 

Published 128 original articles · won praise 18 · views 50000 +

Guess you like

Origin blog.csdn.net/xiaoxionglove/article/details/80867720