问题描述
我已经能够在我的asp.net core 3.1项目中成功使用StackExchange.Exceptional.AspNetCore进行SQL错误记录和错误电子邮件报告。我已替换了下面显示的Startup.cs类中的默认开发人员错误页面(ConfigureServices的第一行)方法。在配置方法中将其添加为中间件。
public class Startup
{
public Startup(IConfiguration configuration,IWebHostEnvironment env)
{
Configuration = configuration;
HostingEnvironment = env;
}
public IWebHostEnvironment HostingEnvironment { get; }
public IConfiguration Configuration { get; }
public ILifetimeScope AutofacContainer { get; private set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddExceptional(Configuration.GetSection("Exceptional"),settings =>
{
settings.Store.ApplicationName = "MyProject" + HostingEnvironment.EnvironmentName;
settings.UseExceptionalPageOnThrow = HostingEnvironment.IsDevelopment();
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseLazyLoadingProxies().UseSqlServer(
Configuration.GetConnectionString("MyDb")));
services.AddIdentity<ApplicationUser,ApplicationRole>(
options =>
{
options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login";
});
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddExpressiveAnnotations();
var appSettings = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettings);
var pgSettings = Configuration.GetSection("PaymentGatewaySettings");
services.Configure<PaymentGatewaySettings>(pgSettings);
services.AddScoped<ValidateClientSubscriptionAttribute>();
services.AddScoped<ValidateClientIssueAttribute>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
var web = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(web).AsImplementedInterfaces();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AsClosedTypesOf(typeof(IQueryHandler<,>));
}
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
app.UseExceptional();
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
if (!env.IsDevelopment())
{
//THIS DOES NOT WORK WITH EXCEPTIONAL
//app.UseExceptionHandler("/Home/Error");
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
我的要求很简单,当环境为Development时,我想显示StackExchange.Exceptional开发人员页面。在生产时,我想显示一些页面,并显示诸如“糟糕!!出现问题”链接返回页面。就是这样。
我似乎找不到任何对我有帮助的选择。 app.UseExceptionHandler(“ / Home / Error”)或app.UseStatusCodePagesWithReExecute之类的选项也不起作用。在生产环境中出现默认的浏览器错误500页面。
如果仍然有帮助,我正在使用Autofac for DI。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)