1 using System; 2 using System.Diagnostics; 3 using System.Globalization; 4 using System.IO; 5 using System.Runtime.InteropServices; 6 using System.Text; 7 using System.Threading; 8 using System.Web; 9 10 namespace WebMonitor { 11 public class UnhandledExceptionModule: IHttpModule { 12 13 static int _unhandledExceptionCount = 0; 14 15 static string _sourceName = null; 16 static object _initLock = new object(); 17 static bool _initialized = false; 18 19 public void Init(HttpApplication app) { 20 21 // Do this one time for each AppDomain. 22 if (!_initialized) { 23 lock (_initLock) { 24 if (!_initialized) { 25 string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll"); 26 27 if (!File.Exists(webenginePath)) { 28 throw new Exception(String.Format(CultureInfo.InvariantCulture, 29 "Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.", 30 webenginePath)); 31 } 32 33 FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath); 34 _sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0", 35 ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart); 36 37 if (!EventLog.SourceExists(_sourceName)) { 38 throw new Exception(String.Format(CultureInfo.InvariantCulture, 39 "There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.", 40 _sourceName)); 41 } 42 43 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException); 44 45 _initialized = true; 46 } 47 } 48 } 49 } 50 51 public void Dispose() { 52 } 53 54 void OnUnhandledException(object o, UnhandledExceptionEventArgs e) { 55 // Let this occur one time for each AppDomain. 56 if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0) 57 return; 58 59 StringBuilder message = new StringBuilder("/r/n/r/nUnhandledException logged by UnhandledExceptionModule.dll:/r/n/r/nappId="); 60 61 string appId = (string) AppDomain.CurrentDomain.GetData(".appId"); 62 if (appId != null) { 63 message.Append(appId); 64 } 65 66 Exception currentException = null; 67 for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) { 68 message.AppendFormat("/r/n/r/ntype={0}/r/n/r/nmessage={1}/r/n/r/nstack=/r/n{2}/r/n/r/n", 69 currentException.GetType().FullName, 70 currentException.Message, 71 currentException.StackTrace); 72 } 73 74 EventLog Log = new EventLog(); 75 Log.Source = _sourceName; 76 Log.WriteEntry(message.ToString(), EventLogEntryType.Error); 77 } 78 } 79 }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/11627.html