zoukankan      html  css  js  c++  java
  • C# 创建一个日志文件

    public class LogHelper
        {
            private static string logFolder = AppDomain.CurrentDomain.BaseDirectory + "errorlog";
            private static string logFile;
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public LogHelper()
            {
                try
                {
                    logFile = Path.Combine(logFolder, (DateTime.Now.Date.ToString("yyyy-MM-dd") + ".Log"));
                    if (!Directory.Exists(logFolder))
                    {
                        Directory.CreateDirectory(logFolder);
                    }
                    if (!File.Exists(logFile))
                    {
                        //日志不存在创建
                        using (StreamWriter write = File.CreateText(logFile))
                        {
                            write.WriteLine("----------记录日志----------");
                        }
                    }
                }
                catch { }
            }
    
            /// <summary>
            /// 追加一条信息
            /// </summary>
            /// <param name="text"></param>
            public void Write(string text)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {                                        
                        sw.Write(string.Format("{0}>{1}", DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] "), text));
                    }
                }
                catch { }
            }
    
            /// <summary>
            /// 追加一行信息
            /// </summary>
            /// <param name="text"></param>
            public void WriteLine(string text)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {
                        sw.Write(string.Format("
    {0}>{1}", DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] "), text));
                    }
                }
                catch { }
            }
    
        }
    

      

  • 相关阅读:
    PHP中的error
    回调函数与PHP实例
    PHP的基本入门知识
    Java script OOP——浅谈
    实现单行或多行文本溢出显示省略号
    ECharts
    session management会话管理的原理
    easyui 入门指南
    H5视频/音频
    CSS清除浮动各种方法
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/5666602.html
Copyright © 2011-2022 走看看