demo20230512/Global.asax.cs

80 lines
3.0 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using log4net;
using NLog;
using Resources.Helper;
using System;
using System.Data.Entity;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Weee.DAL;
namespace Weee
{
public class WeeeTraceSourceExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
Logger log = NLog.LogManager.GetCurrentClassLogger();
Exception tmpEx = CScommon.Exceptions.inner(context.Exception);
//log.Error("App_Error", context.Exception);
log.Error(tmpEx);
log.Error(context.Exception.StackTrace);
}
}
public class MvcApplication : System.Web.HttpApplication
{
void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Error("App_Error", ex);
log.Error(ex.StackTrace);
}
protected void Application_Start()
{
IoCConfig.RegisterDependencies();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configuration.EnsureInitialized();
string log4netPath = Server.MapPath("~/log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(log4netPath));
Database.SetInitializer<WeeeDataContext>(new MigrateDatabaseToLatestVersion<WeeeDataContext,Weee.Migrations.Configuration>());
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger),new WeeeTraceSourceExceptionLogger());
Application["customLoginImg"] = "";
}
/*
* Replicate the culture logic from ApiMultilanguageAttribute and MvcMultilanguageAttribute here,
* so that culture can be set BEFORE model binding, otherwise model validation message won't be localized.
*/
protected void Application_BeginRequest()
{
string cultureName = null;
// Attempt to read the culture cookie from Request
HttpCookie cultureCookie = HttpContext.Current.Request.Cookies["_culture"];
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
}