using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace mySerialPort.Com
{
class Log
{
private static string debug_grade = "DEBUG";
public static StackFrame stackFrame = new StackFrame();
private static TextWriterTraceListener listner = null;
// 输出跟踪文件
private static string TRACE_FILE = null;
private static Mutex mute = new Mutex(true);
/// <summary>
/// 初始化路径
/// </summary>
/// <param name="path"></param>
public static void InitFilePath(string path)
{
mute.WaitOne();
TRACE_FILE = null;
TRACE_FILE = string.Copy(path);
mute.ReleaseMutex();
}
/// <summary>
/// 打印消息
/// </summary>
/// <param name="traceName"></param>
/// <param name="traceMessage"></param>
public static void Trace(string traceName, string traceMessage)
{
// 变量定义
DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
string file = System.Environment.CurrentDirectory + "\\" + time.ToString("yyyy_MM_dd") + ".log";
StringBuilder message = null;
if (listner == null)
{
TRACE_FILE = file;
try
{
listner = new TextWriterTraceListener(TRACE_FILE);
System.Diagnostics.Trace.Listeners.Add(listner);
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
catch (System.Exception)
{
return;
}
}
else
{
if (TRACE_FILE != file)
{
TRACE_FILE = file;
listner.Close();
listner.Dispose();
listner = new TextWriterTraceListener(TRACE_FILE);
System.Diagnostics.Trace.Listeners.Add(listner);
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
}
message = new StringBuilder();
message.Append("[");
message.Append(DateTime.Now);
message.Append("] ");
message.Append("[");
message.Append(traceName);
message.Append("] ");
message.Append(traceMessage);
switch (debug_grade)
{
case "DEBUG":
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "MESSAGE":
if (traceName.Equals("MESSAGE") ||
traceName.Equals("WARING") ||
traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "WARING":
if (traceName.Equals("WARING") ||
traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "ERROR":
if (traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace mySerialPort.Com
{
class Log
{
private static string debug_grade = "DEBUG";
public static StackFrame stackFrame = new StackFrame();
private static TextWriterTraceListener listner = null;
// 输出跟踪文件
private static string TRACE_FILE = null;
private static Mutex mute = new Mutex(true);
/// <summary>
/// 初始化路径
/// </summary>
/// <param name="path"></param>
public static void InitFilePath(string path)
{
mute.WaitOne();
TRACE_FILE = null;
TRACE_FILE = string.Copy(path);
mute.ReleaseMutex();
}
/// <summary>
/// 打印消息
/// </summary>
/// <param name="traceName"></param>
/// <param name="traceMessage"></param>
public static void Trace(string traceName, string traceMessage)
{
// 变量定义
DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
string file = System.Environment.CurrentDirectory + "\\" + time.ToString("yyyy_MM_dd") + ".log";
StringBuilder message = null;
if (listner == null)
{
TRACE_FILE = file;
try
{
listner = new TextWriterTraceListener(TRACE_FILE);
System.Diagnostics.Trace.Listeners.Add(listner);
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
catch (System.Exception)
{
return;
}
}
else
{
if (TRACE_FILE != file)
{
TRACE_FILE = file;
listner.Close();
listner.Dispose();
listner = new TextWriterTraceListener(TRACE_FILE);
System.Diagnostics.Trace.Listeners.Add(listner);
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Trace.AutoFlush = true;
}
}
message = new StringBuilder();
message.Append("[");
message.Append(DateTime.Now);
message.Append("] ");
message.Append("[");
message.Append(traceName);
message.Append("] ");
message.Append(traceMessage);
switch (debug_grade)
{
case "DEBUG":
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "MESSAGE":
if (traceName.Equals("MESSAGE") ||
traceName.Equals("WARING") ||
traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "WARING":
if (traceName.Equals("WARING") ||
traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
case "ERROR":
if (traceName.Equals("ERROR"))
System.Diagnostics.Trace.WriteLine(message.ToString());
break;
}
}
}
}