很多人的程序在本地運行是好的,但是發(fā)布在服務(wù)器上后就會有各種各樣的問題,但是服務(wù)器上又不能直接調(diào)試,所以直接讀寫本地日志成為解決問題的關(guān)鍵,我這個方法,會在發(fā)布網(wǎng)站的根目錄自動創(chuàng)建 log.txt,并且會自動拼接日志信息。
日志可在如下找到:
代碼如下:
1、引用
1
2
3
|
using system; using system.io; using system.text; |
2、具體方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void writelog(string msg) { streamwriter stream; //寫入日志內(nèi)容 string path = appdomain.currentdomain.basedirectory; //檢查上傳的物理路徑是否存在,不存在則創(chuàng)建 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#快速寫本地日志方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/highest/p/8257322.html