Asp.NetCore全局错误日志记录


using System.IO;
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.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace LoggerFactory
{
    public class Startup
    {
        //日志记录
        public static ILoggerRepository Repository;
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            //初始化Repository
            Repository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(Repository, new FileInfo("log4net.config"));
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc(options =>
            {
                options.Filters.Add(); //加入全局异常类
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

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

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

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


  
  
    
      
    

    
      
      
      
      
        
      
    

    
      
      
      
      
      
      
      
      
      
        
      
    

    
    
      
      
      
      
    

  
using log4net;
using Microsoft.AspNetCore.Mvc.Filters;

namespace LoggerFactory.Models
{
    public class HttpGlobalExceptionFilter:IExceptionFilter
    {
        private ILog log = LogManager.GetLogger(Startup.Repository.Name, typeof(HttpGlobalExceptionFilter));
        public void OnException(ExceptionContext context)
        {
            log.Error(context.Exception);
        }
    }
}

当前题目:Asp.NetCore全局错误日志记录
当前路径:http://bzwzjz.com/article/gipoco.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 手机网站制作设计 手机网站设计 专业网站设计 外贸营销网站建设 成都企业网站建设公司 成都网站建设流程 成都网站建设 宜宾网站设计 网站建设改版 高端定制网站设计 网站制作报价 上市集团网站建设 成都网站设计 移动手机网站制作 网站建设 网站建设方案 定制网站制作 成都商城网站建设 重庆企业网站建设 定制网站设计 重庆企业网站建设 成都网站制作