zoukankan      html  css  js  c++  java
  • 错误日志记录代码

        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;
                    }
                }
            }
        }
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    python爬虫---selenium库的用法
    Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
    python字符串截取、查找、分割
    jupyter notebook快捷键使用指南
    python中防止字符串转义
    Python之print()函数
    使用腾讯电脑管家清理电脑后,上不了网了
    Python正则表达式指南
    python之format函数
    python安装media报错
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667858.html
Copyright © 2011-2022 走看看