zoukankan      html  css  js  c++  java
  • 生成Log文件的写法

    System.IO.StreamWriter w = new System.IO.StreamWriter(Server.MapPath("~/App_data/xxx.log"), true);
    w.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " IP:" + clientIp + " UserName:" + this.Login1.UserName);
    w.Close();

     1 using System.IO;
     2 public static void WriteLog(string strLog)
     3 {
     4   string sFilePath="d:\"+DateTime.Now.ToString("yyyyMM");
     5   string sFileName = "rizhi" + DateTime.Now.ToString("dd") + ".log";
     6   sFileName = sFilePath+ "\"+sFileName; //文件的绝对路径
     7   if (!Directory.Exists(sFilePath))//验证路径是否存在
     8   {
     9     Directory.CreateDirectory(sFilePath);
    10     //不存在则创建
    11   }
    12   FileStream fs;      
    13   StreamWriter sw;
    14   if (File.Exists(sFileName))
    15   //验证文件是否存在,有则追加,无则创建
    16   {
    17     fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
    18   }
    19   else
    20   {
    21     fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
    22   }
    23   sw = new StreamWriter(fs);
    24   sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "   ---   " + strLog);
    25   sw.Close();
    26   fs.Close();    
    27 }
    View Code
  • 相关阅读:
    【bzoj3211】花神游历各国
    四维偏序(K-D-Tree+rebuild)
    【bzoj3295】动态逆序对
    BZOJ3444: 最后的晚餐
    BZOJ3091: 城市旅行
    BestCoder Round #72
    BZOJ2733: [HNOI2012]永无乡
    wc2016总结
    BZOJ4381: [POI2015]Odwiedziny
    BZOJ4196: [Noi2015]软件包管理器
  • 原文地址:https://www.cnblogs.com/huangjing/p/7452423.html
Copyright © 2011-2022 走看看