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