using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using System.Configuration; using System.Text; using BLL; using Model; using System.Globalization; namespace WebAPI { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); BLL.Funs.ConnString = ConfigurationManager.AppSettings["ConnectionString"]; BLL.Funs.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]); BLL.ErrLogInfo.DefaultErrLogFullPath = Server.MapPath("~/ErrLog.txt"); // 使api返回为json GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); } protected void Application_Error(object sender, EventArgs e) { StringBuilder errLog = null; Exception ex = null; using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { Sys_ErrLogInfo newErr = new Sys_ErrLogInfo { ErrLogId = SQLHelper.GetNewID() }; try { // 获取错误类 ex = Server.GetLastError().InnerException; if (ex == null) { ex = Server.GetLastError().GetBaseException(); } errLog = new StringBuilder(); errLog.Append(String.Format(CultureInfo.InvariantCulture, "出错文件:{0}\r\n", Request.Url.AbsoluteUri)); newErr.ErrUrl = Request.Url.AbsoluteUri; if (Request.UserHostAddress != null) { errLog.Append(String.Format(CultureInfo.InvariantCulture, "IP地址:{0}\r\n", Request.UserHostAddress)); newErr.ErrIP = Request.UserHostAddress; } if (Session != null && Session["CurrUser"] != null) { errLog.Append(String.Format(CultureInfo.InvariantCulture, "操作人员:{0}\r\n", ((Model.Sys_User)Session["CurrUser"]).UserName)); newErr.UserName = ((Model.Sys_User)Session["CurrUser"]).UserId; } } catch { } finally { if (errLog != null) { db.Sys_ErrLogInfo.InsertOnSubmit(newErr); db.SubmitChanges(); } ErrLogInfo.WriteLog( ex, errLog == null ? null : errLog.ToString()); Server.ClearError(); } } } } }