资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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://cdkjz.cn/article/csched.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220