zoukankan      html  css  js  c++  java
  • C# 记录错误日志

    C#   记录错误日志(简介,方法代码)

    简介:程序在出错时记录错误日志,可以有利于维护,也可以记录一些程序内部运行的操作等等,它的作用很大,也很重要

    方法1:

     1 /// <summary>
     2         /// 将异常打印到LOG文件
     3         /// </summary>
     4         /// <param name="ex">异常</param>
     5         /// <param name="LogAddress">日志文件地址</param>
     6         public static void WriteLog(Exception ex, string LogAddress)
     7         {
     8             //如果日志文件路径LogAddress为空,则默认在Debug目录下新建 YYYY-mm-dd-Log.log文件
     9             if (LogAddress == "")
    10             {
    11                 LogAddress = Environment.CurrentDirectory + '\' +
    12                     DateTime.Now.Year + '-' +
    13                     DateTime.Now.Month + '-' +
    14                     DateTime.Now.Day + "-Log.log";
    15             }
    16             //把异常信息输出到文件
    17             StreamWriter fs = new StreamWriter(LogAddress, true);
    18             fs.WriteLine("当前时间:" + DateTime.Now.ToString());
    19             fs.WriteLine("异常信息:" + ex.Message);
    20             fs.WriteLine("异常对象:" + ex.Source);
    21             fs.WriteLine("调用堆栈:
    " + ex.StackTrace.Trim());
    22             fs.WriteLine("触发方法:" + ex.TargetSite);
    23             fs.WriteLine();
    24             fs.Close();
    25         }
  • 相关阅读:
    redis初步入门(2)
    redis初步入门(1)
    iOS9 中 alertView 的使用
    iOS应用 数据存储方式 (一)
    Python 选课系统
    Python 计算器
    Python ATM
    Python 购物车
    Python 城市列表
    Python 基础登入接口
  • 原文地址:https://www.cnblogs.com/DXW74741/p/5996314.html
Copyright © 2011-2022 走看看