保存日志扩展方法:
public static void SaveLog(this Exception ex) { System.Text.StringBuilder sbText = new StringBuilder(); sbText.AppendLine("当前时间:" + DateTime.Now.ToString()); sbText.AppendLine("异常对象:" + ex.Source); sbText.AppendLine("异常信息:" + ex.Message); sbText.AppendLine(" InnerException: " + ex.InnerException); sbText.AppendLine(" 调用堆栈: " + ex.StackTrace); sbText.AppendLine(" 触发方法:" + ex.TargetSite); sbText.AppendLine("----------------------------------------------------- "); string logPath = System.AppDomain.CurrentDomain.BaseDirectory + "日志.txt"; try { System.IO.File.WriteAllText(logPath, sbText.ToString(), Encoding.UTF8); } catch (Exception) { } }
经分析项目中只有8种catch写法,如下
catch (Exception ex)
catch (Exception err)
catch (Exception e)
catch (Exception )
catch (Exception error)
catch (Exception)
catch(Exception e)
catch(Exception err)
VS替换:
查询内容:catch.*(Exception.*).* .*{
替换内容:catch (Exception ex) { ex.SaveLog();