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

     1 /// <summary>
     2 /// 创建日志文件,每天一个
     3 /// </summary>
     4 /// <param name="logContent">日志内容 </param>
     5 public static void CreateInLog(string logContent)
     6 {
     7     string filePath = logurl.Trim();//路径:如 E:
     8     filePath = filePath.EndsWith(@"") ? filePath : (filePath + @"");
     9     if (Directory.Exists(filePath) == false)
    10     {
    11         Directory.CreateDirectory(filePath);//判断该路径是否存在,不存在则创建该路径文件夹
    12     }
    13 
    14     DateTime dtNow = DateTime.Now;//获取当前日期
    15     string dateString = dtNow.Year.ToString();
    16     dateString += dtNow.Month.ToString().Length < 2 ? ("0" + dtNow.Month.ToString()) : (dtNow.Month.ToString());
    17     dateString += dtNow.Day.ToString().Length < 2 ? ("0" + dtNow.Day.ToString()) : (dtNow.Day.ToString());  //将日期格式转成yyyyMMdd的格式,如:20170921
    18 
    19     string filename = filePath + "Log_" + dateString + ".txt"; //创建文件名
    20     StreamWriter sw = null;
    21 
    22     if (File.Exists(filename))
    23     {
    24         sw = new StreamWriter(filename, true, System.Text.Encoding.GetEncoding("UTF-8"));
    25     }
    26     else
    27     {
    28         sw = new StreamWriter(filename, false, System.Text.Encoding.GetEncoding("UTF-8"));
    29     }
    30 
    31     StringBuilder dataRow = new StringBuilder("");
    32     dataRow.Append(logContent);
    33     sw.WriteLine(dataRow.ToString());//写入内容
    34     sw.Close();
    35 }
  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/haibing0107/p/7566914.html
Copyright © 2011-2022 走看看