你可以异步去做这件事,
也可以以类似"池"方式自己实现一个也可以.
或者是自已写一个Windows服务专门做这件事.等等.
个人看法,请各位指正.

解决方案 »

  1.   

    'Author:        Lujun
    'Date:          2004-2-6
    'Description:   Base class for common function.Option Strict On
    Option Explicit On Imports System
    Imports System.Web.Security
    Imports System.Diagnostics
    Imports System.Configuration
    Imports System.Web
    '----------------------------------------------------------------
    ' Namespace: TestTransaction.Common
    ' Class: Utility
    '
    ' Description:
    '   Provide a base class of common function for every layer 
    '----------------------------------------------------------------
    Public Class Common
    #Region "Declare Member Variables"
        Protected m_TraceEnable As Boolean
        Protected m_LogLevel As Integer
        Protected m_LogEventSource As String
    #End Region#Region "Construct function"
        Public Sub New()
            m_TraceEnable = CBool(getAppSetting("TraceEnable"))
            m_LogLevel = CInt(getAppSetting("LogLevel"))
            m_LogEventSource = getAppSetting("LogEventSource")
        End Sub
    #End Region #Region "Construct function LogEvent"
        '----------------------------------------------------------------
        ' Function Create:
        '   write some log information to event log for admin user.
        ' Returns:
        '   nothing
        ' Parameters:
        '   [in]  string                errMessage        write message
        '   [in]  EventLogEntryType     LogType           log type
        '----------------------------------------------------------------
    Public Sub LogEvent(ByVal errMessage As String, ByVal LogType As EventLogEntryType)
            If Not EventLog.SourceExists(m_LogEventSource) Then
                EventLog.CreateEventSource(m_LogEventSource, m_LogEventSource)
            End If
            EventLog.WriteEntry(m_LogEventSource, errMessage, LogType)
        End Sub    '----------------------------------------------------------------
        ' Function Create:
        '   write some log information to event log for admin user.
        ' Returns:
        '   nothing
        ' Parameters:
        '   [in]  string                storedProcName        which stored procedure execute.
        '   [in]  EventLogEntryType     LogType               log type
        '   [in]  string                ErrMsg                Error message
        '----------------------------------------------------------------
        Public Sub LogEvent(ByVal storedProcName As String, ByVal LogType As EventLogEntryType, ByVal ErrMsg As String)
            Dim errMessage As String        If Not EventLog.SourceExists(m_LogEventSource) Then
                EventLog.CreateEventSource(m_LogEventSource, m_LogEventSource)
            End If        errMessage = "Run stored procedure [" + storedProcName + "]." + vbCrLf
            errMessage += ErrMsg        EventLog.WriteEntry(m_LogEventSource, errMessage, LogType)
        End Sub    '----------------------------------------------------------------
        ' Function Create:
        '   write some log information to event log for admin user.
        ' Returns:
        '   nothing
        ' Parameters:
        '   [in]  string                storedProcName          which stored procedure execute.
        '   [in]  IDataParameter[]      parameters(Present)     run stored procedure parameter
        '   [in]  EventLogEntryType     LogType                 which log type write to event log 
        '   [in]  string                ErrMsg                  Error message(optional)
        '----------------------------------------------------------------
        Public Sub LogEvent(ByVal storedProcName As String, ByVal parameters() As IDataParameter, ByVal LogType As EventLogEntryType, Optional ByVal ErrMsg As String = "")
            Dim errMessage As String
            Dim parameter As System.Data.SqlClient.SqlParameter        If Not EventLog.SourceExists(m_LogEventSource) Then
                EventLog.CreateEventSource(m_LogEventSource, m_LogEventSource)
            End If        errMessage = "Run stored procedure [" + storedProcName + "]." + vbCrLf
            If parameters.Length > 0 Then
                errMessage += "Parameters:" + vbCrLf            For Each parameter In parameters                If parameter.Value Is DBNull.Value Then
                        errMessage += vbTab + "[" + parameter.ParameterName + "] = ""Null""" + vbCrLf
                    ElseIf parameter.DbType = DbType.Binary Then
                        errMessage += vbTab + "[" + parameter.ParameterName + "] = ""Binary FIle""" + vbCrLf
                    Else
                        errMessage += vbTab + "[" + parameter.ParameterName + "] = """ + CStr(parameter.Value) + """" + vbCrLf
                    End If
                Next
            End If
            errMessage += ErrMsg + vbCrLf + vbCrLf        'facilitate debuging stored procedure, add these information here!
            errMessage += storedProcName + " "
            If parameters.Length > 0 Then
                For Each parameter In parameters
                    If parameter.Value Is DBNull.Value Then
                        errMessage += "null,"
                    ElseIf parameter.DbType = DbType.Binary Then
                        errMessage += "Ox00,"
                    Else
                        errMessage += "'" + CStr(parameter.Value) + "',"
                    End If            Next
            End If
            errMessage = Left(errMessage, Len(errMessage) - 1)
            EventLog.WriteEntry(m_LogEventSource, errMessage, LogType)
        End Sub
    #End Region
    End ClassgetAppSetting()是一个函数
     Public Shared Function getAppSetting(ByVal KeyName As String) As String
            Dim AppSetting As String
            Try
                AppSetting = ConfigurationSettings.AppSettings(KeyName)
            Catch
                AppSetting = ""
            End Try
            Return AppSetting
        End FunctionkeyName 是web.config 中的键值
    <!--Debug setting-->
    <add key="LogLevel" value="1" />
    <add key="TraceEnable" value="True" />
    <add key="LogEventSource" value="ABM" />
    <!--Debug setting-->是vb.net代码,你自己转成C#把
      

  2.   

    不是有开源项目log4net吗?那个多强大啊!
    参考:http://blog.joycode.com/aspdian/posts/3750.aspxUsing log4net 
     很多开发者都想要一个非常强大的程序日志管理库,能够了解用户对应用程序的使用情况。当程序出现问题时,可以通过分析日志来了解问题之所在。
        过去我们总是用一种很简单的方式来处理日志,即:使用一个日志文件,当程序有问题时就要求用户提供这个文件。但常常日志文件不是没有,就是已经被删除。 
     Log4net 是著名的 log4j for Java 项目的一部分。它是由 www.neoworks.com 的一个团队开发出来,支持多种方式的日志。如 ADO (MS Sql Server 等),File (文件), Console (控制台),EventLog (系统日志),SMTP(邮件方式)...而且支持所有的.Net 平台:Microsoft .Net Framework 1.0 (1.0.3705) 
    Microsoft .Net Framework 1.1 (1.1.4322) 
    Microsoft .Net Compact Framework 1.0 (1.0.5000) 
    Mono 0.25 or higher (Linux 下的) 
    Microsoft Shared Source CLI 1.0 (就是MS 开发源代码的 .Net 运行库) 
     如果你要了解更详细的信息:
      log4net Site  (好象下载有点问题,怎么也下载不了。也有可能是我这边网络的问题)
      Using  log4Net  (一篇很专业的使用指南,花了不少时间才找到的)
      log4net.dll  (我找到的编译好的 log4net.dll 文件,可以直接加入你的工程。)Friday, October 24, 2003 5:30 PM