分析IIS进程未处理异常

方法 1

修改 IHttpModule 对象的源代码,以便将异常信息记录到应用程序日志中。记录的信息将包含以下内容:

  • 出现异常的虚拟目录路径
  • 异常名称
  • 消息
  • 堆栈跟踪
要修改 IHttpModule 对象,请按照下列步骤操作。

 

注意:此代码将会在应用程序日志中记录事件类型为“错误”且事件来源为“ASP.NET 2.0.50727.0”的消息。要测试模块,可以请求使用 ThreadPool.QueueUserWorkItem 方法的 ASP.NET 页,以调用引发未处理的异常的方法。

1:将下面的代码放在名为 UnhandledExceptionModule.cs 的文件中。

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Web;

namespace WebMonitor {
    public class UnhandledExceptionModule: IHttpModule {

        static int _unhandledExceptionCount = 0;

        static string _sourceName = null;
        static object _initLock = new object();
        static bool _initialized = false;

        public void Init(HttpApplication app) {

            // Do this one time for each AppDomain.
            if (!_initialized) {
                lock (_initLock) {
                    if (!_initialized) {

                        string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");

                        if (!File.Exists(webenginePath)) {
                            throw new Exception(String.Format(CultureInfo.InvariantCulture,
                                                              "Failed to locate webengine.dll at '{0}'.  This module requires .NET Framework 2.0.",
                                                              webenginePath));
                        }

                        FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
                        _sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
                                                    ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);

                        if (!EventLog.SourceExists(_sourceName)) {
                            throw new Exception(String.Format(CultureInfo.InvariantCulture,
                                                              "There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",
                                                              _sourceName));
                        }

                        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

                        _initialized = true;
                    }
                }
            }
        }

        public void Dispose() {
        }

        void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
            // Let this occur one time for each AppDomain.
            if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
                return;

            StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException logged by UnhandledExceptionModule.dll:\r\n\r\nappId=");

            string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
            if (appId != null) {
                message.Append(appId);
            }
           

            Exception currentException = null;
            for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {
                message.AppendFormat("\r\n\r\ntype={0}\r\n\r\nmessage={1}\r\n\r\nstack=\r\n{2}\r\n\r\n",
                                     currentException.GetType().FullName,
                                     currentException.Message,
                                     currentException.StackTrace);
            }          

            EventLog Log = new EventLog();
            Log.Source = _sourceName;
            Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
        }

    }
}

2:将 UnhandledExceptionModule.cs 文件保存到下面的文件夹中:

3:打开 Microsoft Visual Studio 2005 命令提示符窗口。

4:键入 sn.exe -k key.snk,然后按 Enter。

5:键入 csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs,然后按 Enter。

6:键入 gacutil.exe /if UnhandledExceptionModule.dll,然后按 Enter。

7:键入 ngen install UnhandledExceptionModule.dll,然后按 Enter。

8:键入 gacutil /l UnhandledExceptionModule,然后按 Enter 以显示 UnhandledExceptionModule 文件的强名称。

9:将下面的代码添加到基于 ASP.NET 的应用程序的 Web.config 文件中。

type="WebMonitor.UnhandledExceptionModule, strong name" />

方法二:

将未处理异常策略更改回 .NET Framework 1.1 和 .NET Framework 1.0 中发生的默认行为。

注意:我们不建议您更改默认行为。如果忽略异常,应用程序可能会泄漏资源并放弃锁定。

要启用这种默认行为,请将下面的代码添加到位于以下文件夹的 Aspnet.config 文件中:

%WINDIR%\Microsoft.NET\Framework\v2.0.50727


   
       
   
 

 

方法三:简单做法

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;

namespace WebMonitor
{
    /**
    /// Summary description for UnhandledExceptionModule
    ///
    public class UnhandledExceptionModule : IHttpModule
    {
        static object _initLock = new object();
        static bool _initialized = false;

        public UnhandledExceptionModule()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        void OnUnhandledException(object o, UnhandledExceptionEventArgs e)
        {
            //Do some thing you wish to do when the Unhandled Exception raised.
            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(@"D:\testme.log",  System.IO.FileMode.Append, System.IO.FileAccess.Write))

                 {
                    using (System.IO.StreamWriter w = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8))
                     {
                        w.WriteLine(e.ExceptionObject);
                    }
                }
            }
            catch
            {
            }
        }
        #region IHttpModule Members
        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public void Init(HttpApplication context)
        {
            // Do this one time for each AppDomain.
            lock (_initLock)
            {
                if (!_initialized)
                {
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
                    _initialized = true;
                }
            }
        }
        #endregion
    }
}
modual中

  
   
  

runtime> <>

转载于:https://my.oschina.net/dxf/blog/230

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值