软件编程
位置:首页>> 软件编程>> C#编程>> c#快速写本地日志方法

c#快速写本地日志方法

作者:思明  发布时间:2021-08-24 09:31:17 

标签:c#,本地,日志

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

日志可在如下找到:

c#快速写本地日志方法

代码如下:

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();
 }

来源:http://www.cnblogs.com/highest/p/8257322.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com