美文网首页
c# 日志帮助类

c# 日志帮助类

作者: liuyuedeyu | 来源:发表于2018-12-05 15:25 被阅读0次

public class LogManager

    {

        private string PathName;

        private string FileName;

        /// <summary>

        /// 构造 Log

        /// </summary>

        /// <param name="pathname">相对于当前程序目录下 Log目录的相对路径,

         ///如System,就相当于 .\Log\System\</param>

        public LogManager(string pathname)

        {

            if (String.IsNullOrEmpty(pathname))

            {

                throw new Exception("没有初始化 Log 类的 PathName 变量");

            }

            PathName = System.AppDomain.CurrentDomain.BaseDirectory + "Log\\" + pathname;

            if (!Directory.Exists(PathName))

            {

                try

                {

                    Directory.CreateDirectory(PathName);

                }

                catch { }

            }

            if (!Directory.Exists(PathName))

            {

                PathName = System.AppDomain.CurrentDomain.BaseDirectory + "Log";

                if (!Directory.Exists(PathName))

                {

                    try

                    {

                        Directory.CreateDirectory(PathName);

                    }

                    catch { }

                }

                if (!Directory.Exists(PathName))

                {

                    PathName = System.AppDomain.CurrentDomain.BaseDirectory;

                }

            }

            FileName = PathName + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

        }

        public void Write(string Message)

        {

            FileName = PathName + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

            if (String.IsNullOrEmpty(FileName))

            {

                return;

            }

            using (FileStream fs = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Write))

            {

                StreamWriter writer = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GBK"));

                try

                {

                    writer.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + System.DateTime.Now.Millisecond.ToString() + "\t\t" + Message + "\r\n");

                }

                catch { }

                writer.Close();

            }

        }

    }

相关文章

  • c# 日志帮助类

    public class LogManager { private string PathName; ...

  • C# 类命名建议

    C# 类命名建议xxxCollection 集合(xxx类的集合)xxxHelper(xxx的帮助类)xxxInf...

  • 3.monkey参数

    参数分类 常规类参数 事件类参数 约束类参数 调试类参数 常规类参数 常规类参数包括帮助参数和日志信息参数。帮助参...

  • unity3d c#调用java

    c#调用java非静态方法 C#调用静态类,静态方法 ; CustomClass的静态类,SetData是它的静...

  • 使用probe.gl打印日志

    在项目或类库编写中,一个友好的日志打印,能帮助开发者快速的定位到问题的存在,介绍一个好用的日志类库probe.gl...

  • c#操作数据库类

    c#操作数据库类

  • C# 访问修饰符

    C# 【方法】默认访问级别 : private C# 【类】和【结构体】默认访问级别 : internal 1.命...

  • # [C# Directory类的操作]

    C# Directory类的操作 Directory类位于System.IO 命名空间。Directory类提供了...

  • C#它山之石

    C# 使用反射技术实例化指定的类C#之玩转反射Reactive Extensions入门IoC solutions...

  • C# 基础

    C# 的源代码都是以 . cs 结尾的。 namespace 命名空间 C# 是由一个个类组成,类是最小的单元,命...

网友评论

      本文标题:c# 日志帮助类

      本文链接:https://www.haomeiwen.com/subject/odcwcqtx.html