zoukankan      html  css  js  c++  java
  • C#日志记录类(待完善,考虑多线程)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Web.Util;
    using System.Threading;




    namespace CountApp.App_Code
    {
        public class SysLog
        {
            /// <summary>
            /// 创建文件夹
            /// </summary>
            public static void CreateLogFile()
            {
                string filePath = HttpContext.Current.Server.MapPath(@"~/temp/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(@"~/temp/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine("/********************************/");
                    sw.WriteLine("日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine();
                    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(@"~/temp/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine("/********************************/");
                    sw.WriteLine("日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine();
                    sw.WriteLine("错误源:" + ex.Source);
                    sw.WriteLine("错误信息:" + ex.Message);
                    sw.WriteLine("/********************************/");
                    sw.WriteLine();
                    sw.WriteLine();
                    sw.Flush();
                    sw.Dispose();
                    sw.Close();
                }
                catch (Exception e)
                {
                    sw.Dispose();
                    sw.Close();
                }
            }
        }
    }
  • 相关阅读:
    javascript中的this和e.target的深入研究
    mysql基础
    php每天一题:怎么在不使用第三个变量的情况下交换两个变量的值
    用原生javascript实现在页面动态显示时间
    php每天一题:strlen()与mb_strlen()的作用分别是什么
    javascript每天一题
    php中用foreach改变数组的值的问题
    程序员进阶之路
    hdu6638 线段树求最大子段和
    P4513 小白逛公园 动态维护最大子段和
  • 原文地址:https://www.cnblogs.com/wangsx/p/2039076.html
Copyright © 2011-2022 走看看