我正在寻找一种记录UserActions / Requests的方法。我倾向于将详细信息记录到文本文件中。 LOG细节以树状(分层)结构组织,以便它可读并以逐步的方式显示方法名称。 (如果请求经过多种方法)
我有一个示例应用程序可以正常工作,但它不是应该如何。请考虑以下类别。
节点类,它是制作树状(分层)结构的模板。它具有名称(方法名称),时间和名为Children的列表类型段等属性。
public class Node
{
public string Name; // method name
public DateTime Time; // time when accessed
public List<Node> Children;
public static void PrintTree(Node tree)
{
string temp = "";
List<Node> firstStack = new List<Node>();
firstStack.Add(tree);
List<List<Node>> childListStack = new List<List<Node>>();
childListStack.Add(firstStack);
while (childListStack.Count > 0)
{
List<Node> childStack = childListStack[childListStack.Count - 1];
if (childStack.Count == 0)
{
childListStack.RemoveAt(childListStack.Count - 1);
}
else
{
tree = childStack[0];
childStack.RemoveAt(0);
string indent = "";
for (int i = 0; i < childListStack.Count - 1; i++)
{
indent += (childListStack[i].Count > 0) ? "| " : " ";
}
temp = indent + "+- " + tree.Name + " (" + tree.Time + ")";
Console.WriteLine(indent + "+- " + tree.Name + " (" + tree.Time + ")");
File.AppendAllText(@"C:\Users\aimalkhan\Desktop\Log Work\Log.txt", temp + Environment.NewLine);
if (tree.Children != null)
{
if (tree.Children.Count > 0)
{
childListStack.Add(new List<Node>(tree.Children));
}
}
}
}
File.AppendAllText(@"C:\Users\aimalkhan\Desktop\Log Work\Log.txt", Environment.NewLine + "*************************************************************" + Environment.NewLine);
}
}
我的示例员工类,其示例方法 SetEmployeeName()的 ANODTR 参数 NODE类型记录目的。
public class Employee
{
private string FirstName { get; set; }
private string LastName { get; set; }
public string SetEmployeeName(string firstName, string lastName, Node node)
{
node.Name = "Class Name: " +this.GetType().Name + ", Calling method Name: setEmployeeName()";
node.Time = DateTime.Now;
this.FirstName = firstName;
this.LastName = lastName;
return this.FirstName + " " + this.LastName;
}
public void CompleteTask(string empName, string taskName)
{
Console.WriteLine("Employee: " + empName + " is completing the task: " + taskName);
}
}
最后这就是我如何使用上述代码示例。
Node root = new Node();
root.Name = "ClassName: Main";
root.Time = DateTime.Now;
root.Children = new List<Node>();
Node child = new Node();
Employee emp = new Employee();
emp.SetEmployeeName("John", "D", child);
root.Children.Add(child);
Node.PrintTree(root);
的方式
现在我的问题是,每当我需要子信息日志时,传递NODE类型para对我来说真的很头疼。这可能是一些如何以任何可能的方式集中化?有比这更好的方法吗?真的很感激一点指导。