【问题标题】:Creating Log file for C# windows Application为 C# windows 应用程序创建日志文件
【发布时间】:2017-04-21 10:20:00
【问题描述】:

我写了一个代码块来生成日志文件,这样我就可以得到所有被调用的函数。

我使用Microsoft.Practises.EnterpriseLibrary.Logging dll 来生成日志文件,我知道如何生成它吗?

string procName = "reports_plan_and_proj.plan_v_paid_inv_pay_method_sum";
Logger.Write("SQL:GetPlanvsPaidInvestPmtMthdSummary():: " + procName, "SQL");

【问题讨论】:

  • 你告诉我们你已经做了什么,但没有问任何问题.....
  • 我能知道如何生成它,我不明白你在这里问什么?
  • 我已经编写了上面的代码来生成日志,但是它不起作用并且没有生成日志文件,请您指导我创建日志文件。

标签: c#


【解决方案1】:

让我们以某种方式使用像这样的类

public class LogControl
{
    private static string _Path = string.Empty;
    private static bool DEBUG = true;

    public static void Write(string msg)
    {
        _Path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        try
        {
            using (StreamWriter w = File.AppendText(Path.Combine(_Path, "log.txt")))
            {
                Log(msg, w);
            }
            if (DEBUG)
                Console.WriteLine(msg);
        }
        catch(Exception e)
        {
            //Handle
        }
    }

    static private void Log(string msg, TextWriter w)
    {
        try
        {
            w.Write(Environment.NewLine);
            w.Write("[{0} {1}]", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
            w.Write("\t");
            w.WriteLine(" {0}", msg);
            w.WriteLine("-----------------------");
        }
        catch(Exception e)
        {
                        //Handle
        }
    }
}

您使用LogControl.Write("I want to log this") 调用它 它会产生这样的输出:[1:19:34 PM Friday, April 21, 2017] I want to log this -------------

【讨论】:

  • 这行得通,但我需要在我的整个应用程序中重写它
  • 为什么要重写?我假设如果您现在没有记录任何内容,那么那里只会出现几次。否则您可以使用您喜欢的 IDE 中实现的“查找和替换”功能吗?
  • 进展如何?我们没有你的任何消息
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多