VC工程的output窗口中输出成 file(line): 的形式就可以在output窗口中双击定位源代码位置。
C#中要怎么做呢?
直接输出成 file:line_no:colum_no 格式似乎并不奏效。
做技术的朋友推荐使用log4net,不过,这太重量级了,我比较喜欢轻量级的,可以简单配置的东西。
又仔细看了看,原来给C#环境用的output窗口是以filepath(line, colum):格式来定位的,只要在output窗口中输出成上述格式就可以在output窗口上双击定位调用点了。
代码很简单,不需要多解释。稍微有点技巧的地方就是sf = st.GetFrame(2);
为什么是2呢?因为从调用点到达真正输出文本的函数有2层函数调用,就这么简单。
[code]
using System;
using System.Diagnostics;
namespace DCCore
{
public enum DebugType
{
LogSysEntry = 1,
LogScreen = 2,
LogSysAndScreen = 3,
}
public enum DebugLevel
{
LevelDisable = 0,
LevelInfo = 1,
LevelWarning = 2,
LevelAll = 3,
}
public class GmDebug
{
private EventLog _eventLog;
private int _logType = (int)DebugType.LogScreen;
private int _logLevel = (int)DebugLevel.LevelAll;
/*
* 记录一条常规信息
*/
public void info(string msg)
{
if ((_logLevel & (int)DebugLevel.LevelInfo) == 0)
return;
String str = format("[INFO] ", msg);
if ((_logType & (int)DebugType.LogSysEntry) == (int)DebugType.LogSysEntry)
_eventLog.WriteEntry(str, EventLogEntryType.Information);
if ((_logType & (int)DebugType.LogScreen) == (int)DebugType.LogScreen)
Debug.WriteLine(str);
}
/*
* 记录一条警告信息
*/
public void warning(string msg)
{
if ((_logLevel & (int)DebugLevel.LevelWarning) == 0)
return;
String str = format("[WARN] ", msg);
if ((_logType & (int)DebugType.LogSysEntry) == (int)DebugType.LogSysEntry)
_eventLog.WriteEntry(str, EventLogEntryType.Warning);
if ((_logType & (int)DebugType.LogScreen) == (int)DebugType.LogScreen)
Debug.WriteLine(str);
}
/*
* 记录一条错误信息
*/
public void error(string msg)
{
//if ((_logLevel & (int)DebugLevel.LevelError) == 0)
// return;
String str = format("[ERRO] ", msg);
if ((_logType & (int)DebugType.LogSysEntry) == (int)DebugType.LogSysEntry)
_eventLog.WriteEntry(str, EventLogEntryType.Error);
if ((_logType & (int)DebugType.LogScreen) == (int)DebugType.LogScreen)
Debug.WriteLine(str);
}
/*
* 设定消息分类来源
*/
public GmDebug(string source)
{
_eventLog = new EventLog();
_eventLog.Source = source;
}
/*
* 设定消息记录目标
*/
public void setLogType(DebugType dbgType)
{
_logType = (int)dbgType;
}
/*
* 设定调试信息输出级别
*/
public void setLogLevel(DebugLevel dbgLevel)
{
_logLevel = (int)dbgLevel;
}
/*
* 内部调用拼字符串功能
*/
private String format(String headStr, String str)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
System.Diagnostics.StackFrame sf = null;
int count = st.FrameCount;
sf = st.GetFrame(2);
String realStr = String.Format("{0}({1},{2}): {3}: {4}", sf.GetFileName(), sf.GetFileLineNumber(), sf.GetFileColumnNumber(), headStr, str);
return realStr;
}
}
}
[/code]