[ 来源:http://www.it55.com | 作者: | 时间:2007-12-18 | 收藏 | 推荐 ] 【大 中 小】
学习完王磊先生的文章:ASP.NET 2.0中的健康监测系统(Health Monitoring)
http://www.cnblogs.com/webabcd/archive/2007/05/20/753507.html
就有了这篇文章
目前基本了解到写入系统日志的方法有三种:
①EIF(Enterprise Implementation Framework),很强大的工具,我上项目中已使用.
我自己创建了一个Web Site项目,进行配置,搞了半天,失败!
难道Web Site项目不适合用EIF,还是本人愚钝,高手的说话.
这里有详细的配置步骤:http://msdn2.microsoft.com/en-us/library/ms979206.aspx
②我自己写了个DLL,感觉很好用!
(基本为王磊先生文章的一个扩展)
1.创建一个Web Site项目.
2.引入DLL(LogLibrary_AX.dll)
3.使用Log类的LogInfo/LogWarning方法进行日志的写入.使用代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//This is my DLL,need import.
using LogLibrary_AX;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Write information
Log.LogInfo("Page load starting",this);
try
{
Log.LogInfo("Come on,baby!", this);
int i = 0;
//There will throw an exception.
int j = 1 / i;
}
catch (Exception ex)
{
//Write warning type information
Log.LogWarning("Hello Warning! Page load has an error!",this,ex);
}
Log.LogInfo("Page load process end.",this);
}
}
4.配置Web.config
添加 HealthMonitoring 节点
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--添加 HealthMonitoring 节点开始-->
<healthMonitoring>
<eventMappings>
<add name="AX" type="LogLibrary_AX.LogInfo"/>
</eventMappings>
<rules>
<add name="rule_AX" eventName="AX" provider="EventLogProvider"/>
</rules>
</healthMonitoring>
<!--添加 HealthMonitoring 节点结束-->
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
LogLibrary_AX.dll源码如下
(下载链接:http://www.cnblogs.com/Files/AXzhz/LogLibrary_AX.rar)
using System;
using System.Web.Management;
namespace LogLibrary_AX
{
public class LogInfo : WebAuditEvent
{
public LogInfo(string message, object eventSource)
: base(message, eventSource, WebEventCodes.WebExtendedBase + 8888)
{
}
}
public class LogWarning : WebBaseErrorEvent
{
public LogWarning(string message, object eventSource,Exception exception)
: base(message, eventSource, WebEventCodes.WebExtendedBase + 9999,exception)
{
}
}
//外观模式
public class Log
{
public static void LogInfo(string message, object eventSource)
{
new LogInfo(message, eventSource).Raise();
}
public static void LogWarning(string message, object eventSource,Exception exception)
{
new LogWarning(message, eventSource,exception).Raise();
}
}
}
运行结果:
(编辑:IT资讯之家 www.it55.com)