zoukankan      html  css  js  c++  java
  • 日志记录类

    日志记录类

      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();
                }
            }
  • 相关阅读:
    js图片飘动
    实战ASP.NET大规模网站架构:Web加速器(1)【转】
    DNS服务器设置详解
    Lucene:基于Java的全文检索引擎简介【转】
    传道解惑 软件开发技术名词解密(转载)
    UTF8 and Unicode FAQ
    高并发 高负载 网站系统架构 !深入讨论!【转载】
    (转)值的关注的Java开源项目
    MSDN:Webcast 系列课程
    ASP.NET MVC 学习网站
  • 原文地址:https://www.cnblogs.com/wangliu/p/4185061.html
Copyright © 2011-2022 走看看