YZ_BSF/HJGL/FineUIPro.Web/Global.asax.cs

158 lines
5.1 KiB
C#
Raw Permalink Normal View History

2024-05-13 08:48:51 +08:00
namespace FineUIPro.Web
{
using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using BLL;
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 自动启用插件标志文件路径
/// </summary>
private static string applicationActiveFlagFilePhysicalPath = String.Empty;
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUserCount"] = 0;
try
{
Funs.RootPath = Server.MapPath("~/");
// 日志文件所在目录
ErrLogInfo.DefaultErrLogFullPath = Server.MapPath("~/ErrLog.txt");
//Funs.ConnString = ConfigurationManager.AppSettings["ConnectionString"];
Funs.SystemName = ConfigurationManager.AppSettings["SystemName"];
Funs.ConnString = ConfigurationManager.ConnectionStrings["HJGLDBConnectionString"].ConnectionString;
}
catch (Exception ex)
{
ErrLogInfo.WriteLog(ex);
AppDomain.Unload(AppDomain.CurrentDomain);
}
// 清理日志
if (DateTime.Now.Hour == 7)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
//12小时执行一次
aTimer.Interval = 12 * 60 * 60 * 1000;
aTimer.Enabled = true;
aTimer.Start();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(DeleteLog);
}
}
private void DeleteLog(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime delDate = DateTime.Now.AddMonths(-1);
var logs = from x in Funs.DB.Sys_Log where x.OperationTime <= delDate select x;
Funs.DB.Sys_Log.DeleteAllOnSubmit(logs);
Funs.DB.SubmitChanges();
}
protected void Session_Start(object sender, EventArgs e)
{
Session.Timeout = 36000;
// 这种统计在线人数的做法会有一定的误差
Application.Lock();
Application["OnlineUserCount"] = (int)Application["OnlineUserCount"] + 1;
Application.UnLock();
// https访问secure设置为true
if (Request.IsSecureConnection)
{
Response.Cookies["ASP.NET_SessionId"].Secure = true;
}
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
StringBuilder errLog = null;
Exception ex = null;
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));
if (Request.UserHostAddress != null)
{
errLog.Append(String.Format(CultureInfo.InvariantCulture, "IP地址:{0}\r\n", Request.UserHostAddress));
}
if (Session != null && Session["CurrUser"] != null)
{
errLog.Append(String.Format(CultureInfo.InvariantCulture, "操作人员:{0}\r\n", BLL.Sys_UserService.GetUsersByUserId(((Model.Sys_User)Session["CurrUser"]).UserId).UserName));
}
else
{
PageBase.ZXRefresh(Request.ApplicationPath + "/LogOff.aspx");
}
if (ex is HttpRequestValidationException)
{
PageBase.ZXRefresh(Request.ApplicationPath + "/Wrong.aspx?Message=0");
}
else if (ex is FriendlyException)
{
PageBase.ZXRefresh(Request.ApplicationPath + "/Wrong.aspx?MessageText=" + ex.Message);
}
}
catch
{
try
{
PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
}
catch
{
}
}
finally
{
ErrLogInfo.WriteLog(ex, errLog == null ? null : errLog.ToString());
Server.ClearError();
PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
}
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}