[asp]代码库
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Security.AccessControl;
namespace Web.UI
{
public class BasePage : System.Web.UI.Page
{
public BasePage()
: base()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 重写事件
///
/// 重写Page的Onload事件,用于判断用户是否登录
///
///
protected override void OnLoad ( EventArgs e )
{
if ( Session[AuthHelper.SESSION_USERNAME] == null )
{
return;
//Response.Redirect("~/Manage/" + AuthHelper.PAGE_LOGINPAGE, true);
}
base.OnLoad ( e );
}
protected override void OnPreInit ( EventArgs e )
{
base.OnPreInit ( e );
}
#endregion
void Page_Error ( Object sender, EventArgs e )
{
System.Exception ex = Server.GetLastError();
#region 将日志写入文本
string ErrorLog = Server.MapPath ( "~/ErrorLog.txt" );
if ( !System.IO.File.Exists ( ErrorLog ) )
{
System.IO.FileStream FS = System.IO.File.Create ( ErrorLog );
FS.Close();
System.Security.AccessControl.DirectorySecurity fSec = new System.Security.AccessControl.DirectorySecurity();
//设置权限的应用为文件夹本身、子文件夹及文件
//所以需要InheritanceFlags.ContainerInherit 或 InheritanceFlags.ObjectInherit
fSec.AddAccessRule ( new FileSystemAccessRule ( "NETWORK SERVICE", FileSystemRights.Write, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow ) );
System.IO.Directory.SetAccessControl ( ErrorLog, fSec );
}
System.IO.StreamWriter SW = null;
bool isappend = true;//是否追加
System.IO.FileInfo FI = new System.IO.FileInfo ( ErrorLog );
// 日志文件大于1M时清空记录
if ( FI.Length > 1024 * 1024 )
{
isappend = false;
}
//System.IO.FileStream FS = FI.OpenWrite();
SW = new System.IO.StreamWriter ( ErrorLog, isappend, System.Text.Encoding.GetEncoding ( 936 ) );
SW.Write ( "\r\n" );
SW.Write ( "ErrorTime:" + DateTime.Now.ToString ( "yyyy年MM月dd日HH时mm分ss秒" ) );
SW.Write ( "\r\n" );
SW.Write ( "访问IP:" + HttpContext.Current.Request.UserHostAddress );
SW.Write ( "\r\n" );
SW.Write ( "出错页面:" + Request.Url.ToString() );
SW.Write ( "\r\n" );
SW.Write ( "Message:" + ex.Message );
SW.Write ( "\r\n" );
SW.Write ( "Source:" + ex.Source );
SW.Write ( "\r\n" );
SW.Write ( "StackTrace:" + ex.StackTrace );
SW.Write ( "\r\n" );
SW.Write ( "TargetSite:" + ex.TargetSite.ReflectedType.FullName + "--" + ex.TargetSite.ToString() ); //错误的方法
SW.Write ( "\r\n" );
SW.Write ( " --" + ex.TargetSite.Module.FullyQualifiedName );
SW.Write ( "\r\n" );
SW.Close();
SW.Dispose();
#endregion
Server.ClearError();
}
}
}
by: 发表于:2017-11-14 10:32:34 顶(0) | 踩(0) 回复
??
回复评论