一段log execption 的代码

80酷酷网    80kuku.com

          static public string WriteExceptionRecord( Exception e ) {
            try {
                string    fileName = Path.Combine( Application.StartupPath, "error.log" );
                StreamWriter sw = new StreamWriter( fileName, true );
            
                sw.WriteLine( "ErrorLog.WriteExceptionLog() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" );

                try {
                    DateTime now = DateTime.Now;
                    sw.WriteLine( "CurrentDate: " + now.ToLongDateString() );
                    sw.WriteLine( "CurrentTime: " + now.ToLongTimeString() );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-CurrentDateTime>" + ex.ToString() );
                }
                
                try {
                    sw.WriteLine( "Exception.Type: " + e.GetType().Name );
                    sw.WriteLine( "Exception.Message: " + e.Message );
                    sw.WriteLine( "Exception.Source: " + e.Source );
                    sw.WriteLine( "Exception.HelpLink: " + e.HelpLink );
                    sw.WriteLine( "Exception.TargetSite: " + e.TargetSite );
                    sw.WriteLine( "Exception.StackTrace: " );
                    sw.WriteLine( e.StackTrace );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-Exception>" + ex.ToString() );
                }
        
                try {
                    sw.WriteLine( "GC.TotalMemory: " + GC.GetTotalMemory( false ) );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-GC>" + ex.ToString() );
                }
        
                try {
                    sw.WriteLine( "Application.ProductName: " + Application.ProductName );
                    sw.WriteLine( "Application.ProductVersion: " + Application.ProductVersion );
                    sw.WriteLine( "Application.StartupPath: " + Application.StartupPath );
                    sw.WriteLine( "Application.ExecutablePath: " + Application.ExecutablePath );
                    sw.WriteLine( "Application.CurrentDirectory: " + Directory.GetCurrentDirectory() );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-Application>" + ex.ToString() );
                }
        
                try {
                    Assembly execAssembly = Assembly.GetExecutingAssembly();
                    sw.WriteLine( "ExecutingAssembly.CodeBase: " + execAssembly.CodeBase );
                    sw.WriteLine( "ExecutingAssembly.Location: " + execAssembly.Location );
                    sw.WriteLine( "ExecutingAssembly.GlobalAssemblyCache: " + execAssembly.GlobalAssemblyCache );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-ExecutingAssembly>" + ex.ToString() );
                }

                try {
                    foreach( string watchName in s_htWatches.Keys ) {
                        sw.WriteLine( "Watch[" + watchName + "]: " + GetWatchString( watchName ) );
                    }
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-Watches>" + ex.ToString() );
                }

                try {
                    foreach( LogEntry logEntry in s_logEntries ) {
                        sw.WriteLine( "LogEntry" + logEntry.ToString() );
                    }
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-LogEntry>" + ex.ToString() );
                }
                
                try {
                    Assembly selfAssembly = Assembly.GetAssembly( typeof( BugTracking ) );
                    sw.WriteLine( "CurrentAssembly.CodeBase: " + selfAssembly.CodeBase );
                    sw.WriteLine( "CurrentAssembly.Location: " + selfAssembly.Location );
                    sw.WriteLine( "CurrentAssembly.GlobalAssemblyCache: " + selfAssembly.GlobalAssemblyCache );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-CurrentAssembly>" + ex.ToString() );
                }
                
                try {
                    Thread thread = Thread.CurrentThread;
                    sw.WriteLine( "CurrentThread.Name: " + thread.Name );
                    sw.WriteLine( "CurrentThread.Priority: " + thread.Priority );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-CurrentThread>" + ex.ToString() );
                }

                try {
                    Process process = Process.GetCurrentProcess();
                    sw.WriteLine( "CurrentProcess.Name: " + process.ProcessName );
                    sw.WriteLine( "CurrentProcess.MachineName: " + process.MachineName );
                    sw.WriteLine( "CurrentProcess.MainModule: " + process.MainModule );
                    sw.WriteLine( "CurrentProcess.StartDate: " + process.StartTime.ToLongDateString() );
                    sw.WriteLine( "CurrentProcess.StartTime: " + process.StartTime.ToLongTimeString() );
                    sw.WriteLine( "CurrentProcess.UserProcessorTime: " + process.UserProcessorTime );
                    sw.WriteLine( "CurrentProcess.TotalProcessorTime: " + process.TotalProcessorTime );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-CurrentProcess>" + ex.ToString() );
                }

                try {
                    OperatingSystem os = Environment.OSVersion;
                    sw.WriteLine( "Environment.OSVersion.Platform: " + os.Platform );
                    sw.WriteLine( "Environment.OSVersion.Version: " + os.Version );
    
                    Version ver = Environment.Version;
                    sw.WriteLine( "Environment.Version.Major: " + ver.Major );
                    sw.WriteLine( "Environment.Version.Minor: " + ver.Minor );
                    sw.WriteLine( "Environment.Version.Revision: " + ver.Revision );
                    sw.WriteLine( "Environment.Version.Build: " + ver.Build );

                    sw.WriteLine( "Environment.UserName: " + Environment.UserName );
                    sw.WriteLine( "Environment.SystemDirectory: " + Environment.SystemDirectory );
                    sw.WriteLine( "Environment.TickCount: " + Environment.TickCount );
                    sw.WriteLine( "Environment.CommandLine: " + Environment.CommandLine );
                    sw.WriteLine( "Environment.WorkingSet: " + Environment.WorkingSet );
                    string[] args = Environment.GetCommandLineArgs();
                    if( args != null ) {
                        for( int i = 0; i < args.Length; i ++ ) {
                            sw.WriteLine( "Environment.CommandLineArgs[" + i + "]: " + args[i] );
                        }
                    }
                    sw.WriteLine( "Environment.StackTrace: " );
                    sw.WriteLine( Environment.StackTrace );
                    sw.Flush();
                }
                catch( Exception ex ) {
                    sw.WriteLine( "<error-Environment>" + ex.ToString() );
                }

                sw.WriteLine( "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" );
                sw.Close();
                
                return    fileName;
            }
            catch( Exception ) {
                return    "-- error writing log file --";
            }
        }


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: