Logging showcasing thread id discovery, parent stack frame self-discovery ..
/// <summary>
/// strictly stateless
/// </summary>
class Log
{
private static readonly log4net.ILog log4n = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public const string FORMAT_BEGIN = " vvv {0} vvv";
public const string FORMAT_CLOSE = " ^^^ {0} ^^^";
/// <summary>
/// This method conveniently logs to console, IDE output window and log4net
/// </summary>
/// <param name="o"></param>
public static void Info(Object o, int levels = 2, bool isStackTraceNeeded = false)
{
/*The thread id seen by log4net. 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]));
for (int i = 2; i <= levels; ++i)
{
if (frames.Length > i)
{
locator.Append("<-" + StackFrameToStr(frames[i]));
}
}
//if (frames.Length > 2)locator.Append("<-" + StackFrameToStr(frames[2]));
var locator2 = "{" + locator.ToString().Trim() + "} ";
var msg1 = (o ?? "_nul_");
var msg2 = thId + locator2 + msg1;