as long as you give ASPNET enough privileges (for example, change userName in <processModel> in machine.config to "SYSTEM") or use impersonation, you can write to anythingEventLog's constructor takes a string which can be "System" and "Security", seehttp://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B329291http://www.4guysfromrolla.com/webtech/chapters/ASPNET2/ch02.9.shtml
<customErrors mode="On" //如果设置为Off则出错只返回错误信息,不会跳到自己的指定页面defaultRedirect="/error/customerrorpage.aspx">
<error statusCode="404" redirect="/error/404Page.aspx"/>
<error statusCode="403" redirect="/error/403page.aspx"/>
</customErrors> 二,在Global.asax文件中添加应用出错代码,写入系统日志文件
protected void Application_Error(Object sender, EventArgs e)
{
Exception LastError = Server.GetLastError();
String ErrMessage = LastError.ToString();String LogName = "MyLog";
String Message = "Url " + Request.Path + " Error: " + ErrMessage;// Create Event Log if It Doesn't Existif (!EventLog.SourceExists(LogName))
{
EventLog.CreateEventSource(LogName, LogName);
}
EventLog Log = new EventLog();
Log.Source = LogName;
//These are the five options that will display a different icon.
Log.WriteEntry(Message, EventLogEntryType.Information, 1);
Log.WriteEntry(Message, EventLogEntryType.Error, 2);
Log.WriteEntry(Message, EventLogEntryType.Warning, 3);
Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4);
Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5);}
三,现在你可以进行测试了。
我在Default.aspx.cs中产生一个错误,果然跳到默认的错误页面!
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
int y=0;
int x=1/y;
}
catch (Exception Err)
{
throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode,该如何实现?
//Err.
}
提供与 Windows 事件日志的交互。
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
MyLog = new System.Diagnostics.EventLog("Log");
MyLog.Log = "我的日志";
MyLog.Source = "source";
if (EventLog.Exists("我的日志"))
button4.Enabled = true;
else
button4.Enabled = false;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(0, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(288, 120);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Control";
//
// button3
//
this.button3.Location = new System.Drawing.Point(136, 88);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 6;
this.button3.Text = "DelText";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(72, 88);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 5;
this.button2.Text = "Display";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 88);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Write";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(80, 56);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(200, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "textBox2";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 2;
this.label2.Text = "Event:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 1;
this.label1.Text = "LogName:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBox3);
this.groupBox2.Location = new System.Drawing.Point(0, 136);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(288, 136);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Display";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 16);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(272, 112);
this.textBox3.TabIndex = 0;
this.textBox3.Text = "";
//
// button4
//
this.button4.Location = new System.Drawing.Point(208, 88);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(56, 23);
this.button4.TabIndex = 7;
this.button4.Text = "DelLog";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
string strContent ="";
strContent = strContent+ "Event:" + textBox2.Text+"\n";
MyLog.WriteEntry(strContent);
textBox2.Text = "";
textBox3.Text = strContent.ToString();
button4.Enabled = true;
} private void button2_Click(object sender, System.EventArgs e)
{
textBox3.Text ="";
string strContent="";
foreach(EventLogEntry entry in MyLog.Entries)
strContent = strContent+ entry.Message+"\n";
textBox3.Text = strContent.ToString();
} private void button3_Click(object sender, System.EventArgs e)
{
MyLog.Clear();
textBox3.Text ="";
} private void button4_Click(object sender, System.EventArgs e)
{
EventLog.Delete("我的日志");
button4.Enabled = false;
}
eventLog.source="eventlogname";
eventLog.WriteEntry("message",EventLogEntryType.Error);
还有好多,自己摸索
安全日志?系统日志中?
Visual Basic 和 Visual C#/使用组件编程/创建系统监视组件 里的内容,希望有帮助