• 错误日志记录代码


        using System.IO;
        
        public class PublicWriteError
        {
            private static StreamWriter streamWriter;
           /// <summary>
           /// 
           /// </summary>
           /// <param name="message">报错信息</param>
           /// <param name="type"></param>
           /// <param name="FFname">方法名称</param>
            public static void WriteError(string message, string type, string FFname)
            {
                try
                {
                    //DateTime dt = new DateTime();  
                    string directPath = "D://DJErrorLog";    //在获得文件夹路径  
                    //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log";
                    if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建  
                    {
                        Directory.CreateDirectory(directPath);
                    }
                    directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
                    if (streamWriter == null)
                    {
                        streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。  
                    }
                    streamWriter.WriteLine("***********************************************************************");
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                    streamWriter.WriteLine("输出信息:" + type + "");
                    streamWriter.WriteLine("方法名称:" + FFname + "");
                    if (message != null)
                    {
                        streamWriter.WriteLine("信息:
    " + message);
                    }
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Flush();
                        streamWriter.Dispose();
                        streamWriter = null;
                    }
                }
            }
    
           /// <summary>
           /// 
           /// </summary>
           /// <param name="ex"></param>
           /// <param name="Message">报错信息</param>
           /// <param name="FFname">方法名称</param>
            public static void WriteError(Exception ex, string Message, string FFname)
            {
                try
                {
                    //DateTime dt = new DateTime();  
                    string directPath = "D://DJErrorLog";    //在获得文件夹路径  
                    //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log";
                    if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建  
                    {
                        Directory.CreateDirectory(directPath);
                    }
                    directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
                    if (streamWriter == null)
                    {
                        streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。  
                    }
                    streamWriter.WriteLine("***********************************************************************");
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                    streamWriter.WriteLine("输出信息:" + Message + "");
                    streamWriter.WriteLine("方法名称:" + FFname + "");
                    streamWriter.WriteLine("错误信息:
    " + ex.Message);
                    streamWriter.WriteLine("栈堆信息:
    " + ex.StackTrace);
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Flush();
                        streamWriter.Dispose();
                        streamWriter = null;
                    }
                }
            }
    
           /// <summary>
           /// 
           /// </summary>
           /// <param name="ex"></param>
           /// <param name="FFname">方法名称</param>
            public static void WriteError(Exception ex, string FFname)
            {
                try
                {
                    //DateTime dt = new DateTime();  
                    string directPath = "D://DJErrorLog";    //在获得文件夹路径  
                    //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log";
                    if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建  
                    {
                        Directory.CreateDirectory(directPath);
                    }
                    directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
                    if (streamWriter == null)
                    {
                        streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。  
                    }
                    streamWriter.WriteLine("***********************************************************************");
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                    streamWriter.WriteLine("输出信息:错误信息");
                    streamWriter.WriteLine("方法名称:" + FFname + "");
                    streamWriter.WriteLine("错误信息:
    " + ex.Message);
                    streamWriter.WriteLine("栈堆信息:
    " + ex.StackTrace);
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Flush();
                        streamWriter.Dispose();
                        streamWriter = null;
                    }
                }
            }
        }
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    Torchkeras,一个源码不足300行的深度学习框架
    【知乎】语义分割该如何走下去?
    【SDOI2017】天才黑客(前后缀优化建图 & 最短路)
    【WC2014】紫荆花之恋(替罪羊重构点分树 & 平衡树)
    【SDOI2017】相关分析(线段树)
    【学习笔记】分治法最短路小结
    【CH 弱省互测 Round #1 】OVOO(可持久化可并堆)
    【学习笔记】K 短路问题详解
    【学习笔记】浅析平衡树套线段树 & 带插入区间K小值
    【APIO2020】交换城市(Kruskal重构树)
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667858.html
Copyright © 2020-2023  润新知