【问题标题】:writelog.cs; error log.txt写日志.cs;错误日志.txt
【发布时间】:2009-09-30 06:54:05
【问题描述】:

我正在创建一个名为 writelog.cs 的通用类 这是存储我所有程序中调用的常用方法(aspx页面) 之后,无论产生什么错误,都会出现一个错误 log.txt

这是我的 writelog.cs 代码 我得到了这个错误:名称'Global'在当前上下文中不存在

/// <summary>
/// Summary description for Writelog
/// </summary>
/// <param name="Desc">Desc</param>
/// <param name="ID">ID</param>
/// <param name="Pg">Program</param>
/// <param name="Msg">System Error Message</param>
public class Writelog
{
    public static void WritelogDesc(string Desc, string ID, string Pg, string Msg)
    {
        StringBuilder SB = new StringBuilder();

        string path = Global.getLogFilePath();

        SB.Append(DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToShortTimeString());
        SB.Append(" | ");
        SB.Append(Desc);
        SB.Append(" | ");
        SB.Append(ID);
        SB.Append(" | ");
        SB.Append(Pg);
        SB.Append(" | ");
        SB.Append(Msg);

        if (!File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(SB.ToString());
            }
        }

        using (StreamWriter sw = File.AppendText(path))
        {
            Writelog.WritelogDesc();
            sw.WriteLine(SB.ToString());
        }

    }
}

这就是我如何调用 writelog.cs

Writelog.WritelogDesc();

【问题讨论】:

  • Global.getLogFilePath 是在哪里定义的,它是什么样的?
  • 编辑您的代码并将其格式化为可读形式。
  • 你不能这样调用你的方法 - 它有 4 个参数,而你没有指定任何参数。
  • 如果我这样称呼,对吗? Writelog.WritelogDesc(Desc, ID, Pg, Msg);

标签: c# class text methods


【解决方案1】:

你有什么理由不使用像log4net 这样的登录框架?这些框架为您提供了更多功能,因此您可以滥用它并继续您想做的工作。

如果您想滚动自己的日志记录是有原因的,您确定您已添加对包含全局命名空间的程序集的引用吗?

【讨论】:

  • 我同意,log4net、NLog、Enterprise Library、ELAMH 都是很棒的日志框架,你不需要自己动手。
  • 嗯。因为我正在做我的期末项目,这是负责老师的要求。
  • 很公平。您是否检查了具有全局命名空间的程序集是否已被引用?
【解决方案2】:
StreamWriter sw = (!File.Exists(path))? File.CreateText(path):File.AppendText(path);  
sw.WriteLine(SB.ToString());      

只使用类名和函数名和参数调用这个函数

Writelog.WritelogDesc(Desc, ID, Pg, Msg);

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2012-01-30
    • 2013-12-27
    • 1970-01-01
    • 2012-05-16
    • 2013-03-16
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多