using System; using System.Diagnostics; using System.Threading; using System.Text; using System.Reflection; using System.Runtime.Remoting.Messaging; // Filename: MarketDataTrace.cs // Author:Sean Kessler // Date:11/2005 namespace MarketData { public enum LogLevel : uint { NONE=0x0000, INFO=0x0002, DEBUG=0x0004, VERBOSE=0x0008 }; /// MarketDataTrace - Utility for . public class MDTrace { private static LogLevel logLevel=LogLevel.DEBUG; /// MarketDataTrace - Private constructor prevents instantiation. /// none private MDTrace() { } /// LogLevel - Get/Set Log level. /// The log level. /// LogLevel public static LogLevel LogLevel { get{return logLevel;} set{logLevel=value;} } /// WriteLine - Writes a line of text to trace log. /// string content of message to write. /// void // [Conditional("TRACE")] public static void Write(LogLevel logLevel,string message) { if(MDTrace.logLevelWriteLine - Writes a line of text to trace log. /// string content of message to write. /// void // [Conditional("TRACE")] public static void WriteLine(string message) { WriteLine(LogLevel.DEBUG,GetCallerIP()+GetThreadRep()+GetLogLevelRep()+"["+DateTime.Now.ToString()+"]"+" "+GetMethodInfo()+message); Console.WriteLine(message); } /// WriteLine - Writes a line of text to trace log. /// string content of message to write. /// void // [Conditional("TRACE")] public static void WriteLine(LogLevel logLevel,Exception exception) { if(MDTrace.logLevelWriteLine - Writes a line of text to trace log. /// string content of message to write. /// void // [Conditional("TRACE")] public static void WriteLine(LogLevel logLevel,string message) { if(MDTrace.logLevelWriteLine - Writes a line of text to trace log. /// string content of message to write. /// void public static void WriteLine(LogLevel logLevel,ConsoleColor consoleColor,string message) { if(MDTrace.logLevelIndent - set trace log indentation. /// void // [Conditional("TRACE")] public static void Indent() { Trace.Indent(); } /// Unindent - set trace log indentation back. /// void // [Conditional("TRACE")] public static void Unindent() { Trace.Unindent(); } /// Flush - Flush trace log buffers to disk. /// void // [Conditional("TRACE")] public static void Flush() { Trace.Flush(); } /// GetLogLevel - Return current log level. /// LogLevel public static LogLevel GetLogLevel(String strLogLevel) { if(strLogLevel.Equals("debug"))return LogLevel.DEBUG; else if(strLogLevel.Equals("verbose"))return LogLevel.VERBOSE; else if(strLogLevel.Equals("info"))return LogLevel.INFO; else return LogLevel.NONE; } /// GetLogLevel - Return current log level. /// LogLevel private static string GetLogLevelRep() { if (MDTrace.logLevel == LogLevel.DEBUG) return "[TRACE.DEBUG]"; else if (MDTrace.logLevel == LogLevel.VERBOSE) return "[TRACE.VERBOSE]"; else if (MDTrace.logLevel == LogLevel.INFO) return "[TRACE.INFO]"; else return "[TRACE.NONE]"; } /// GetThreadRep - Return threading information. /// LogLevel private static string GetThreadRep() { return "[Thread="+Thread.CurrentThread.GetHashCode()+"]"; } /// GetMethodInfo - Returns information about the calling method 2 frames up. /// String private static String GetMethodInfo() { StringBuilder sb=new StringBuilder(); StackFrame frame=new StackFrame(2,true); MethodBase methodBase=frame.GetMethod(); ParameterInfo[] parameters=methodBase.GetParameters(); sb.Append("[").Append(methodBase.DeclaringType.FullName).Append("::").Append(methodBase.Name).Append("("); for(int index=0;indexGetCallerIP - Returns the calling methods IP address. /// String private static String GetCallerIP() { String hostName=(String)CallContext.GetData("HostName"); String hostAddress=(String)CallContext.GetData("HostAddress"); if(null!=hostName)hostName=hostName.Split('.')[0]; if(null==hostName && null==hostAddress)return "[LOCAL]"; return "["+hostAddress+"->"+hostName+"]"; } } }