LogHelper类

纯属个人记录,不供大家学习!

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace DPForAD2BPMTel

{

    public class LogHelper

    {

        #region 写文本日志

 

        /// <summary>

        /// 向日志文件中输出一个空行

        /// </summary>

        public void WriteSpaceLine()

        {

            WriteLog(string.Empty);

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="message">日志信息</param>

        /// /// <param name="logPath">日志路径</param>

        /// <param name="logName">日志文件名</param>

        public void WriteLog(string message, string logPath, string logName)

        {

            string logMsg = DateTime.Now.ToString("HH:mm:ss \t");

            string logFile = Path.Combine(logPath, logName); // logPath + "\\" + logName; //2008-09-05 修改

            //如果路径不存在,建立目录

            if (!Directory.Exists(logPath))

            {

                Directory.CreateDirectory(logPath);

            }

            //如果文件不存在,建立文件

            if (!File.Exists(logFile))

            {

                File.Create(logFile).Close();

            }

 

            logMsg += message;

 

            try

            {

                WriteToFile(logFile, logMsg);

            }

            catch

            {

                // do nothing

            }

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="message">日志信息</param>

        /// /// <param name="logPath">日志路径</param>

        public void WriteLog(string message, string logPath)

        {

            WriteLog(message, logPath, DateTime.Now.ToString("yyyy-MM-dd.LOG"));

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="sMessage">日志信息</param>

        public void WriteLog(string message)

        {

            WriteLog(message, AppDomain.CurrentDomain.BaseDirectory + "\\LOG");

        }

 

        /// <summary>

        /// 把信息写到指定文件的尾部

        /// </summary>

        /// <param name="fileName">文件完整路径</param>

        /// <param name="msg">信息文本</param>

        public void WriteToFile(string fileName, string msg)

        {

            StreamWriter swWriter = new StreamWriter(new FileStream(fileName, FileMode.Append));

            swWriter.WriteLine(msg);

            swWriter.Close();

        }

        #endregion

    }

}

 

posted on   秋天  阅读(468)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2013年6月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
点击右上角即可分享
微信分享提示