详细的日志文件

public static void ErrorLog(Exception ex)
{
string FilePath = AppDomain.CurrentDomain.BaseDirectory+"ErrorLog.txt";/bin/Debug

        StringBuilder msg = new StringBuilder();
        msg.Append("*************************************** 
");
        msg.AppendFormat(" 异常发生时间: {0} 
", DateTime.Now);
        msg.AppendFormat(" 异常类型: {0} 
", ex.HResult);
        msg.AppendFormat(" 导致当前异常的 Exception 实例: {0} 
", ex.InnerException);
        msg.AppendFormat(" 导致异常的应用程序或对象的名称: {0} 
", ex.Source);
        msg.AppendFormat(" 引发异常的方法: {0} 
", ex.TargetSite);
        msg.AppendFormat(" 异常堆栈信息: {0} 
", ex.StackTrace);
        msg.AppendFormat(" 异常消息: {0} 
", ex.Message);
        msg.Append("***************************************");

        try
        {
            if (File.Exists(FilePath))
            {
                using (StreamWriter tw = File.AppendText(FilePath))
                {
                    tw.WriteLine(msg.ToString());
                }
            }
            else
            {
                TextWriter tw = new StreamWriter(FilePath);
                tw.WriteLine(msg.ToString());
                tw.Flush();
                tw.Close();
                tw = null;
            }
        }
        catch (Exception)
        {
            Console.ReadKey();
        }

    }
原文地址:https://www.cnblogs.com/zmldz/p/14518879.html