使用哪种设计模式来创建此记录器 [英] Which Design pattern is used to create this Logger
本文介绍了使用哪种设计模式来创建此记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
static void Main(string[] args)
{
int strText;
int a, b ,c;
StreamWriter logFile;
Console.Write("Name: ");
try
{
strText = Convert.ToInt32(Console.ReadLine());
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a / b;
if (!File.Exists("Log.txt"))
{
logFile = new StreamWriter("Log.txt");
}
else
{
logFile = File.AppendText("Log.txt");
}
logFile.WriteLine(DateTime.Now);
logFile.WriteLine(strText.ToString());
logFile.WriteLine();
Console.WriteLine("Log file saved successfully!");
}
catch (Exception ex)
{
if (!File.Exists("Log.txt"))
{
logFile = new StreamWriter("Log.txt");
}
else
{
logFile = File.AppendText("Log.txt");
}
Console.WriteLine("Error: " + ex.Message);
logFile.WriteLine("Error: " + ex.Message);
}
logFile.Close();
Console.Read();
}
}
}
推荐答案
如果尝试询问使用哪种模式",则应该使用单例进行日志记录.
但是,如果目的是真正地记录日志,而不是重新发明轮子,则可以尝试使用Log4Net之类的成熟框架.
-Milind
Hi,
If you are trying to ask "which pattern to use", you should be using singleton for logging purposes.
However, if the purpose is really logging, rather than re-inventing wheel, you can try using proven framework like Log4Net.
-Milind
这篇关于使用哪种设计模式来创建此记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文