C#快速创建日志-创新互联

public static void WriteLog(string msg)

目前成都创新互联公司已为1000多家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器托管、企业网站设计、平定网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

  {

  //该日志文件会存在windows服务程序目录下

  //string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";

  //该日志文件会存在最后一个硬盘下目录下

  System.IO.DriveInfo[] allDrives = System.IO.DriveInfo.GetDrives();

  string path = allDrives.Last().Name + "LYSpecServiceLog" ;

  if (!Directory.Exists(path))

  {

  Directory.CreateDirectory(path);

  }

  path = path  + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";         

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

  {

  msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "\t" + msg + "\r\n";

  msg += "----------------------------------------------------------------------\r\n";

  byte[] bs = Encoding.UTF8.GetBytes(msg);

  fs.Write(bs, 0, bs.Length);

  fs.Close();             

  }

  }

方法2

public static void Save(string gasName, double[] value)

  {

  string configName = Directory.GetCurrentDirectory() + "\\Config\\";

  if (!Directory.Exists(configName))

  {

  Directory.CreateDirectory(configName);

  }

  StringBuilder sb = new StringBuilder(5120);

  for (int i = 0; i< value.Length; i++)

  {

  sb.Append(i + 1);

  sb.Append('\t');

  sb.Append(value[i]);

  sb.Append(Environment.NewLine);

  }

  File.WriteAllText(configName + $"{gasName}.txt", sb.ToString());

  }

方法3、

public static void WriteLog(string msg)

  {

  string path = AppDomain.CurrentDomain.BaseDirectory + "LYLog";

  if (!Directory.Exists(path))

  {

  Directory.CreateDirectory(path);

  }

  path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

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

  {

  msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "\t" + msg + "\r\n";

  msg += "----------------------------------------------------------------------\r\n";

  byte[] bs = Encoding.UTF8.GetBytes(msg);

  fs.Write(bs, 0, bs.Length);

  fs.Close();

  }

  }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前标题:C#快速创建日志-创新互联
标题来源:http://azwzsj.com/article/csched.html