Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public sealed class Tracing {
- public static event Action<string> Traced;
- static void OnTraced(string msg) {
- if(Traced == null)
- return;
- Traced(msg);
- }
- // Methods
- private Tracing() {
- }
- [Conditional("TRACE")]
- public static void Assert(bool condition,string msg) {
- if(!condition) {
- Trace.WriteLine(msg);
- Trace.Assert(condition,msg);
- }
- }
- [Conditional("TRACE")]
- public static void ExitTracing() {
- }
- [Conditional("TRACE")]
- public static void InitTracing() {
- }
- [Conditional("TRACE")]
- public static void TraceCall() {
- StackTrace trace = new StackTrace();
- if((trace != null) && (trace.FrameCount > 1)) {
- StackFrame startFrame = trace.GetFrame(1);
- TraceCall("",startFrame,trace.FrameCount);
- }
- else {
- TraceCall("",null,0);
- }
- }
- [Conditional("TRACE")]
- public static void TraceCall(string msg) {
- StackTrace trace = new StackTrace();
- if((trace != null) && (trace.FrameCount > 1)) {
- StackFrame startFrame = trace.GetFrame(1);
- TraceCall(msg,startFrame,trace.FrameCount);
- }
- else {
- TraceCall(msg,null,0);
- }
- }
- [Conditional("TRACE")]
- private static void TraceCall(string msg,StackFrame startFrame,int indent) {
- try {
- if(startFrame != null) {
- StringBuilder builder = new StringBuilder();
- MethodBase method = startFrame.GetMethod();
- while(indent-- > 0) {
- builder.Append(" ");
- }
- builder.Append("--> " + method.DeclaringType.Name + "." + method.Name + "()");
- if(!string.IsNullOrEmpty(msg)) {
- builder.Append(": " + msg);
- }
- TraceMsg(builder.ToString());
- }
- else {
- TraceMsg("Method Unknown: " + msg);
- }
- Trace.Flush();
- }
- catch {
- TraceMsg(msg);
- }
- }
- [Conditional("TRACE")]
- public static void TraceInfo(string msg) {
- TraceMsg(msg);
- }
- [Conditional("TRACE")]
- public static void TraceMsg(string msg) {
- try {
- string message = "[" + DateTime.Now.ToString("HH:mm:ss:ffff",CultureInfo.InvariantCulture) + "] - " + msg;
- Trace.WriteLine(message);
- Trace.Flush();
- OnTraced(message);
- }
- catch {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment