日志统一处理

保存日志扩展方法:

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("\r\nInnerException:\r\n" + ex.InnerException);
    sbText.AppendLine("\r\n调用堆栈:\r\n" + ex.StackTrace);
    sbText.AppendLine("\r\n触发方法:" + ex.TargetSite);
    sbText.AppendLine("-----------------------------------------------------\r\n\r\n\r\n");

    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.*\).*\n.*{

替换内容:catch (Exception ex)\n            {\n ex.SaveLog();\n

 

posted @ 2017-12-11 21:29  随便取个名字算了  阅读(209)  评论(0编辑  收藏  举报