当前位置: 首页 > 编程语言 > C#

如何记录异常信息进行故障排除?分享

时间:2023-04-10 12:22:11 C#

如何记录异常信息进行排查?我目前正在对Windows服务进行维护,在代码的某些点上有一些异常处理(例如来自计时器和其他外部事件的回调):try{...}catch(Exceptionex){_logger.Log("未处理的异常:{0}rn{1}",ex.Message,ex.StackTrace);Environment.FailFast("致命错误。");}记录异常信息有助于排除错误。然而,有时有趣的信息是内部异常,这使得很难确定问题的根本原因。例如,TypeInitializationException可能很难理解。有没有更好的方法来记录异常信息以进行故障排除?有没有更好的方法来记录异常信息以进行故障排除?就在这儿。不要“聪明”地使用ex.Message和ex.StackTrace。只需使用ex.ToString()。它将递归到内部异常(必要时多级)并显示完整的堆栈跟踪。try{...}catch(Exceptionex){_logger.Log("未处理的异常:rn{0}",ex);Environment.FailFast("致命错误。");举个小例子,如果你创建一个类的实例,在它的静态构造函数中抛出异常,那么你得到的就是这个。此异常抛出将包装在TypeInitializationException中。之前:未处理的异常:“SomeClass”的类型初始值设定项引发异常。在C:\ExceptionHandlingDemo.cs中的SomeNamespace.SomeClass..ctor()SomeNamespace.Callback():第34行不是很有帮助。很难确定哪里出了问题。之后:未处理的异常:System.TypeInitializationException:“SomeClass”的类型初始值设定项引发异常。--->System.ArgumentException:已添加具有相同键的项目。在System.ThrowHelper.ThrowArgumentException(ExceptionResource资源)在System.Collections.Generic.Dictionary`2.Insert(TKey键,TValue值,布尔加法)在System.Collections.Generic.Dictionary`2.Add(TKey键,TValue值)inC:\SomeNamespace.SomeClass..cctor()inExceptionHandlingDemo.cs:line43---endofinnerexceptionstacktrace---atSomeNamespace.SomeClass..ctor()inC:\SomeNamespaceinExceptionHandlingDemo.cs.Callback():Line34您现在可以很容易地确定问题的根本原因是字典中的重复键并将其精确定位到源文件中的第43行。我不知道这是否有帮助,或者是否有点矫枉过正,但您对Microsoft的企业库一无所知吗?(http://msdn.microsoft.com/en-us/library/ff648951.aspx)。其中有一个记录“应用程序块”,这是一个大锤,但最终非常强大/灵活。完成了我想要的设置(全部由配置驱动)后,这就是我构建的训练器的标准。特别是除了例外,我认为我不需要做太多事情来获得有意义的信息。我不确定这是否有帮助。我写了这个Utility类来记录异常的所有信息,我使用Exception.Data和Exception.Message来记录这里共享的信息:https://stackoverflow.com/a/15005319/1060656publicclassExceptionInfoUtil{publicstaticstringGetAllExceptionInfo(异常ex){StringBuildersbexception=newStringBuilder();诠释我=1;sbexception.Append(GetExceptionInfo(ex,i));while(ex.InnerException!=null){i++;ex=ex.InnerException;.Append(GetExceptionInfo(ex,i));}返回sbexception.ToString();}privatestaticstringGetExceptionInfo(Exceptionex,intcount){StringBuildersbexception=newStringBuilder();sbexception.AppendLine(string.Format(""));sbexception.AppendLine(string.Format(""));sbexception.AppendLine(string.Format("**************************************************"));sbexception.AppendLine(string.Format("************************************************"));sbexception.AppendLine(string.Format("内部异常:No.{0}",count));sbexception.AppendLine(字符串。格式(”************************************************"));sbexception.AppendLine(string.Format("=================================================="));sbexception.AppendLine(string.Format("错误信息:{0}",ex.Message));sbexception.AppendLine(string.Format("=================================================="));#regionMineThru数据字典try{sbexception.AppendLine(string.Format("=================================================="));sbexception.AppendLine(string.Format("源数据参数计数:{0}",ex.Data.Count));sbexception.AppendLine(string.Format("=================================================="));字符串skey=string.Empty;foreach(ex.Data.Keys中的对象键){try{if(key!=null){skey=Convert.ToString(key);sbexception.AppendLine(string.Format("Key:{0},Value:{1}",skey,Convert.ToString(ex.Data[key])));}else{sbexception.AppendLine(string.Format("Keyisnull"));}}赶上(异常e1){sbexception.AppendLine(string.Format("**写日志时出现异常***[{0}]",e1.Message));}}}catch(Exceptionex1){sbexception.AppendLine(string.Format("**写日志时出现异常***[{0}]",ex1.Message));}#endregionsbexception.AppendLine(string.Format("=================================================="));sbexception.AppendLine(string.Format("Source:{0}",ex.Source));sbexception.AppendLine(string.Format("=================================================="));sbexception.AppendLine(string.Format("StackTrace:{0}",ex.StackTrace));sbexception.AppendLine(string.Format("=================================================="));sbexception.AppendLine(string.Format("TargetSite:{0}",ex.TargetSite));sbexception.AppendLine(string.Format("*************************************************"));sbexception.AppendLine(string.Format("写完异常信息:{0}",count));sbexception.AppendLine(string.格式(“***************************************************"));sbexception.AppendLine(string.Format("***************************************************"));sbexception.AppendLine(string.Format(""));sbexception.AppendLine(string.Format(""));返回sbexception.ToString();下面是使用这个实用类的示例类上面是C#学习教程:如何记录异常信息以进行故障排除?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——[Serializable]publicclassFlatFileItem{ArrayListerrorlist=newArrayList();publicFlatFileItem(){if(errorlist==null){errorlist=newArrayList();}}//文件名publicstringFileName{get;放;}publicoverridestringToString(){returnstring.Format(@"FlatFileItem(UnzipFTPLineItem)=>FileName:{0}",this.FileName);}}publicclasssomeclass{publicvoidsomemethod(){try{//抛出异常代码}catch(Exceptionex){ex.Data["flatfile"]=Convert.ToString(flatfile);//使用数据属性flatfile.HasErrors=true;//在上面的例子中没有latfile.Parent.AddErrorInfo(前);//上面的例子中没有logger.Error(String.Format(ex.Message));//上面的例子中没有throw(newException("yourmsg",ex));//如果你想这样做}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: