OutputDebugString is what gets called by the TRACE macro in an MFC app.
Right?
You can use Debug.WriteLine or Trace.WriteLine after including the line
using System.Diagnostics
Another way is using lo4net.
Simple:
static public void DebugOut(string msg)
{
StackTrace st = new StackTrace(false);
string caller = st.GetFrame(1).GetMethod().Name;
Debug.WriteLine(caller + ": " + msg);
}
public void MyBuggyFunction()
{
DebugOut("hey there");
//...
}
//complicated: full call stack
// The namespaces that ShortenType will remove from a type name.
static private string[] assumedPrefixes =
new string[] {
"System.Windows.",
"System."
};
// Returns a short name for the given type.
static private string ShortTypeName(System.Type type)
{
string typeName = type.ToString();
foreach (string pref in assumedPrefixes)
{
if (typeName.StartsWith(pref))
{
return typeName.Substring(pref.Length);
}
}
return typeName;
}
// Return a string description of the stack, with parameter types.
static public string GetDetailedStack(int skip, string prefix)
{
string s = "";
StackTrace st = new StackTrace(true);
for (int i = skip; i < st.FrameCount; i++)
{
StackFrame sf = st.GetFrame(i);
MethodBase meth = sf.GetMethod();
string method = ShortTypeName(meth.DeclaringType) + "." + meth.Name + "(";
bool first = true;
foreach (ParameterInfo p in meth.GetParameters())
{
if (!first)
{
method += ", ";
}
method += ShortTypeName(p.ParameterType);
first = false;
}
method += ")";
s += prefix + method + "/n";
}
return s;
}
// Write a debug message, with the full stack.
static public void DebugOutStack(string msg)
{
StackTrace st = new StackTrace(false);
// The real caller is one frame up the stack.
string caller = st.GetFrame(1).GetMethod().Name;
Debug.WriteLine(caller + ": " + msg + "/n" + GetDetailedStack(2, " "));
}