国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#寫日志類實例

2020-01-24 02:21:47
字體:
來源:轉載
供稿:網友

本文實例講述了C#寫日志類,分享給大家供大家參考。

具體實現方法如下:

復制代碼 代碼如下:
using System;
using System.Configuration;
using System.IO;
using System.Threading;

namespace FQDService.Utils
{
    /// <summary>
    /// 寫日志類
    /// </summary>
    public class FileLogger
    {
        #region 字段
        public static readonly object _lock = new object();
        #endregion

        #region 寫文件
        /// <summary>
        /// 寫文件
        /// </summary>
        public static void WriteFile(string log, string path)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
            {
                lock (_lock)
                {
                    if (!File.Exists(path))
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Create)) { }
                    }

                    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            #region 日志內容
                            string value = string.Format(@"{0}
--------------------------------------------------------
{1}

", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), obj.ToString());
                            #endregion

                            sw.WriteLine(value);
                            sw.Flush();
                        }
                    }
                }
            }));
            thread.Start(log);
        }
        #endregion

        #region 寫日志
        /// <summary>
        /// 寫日志
        /// </summary>
        public static void WriteLog(string log)
        {
            string logPath = ConfigurationManager.AppSettings["LogPath"] + "http://FQDService_Log.txt";
            WriteFile(log, logPath);
        }
        #endregion

        #region 寫錯誤日志
        /// <summary>
        /// 寫錯誤日志
        /// </summary>
        public static void WriteErrorLog(string log)
        {
            string logPath = ConfigurationManager.AppSettings["LogPath"] + "http://FQDService_ErrorLog.txt";
            WriteFile(log, logPath);
        }
        #endregion

    }
}

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荣昌县| 宜昌市| 新疆| 赫章县| 苏尼特右旗| 汽车| 兴海县| 比如县| 湘乡市| 甘谷县| 铁岭县| 福安市| 清流县| 四子王旗| 曲沃县| 正定县| 泸州市| 泉州市| 延边| 荣成市| 肃宁县| 灵山县| 麻阳| 承德市| 连平县| 包头市| 巴林左旗| 赞皇县| 务川| 潞西市| 枣庄市| 桐乡市| 黄浦区| 股票| 石柱| 高台县| 高台县| 区。| 道真| 道真| 龙里县|