• 日志记录类


    日志记录类

      public class SysLog
        {
            public static void CreateLogFile()
            {
                string filePath = HttpContext.Current.Server.MapPath(@"~/LogFolder/");
                if (!Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                if (!File.Exists(filePath + "LogFile.log"))
                {
                    try
                    {
                        FileStream file = File.Create(filePath + "LogFile.log");
                        file.Dispose();
                        file.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
    
            /// <summary>
            ///向日志文件中写入日志
            /// </summary>
            /// <param name="logText"></param>
            public static void LogWrite(string logText)
            {
                CreateLogFile();
    
                string filePath = HttpContext.Current.Server.MapPath(@"~/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine("/*");
                    sw.WriteLine(" *日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine(logText);
                    sw.WriteLine("/*");
                    sw.WriteLine();
                    sw.WriteLine();
                    sw.Flush();
                    sw.Dispose();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    sw.Dispose();
                    sw.Close();
                }
            }
    
            public static void LogWrite(Exception ex)
            {
                CreateLogFile();
                string filePath = HttpContext.Current.Server.MapPath(@"~/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine(" ------------------------------------------------------------------------------------");
                    sw.WriteLine(" *日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine(" *错误源:" + ex.Source);
                    sw.WriteLine(" *错误信息:" + ex.Message);
                    sw.WriteLine(" ------------------------------------------------------------------------------------");
                    sw.WriteLine();
                    sw.Flush();
                    sw.Dispose();
                    sw.Close();
                }
                catch (Exception e)
                {
                    sw.Dispose();
                    sw.Close();
                }
            }
  • 相关阅读:
    php使用时间戳保存时间的意义
    php输出控制函数存在的意义
    php中foreach使用引用的陷阱
    mac下php添加openssl扩展
    gitlab配置自动同步
    lnmp集成环境tp nginx vhost配置
    上传文件中文文件名乱码的解决方法以及iconv函数的使用
    php返回数据格式
    怎样让Git忽略当前已经更改的文件
    connect() php-cgi.sock failed (2: No such file or directory)
  • 原文地址:https://www.cnblogs.com/wangliu/p/4185061.html
Copyright © 2020-2023  润新知