What is the C# equivalent to C++ OutputDebugString?

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, "   "));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值