AXzhz

专注ASP.NET!
        写软件的都是小姐,用软件的都是大爷。真TMD说的正确!
                嫖妓不给钱,反说被强奸!___中国共享软件的无奈!
读小学的时候大学不要钱,读大学了小学不要钱。
没工作时国家有分福利房,工作了后全是高价房!!

                        此软件能有效的破解QQ密码。(注:破解QQ密码是违法行为,请不要将软件用于违法行为)
“安得广厦千万间,大庇天下寒士俱欢颜,风雨不动安如山。”1200多年前,诗人杜甫的理想,如今被一帮享受着电脑和互联网带来的快捷生活方式的受过高等教育的大孩子憧憬着。

导航

把项目运行情况写入系统日志(Log)的三种方法_AX

学习完王磊先生的文章: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源码如下
(下载链接:https://files.cnblogs.com/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();
        }

    }

}


运行结果:


③使用Diagnostics.EventLog类,很easy的方法.我不想用的说.
参见:http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemdiagnosticseventlogclasstopic.asp


---------------------------------------------------
【总结】
方法①很好,很强大.
方法②不能出带红×的信息类型,也不能创建一个和Application同级的节点.(会的说话)
方法③可以创建一个和Application同级的节点,参见上图MyNowLog节点,但是好像出来的类型都是Information
(会出别的的说话)

博客园斧头帮少帮主

posted on 2007-12-17 16:56  斧头帮少帮主  阅读(3389)  评论(10编辑  收藏  举报

Google
 
站内搜索:        
园内搜索:
金山词霸: