A simple Tracer using entLib (draft)
Update (24/6/05): the original code below (section after TODO) should be much simpler and as follow:
We check that Tracer is Active using the configSettings. In addition, we don't need to write the method name because this will be done by using the entLib Tracer block that we can add to wrap our application method/s. The "before check" string will be written to the trace file in any call to Write because its located before the IsTraceEnabled and I add it only to watch it myself.
public static void Write(params object[] args)
{
Logger.Write("before check!!", TracerCategory);
if (IsTracingEnabled())
{
StringBuilder sbTrace = new StringBuilder();
StringWriter writer = new StringWriter(sbTrace);
foreach (object obj in args)
{
writer.Write(obj);
writer.Write(Environment.NewLine);
}
//TODO: add the args
//TODO: check if args contains dataset table/s
//TODO: foreach table write its Xml representation to the log
Logger.Write(sbTrace.ToString(), TracerCategory);
}
}
public static bool IsTracingEnabled()
{
context = ConfigurationManager.GetCurrentContext();
LoggingSettings settings = (LoggingSettings) context.GetConfiguration(LoggingSettings.SectionName);
return settings.TracingEnabled;
}
** TODO: I need to skip the methods body when trace is disabled
This is a draft that will be check during the week - I hope its working fine. Its for using in a sequence serial senario methods (not synchronic methods). By using the TextExceptionFormatter I guarantee that the machine/process/user context details will be written to the trace file as well.
public class TracerHelper
{
public const string TracerCategory = "DoTrace"; //write to a trace file
private static string contextNamespace;
private static string contextClassName;
private static string contextModuleName;
protected TracerHelper()
{
}
public static void StartTrace(string namespaceName, string className, string moduleName, params object[] args)
{
//set the module global name
contextNamespace = namespaceName;
contextClassName = className;
contextModuleName = moduleName;
StringBuilder sbTrace = new StringBuilder();
StringWriter writer = new StringWriter(sbTrace);
writer.WriteLine("Start Trace: " + contextNamespace + "." + contextClassName + "." + contextModuleName + Environment.NewLine);
//TODO: add the args
//TODO: check if args contains dataset. arrays, hash or simple arguements
//TODO: foreach dataset write its Xml representation to the log
TextExceptionFormatter formatter = new TextExceptionFormatter(writer, null);
formatter.AdditionalInfo.Add("argName", "");
Logger.Write(sbTrace.ToString(), TracerCategory);
}
public static void EndTrace()
{
Logger.Write("End Trace:" + contextNamespace + "." + contextClassName + "." +contextModuleName , TracerCategory);
contextNamespace = string.Empty;
contextClassName = string.Empty;
contextModuleName = string.Empty;
}
public static void EndTrace(params object[] args)
{
//TODO: add the args value/s to the logger
Logger.Write("End Trace:" + contextNamespace + "." + contextClassName + "." + contextModuleName, TracerCategory);
contextNamespace = string.Empty;
contextClassName = string.Empty;
contextModuleName = string.Empty;
}
}