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;
                    }
                }
            }
        }
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    当我有一台服务器时我做了什么
    git 安装及基本配置
    关于大厂面试中问到的二十几个 HTTP 面试题
    日问周刊 | 全栈面试汇总 | 第七期
    dockerfile 最佳实践及示例
    面试官:如果 http 响应头中 ETag 值改变了,是否意味着文件内容一定已经更改
    Nginx 反向代理时获取用户的真实 IP
    Go 语言实现 HTTP 层面的反向代理
    Go 语言中的 Http 路由基础
    Json Schema
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667858.html
Copyright © 2011-2022 走看看