using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Harlinn.Common
{
public class Logger
{
private static bool enableTracing;
private static bool enableMemoryTracking;
private static bool disableDebugBreak;
public static void LogException(log4net.ILog log, Exception exc, System.Reflection.MethodBase method)
{
string methodName = method.ToString();
string exceptionMessageText = exc.Message;
string messageText = string.Format("Exception in {0} - {1}", methodName, exceptionMessageText);
log.Error(messageText, exc);
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Log(1, "Exception", messageText);
System.Diagnostics.Debugger.Break();
}
#endif
}
public static void LogException(log4net.ILog log, Exception exc, System.Reflection.MethodBase method, string format, params string[] args)
{
string methodName = method.ToString();
string exceptionMessageText = exc.Message;
string additionalInfo = string.Format(format, args);
string messageText = string.Format("Exception in {0} - {1} {2} {3}", methodName, exceptionMessageText, Environment.NewLine + additionalInfo);
log.Error(messageText, exc);
#if DEBUG
if ((System.Diagnostics.Debugger.IsAttached) && (disableDebugBreak == false))
{
System.Diagnostics.Debugger.Log(1, "Exception", messageText);
System.Diagnostics.Debugger.Break();
}
#endif
}
public static bool EnableTracing
{
get
{
return enableTracing;
}
set
{
if (enableTracing == value)
return;
enableTracing = value;
}
}
public static bool EnableMemoryTracking
{
get
{
return enableMemoryTracking;
}
set
{
if (enableMemoryTracking == value)
return;
enableMemoryTracking = value;
}
}
public static bool DisableDebugBreak
{
get
{
return disableDebugBreak;
}
set
{
if (disableDebugBreak == value)
return;
disableDebugBreak = value;
}
}
public static void Entering(log4net.ILog log, System.Reflection.MethodBase method)
{
if (EnableTracing)
{
if (EnableMemoryTracking)
{
long memoryUsed = GC.GetTotalMemory(false);
string methodName = method.ToString();
string message = string.Format("Entering {0} - Allocated {1}", methodName, memoryUsed);
log.Debug(message);
}
else
{
string methodName = method.ToString();
string message = string.Format("Entering {0}", methodName);
log.Debug(message);
}
}
}
public static void Leaving(log4net.ILog log, System.Reflection.MethodBase method)
{
if (EnableTracing)
{
if (EnableMemoryTracking)
{
long memoryUsed = GC.GetTotalMemory(false);
string methodName = method.ToString();
string message = string.Format("Leaving {0} - Allocated {1}", methodName, memoryUsed);
log.Debug(message);
}
else
{
string methodName = method.ToString();
string message = string.Format("Leaving {0}", methodName);
log.Debug(message);
}
}
}
}
}