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);
}
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
成都创新互联专注于企业成都全网营销推广、网站重做改版、鹤城网站定制设计、自适应品牌网站建设、H5开发、购物商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为鹤城等各大城市提供网站开发制作服务。分享文章:Asp.NetCore全局错误日志记录-创新互联
网站URL:http://cdiso.cn/article/docddp.html