It also logs the file name, method name and line number of top 2 levels on the call stack
public static void Info(Object o, bool isStackTraceNeeded = false)
{
/*According to MSDN:
* An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host
* can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host
* can use the CLR Hosting API to schedule many managed threads against the same operating system thread,
* or to move a managed thread between different operating system threads.
*/
var winThrId = AppDomain.GetCurrentThreadId();
var thId = "/T" + Thread.CurrentThread.ManagedThreadId + "/" + winThrId + "/ ";
#region locator
StackTrace stackTrace = new StackTrace(fNeedFileInfo:true);
var frames = stackTrace.GetFrames();
StringBuilder locator = new StringBuilder(StackFrameToStr(frames[1]));
if (frames.Length > 2)
locator.Append(StackFrameToStr(frames[2]));
var locator2 = "{" + locator.ToString().Trim() + "}";
var msg = thId + locator2 + (o ?? "_nul_");
#endregion
if (isStackTraceNeeded) msg += "\n" + stackTrace;
log4n.Debug(msg);
//System.Diagnostics.Trace.Write("Trace is possible as well.");
System.Diagnostics.Debug.WriteLine(msg);
Console.WriteLine(msg);
return;
}
public static string StackFrameToStr(StackFrame f)
{
return Path.GetFileNameWithoutExtension(f.GetFileName()) + "." + f.GetMethod().Name + "." +f.GetFileLineNumber() + " ";
}
}