您的位置:首页 > 其它

log4net自定义错误日志实例

2007-08-03 15:08 399 查看
1、log4net配置 Web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="E://AppLog//" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<!--param name="DatePattern" value="projectError.txt" /-->
<param name="DatePattern" value="yyyyMMdd".htm"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

<configuration>

2、log4net 操作类 ErrClass.cs

using System;
using log4net;

namespace WebApplication1
{
/// <summary>
/// ErrClass 的摘要说明。
/// </summary>
public class ErrClass
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ErrClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void ErrOption(string mess)
{
log.Warn(mess);
}
}
}

3、前台测试页面 WebForm1.aspx

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 368px; POSITION: absolute; TOP: 128px" runat="server"
Text="Button"></asp:Button>
<asp:TextBox id="tbMessage" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 72px"
runat="server"></asp:TextBox>
<asp:Label id="LabMessage" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 72px"
runat="server">输入信息:</asp:Label></FONT>
</form>
</body>
</HTML>

后台测试页面 WebForm1.aspx.c

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using log4net;
using WebApplication1;

namespace WebApplication1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbMessage;
protected System.Web.UI.WebControls.Label LabMessage;
protected System.Web.UI.WebControls.Button Button1;
string _strMessage;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
_strMessage = this.tbMessage.Text.ToString();
// log.Info("/r/n客户机IP:"+ Request.UserHostAddress +"/r/n<BR>错误地址:"+ Request.Url,Server.GetLastError());
ErrClass ec = new ErrClass();
ec.ErrOption("错误信息:"+ _strMessage);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: