当前位置: 移动技术网 > IT编程>开发语言>c# > c#快速写本地日志方法

c#快速写本地日志方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

很多人的程序在本地运行是好的,但是发布在服务器上后就会有各种各样的问题,但是服务器上又不能直接调试,所以直接读写本地日志成为解决问题的关键,我这个方法,会在发布网站的根目录自动创建 log.txt,并且会自动拼接日志信息。

日志可在如下找到:

代码如下:

1、引用

using system;
using system.io;
using system.text;

2、具体方法:

public static void writelog(string msg)
  {
   streamwriter stream;
   //写入日志内容
   string path = appdomain.currentdomain.basedirectory;
   //检查上传的物理路径是否存在,不存在则创建
   if (!directory.exists(path))
   {
    directory.createdirectory(path);
   }
   stream = new streamwriter(path + "\\log.txt", true, encoding.default);
   stream.write(datetime.now.tostring() + ":" + msg);
   stream.write("\r\n");
   stream.flush();
   stream.close();
  }

以上这篇c#快速写本地日志方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网