用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - asp代码库

网站自定义格式的错误日志 

2012-10-19 作者: 程序猿style举报

[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();
	}






}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...