using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static CancellationTokenSource cts = new CancellationTokenSource();
static void Main(string[] args)
{
Console.WriteLine("Main threadId is:" +
Thread.CurrentThread.ManagedThreadId);
//Task.Factory.StartNew(ShowMessage, cts.Token);
Task my_task = new Task(ShowMessage, cts.Token);
my_task.Start();
Thread.Sleep(3000);
cts.Cancel();
//Thread thread = new Thread(new ThreadStart(message.ShowMessage));
//thread.Start();
Console.WriteLine("Do something ..........!");
Console.WriteLine("Main thread working is complete!");
Console.ReadKey();
}
static void ShowMessage()
{
while (!cts.IsCancellationRequested)
{
Console.WriteLine(DateTime.Now);
Thread.Sleep(1000);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace WindowsFormsApplication1
{
class LogClass
{
/**/
/// <summary>
/// 寫入日誌文件
/// </summary>
/// <param name="input"></param>
public static void WriteLogFile(string input)
{
/**/
///指定日誌文件的目錄
string fname = Directory.GetCurrentDirectory() + "\\LogFile.txt";
/**/
///定義文件信息對象
FileInfo finfo = new FileInfo(fname);
if (!finfo.Exists)
{
FileStream fs;
fs = File.Create(fname);
fs.Close();
finfo = new FileInfo(fname);
}
/**/
///判斷文件是否存在以及是否大於2K
if (finfo.Length > 1024 * 1024 * 10)
{
/**/
///文件超過10MB則重命名
File.Move(Directory.GetCurrentDirectory() + "\\LogFile.txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.txt");
/**/
///刪除該文件
//finfo.Delete();
}
//finfo.AppendText();
/**/
///建立只寫文件流
using (FileStream fs = finfo.OpenWrite())
{
/**/
///根據上面建立的文件流建立寫數據流
StreamWriter w = new StreamWriter(fs);
/**/
///設置寫數據流的起始位置爲文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
/**/
///寫入「Log Entry : 」
//w.Write("\n\rLog Entry : ");
/**/
///寫入當前系統時間並換行
w.Write("\n\r{0} {1} ", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
/**/
///寫入日誌內容並換行
w.Write(input + "\n\r");
/**/
///寫入------------------------------------「並換行
w.Write("------------------------------------\n\r");
/**/
///清空緩衝區內容,並把緩衝區內容寫入基礎流
w.Flush();
/**/
///關閉寫數據流
w.Close();
}
}
}
}
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Diagnostics;
5: using System.IO;
6:
7: namespace ProjectLog
8: {
9: public class ProjectTraceListener : TraceListener
10: {
11: public string FilePath { get; private set; }
12:
13: public ProjectTraceListener(string filePath)
14: {
15: FilePath = filePath;
16: }
17:
18: public override void Write(string message)
19: {
20: File.AppendAllText(FilePath, message);
21: }
22: public override void WriteLine(string message)
23: {
24: File.AppendAllText(FilePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + message + Environment.NewLine);
25: }
26: public override void Write(object o, string category)
27: {
28: string message = string.Empty;
29: if (!string.IsNullOrEmpty(category))
30: {
31: message = category + ":";
32: }
33: if (o is Exception)//若是參數對象o是與Exception類兼容,輸出異常消息+堆棧,不然輸出o.ToString()
34: {
35: var ex = (Exception)o;
36: message += ex.Message + Environment.NewLine;
37: message += ex.StackTrace;
38: }
39: else if(null != o)
40: {
41: message += o.ToString();
42: }
43:
44: WriteLine(message);
45: }
46: }
47: }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new MyTraceListener());
Test();
Console.ReadKey();
}
static void Test()
{
try
{
int i = 0;
Console.WriteLine(5 / i);
}
catch (Exception ex)
{
Trace.WriteLine("出現異常:" + ex.Message +"1");//記錄日誌
Trace.TraceError("出現異常:" + ex.Message + "2");//記錄日誌
Trace.TraceWarning("出現異常:" + ex.Message + "3");//記錄日誌
Trace.TraceInformation("出現異常:" + ex.Message + "4");//記錄日誌
Trace.Flush();//當即輸出
}
}
}
class MyTraceListener : TraceListener
{
public override void Write(string message)
{
File.AppendAllText("e:\\1.log", message);
}
public override void WriteLine(string message)
{
File.AppendAllText("e:\\1.log", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + message + Environment.NewLine);
}
}
}
ConsoleApplication1.vshost.exe Error: 0 : 2016-12-04 22:53:09 出現異常:Attempted to divide by zero.
2016-12-04 23:13:14 出現異常:Attempted to divide by zero.
2016-12-04 23:14:04 出現異常:Attempted to divide by zero.1
2016-12-04 23:14:04 出現異常:Attempted to divide by zero.2
2016-12-04 23:14:04 出現異常:Attempted to divide by zero.3
2016-12-04 23:14:04 出現異常:Attempted to divide by zero.4
2016-12-04 23:17:14 出現異常:Attempted to divide by zero.1
ConsoleApplication1.vshost.exe Error: 0 : 2016-12-04 23:17:14 出現異常:Attempted to divide by zero.2
ConsoleApplication1.vshost.exe Warning: 0 : 2016-12-04 23:17:14 出現異常:Attempted to divide by zero.3
ConsoleApplication1.vshost.exe Information: 0 : 2016-12-04 23:17:14 出現異常:Attempted to divide by zero.4
2016-12-04 23:17:25 出現異常:Attempted to divide by zero.1
ConsoleApplication1.vshost.exe Error: 0 : 2016-12-04 23:17:25 出現異常:Attempted to divide by zero.2
ConsoleApplication1.vshost.exe Warning: 0 : 2016-12-04 23:17:25 出現異常:Attempted to divide by zero.3
ConsoleApplication1.vshost.exe Information: 0 : 2016-12-04 23:17:25 出現異常:Attempted to divide by zero.4
http://blog.csdn.net/qinyuanpei/article/details/53054002ide