zoukankan      html  css  js  c++  java
  • C#日志文件

    写日志文件是一个很常用的功能,以前都是别人写好的,直接调用的,近期写了一个小工具,因为比较小,所以懒得引用dll文件了,直接上网找了一个,很方便,现在记录下

     public class LogClass
        {
            /**/
            /// <summary>
            /// 写入日志文件
            /// </summary>
            /// <param name="input"></param>
            public static void WriteLogFile(string input)
            {
                string dateTimeNow = DateTime.Now.ToString("yyyyMMdd");
                /**/
                ///指定日志文件的目录
                ///
                //string fname = Directory.GetCurrentDirectory() + "\LogFile" + dateTimeNow + ".txt";//用于获得应用程序当前工作目录
                string fname = Application.StartupPath + "\LogFile" + dateTimeNow + ".txt";//获取程序启动路径
                //StartupPath
                /**/
                ///定义文件信息对象
    
                FileInfo finfo = new FileInfo(fname);
    
                if (!finfo.Exists)
                {
                    FileStream fs;
                    fs = File.Create(fname);
                    fs.Close();
                    finfo = new FileInfo(fname);
                }
    
                /**/
                ///判断文件是否存在以及是否大于2K
                if (finfo.Length > 1024 * 1024 * 10)
                {
                    /**/
                    ///文件超过10MB则重命名
                    // File.Move(Directory.GetCurrentDirectory() + "\LogFile" + dateTimeNow + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\LogFile.txt");
                    File.Move(Application.StartupPath + "\LogFile" + dateTimeNow + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\LogFile.txt");
                    /**/
                    ///删除该文件
                    //finfo.Delete();
                }
                //finfo.AppendText();
                /**/
                ///创建只写文件流
    
                using (FileStream fs = finfo.OpenWrite())
                {
                    /**/
                    ///根据上面创建的文件流创建写数据流
                    StreamWriter w = new StreamWriter(fs);
    
                    /**/
                    ///设置写数据流的起始位置为文件流的末尾
                    w.BaseStream.Seek(0, SeekOrigin.End);
    
                    /**/
                    ///写入“Log Entry : ”
                    w.Write("
    
    Log Entry : ");
    
                    /**/
                    ///写入当前系统时间并换行
                    w.Write("{0} {1} 
    
    ", DateTime.Now.ToLongTimeString(),
                        DateTime.Now.ToLongDateString());
    
                    /**/
                    ///写入日志内容并换行
                    w.Write(input + "
    
    ");
    
                    /**/
                    ///写入------------------------------------“并换行
                    //w.Write("
    
    ");
    
                    /**/
                    ///清空缓冲区内容,并把缓冲区内容写入基础流
                    w.Flush();
    
                    /**/
                    ///关闭写数据流
                    w.Close();
                }
            }
        }

    根据我自己的需要修改了一下日志文件生成路径

    Application.StartupPath + "\LogFile" + dateTimeNow + ".txt";//获取程序启动路径

    Directory.GetCurrentDirectory() + "\LogFile" + dateTimeNow + ".txt";//用于获得应用程序当前工作目录

    需要写日志的时候,调用下WriteLogFile()方法就可以了,这个可以调用的,很方便

    转载:http://www.cnblogs.com/StupidsCat/archive/2012/08/02/2619499.html

  • 相关阅读:
    Azure HPC Pack Cluster添加辅助节点
    Azure HPC Pack 辅助节点模板配置
    Azure HPC Pack配置管理系列(PART6)
    Windows HPC Pack 2012 R2配置
    Azure HPC Pack 节点提升成域控制器
    Azure HPC Pack VM 节点创建和配置
    Azure HPC Pack 部署必要条件准备
    Azure HPC Pack 基础拓扑概述
    Azure VM 性能计数器配置
    Maven私仓配置
  • 原文地址:https://www.cnblogs.com/zhangjd/p/5867391.html
Copyright © 2011-2022 走看看