在服务器上记录错误日志

using System.IO;

public class DebugLog
{
    public DebugLog()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

 

/// <summary>
  /// 根据日志的主文件名创建日志 
  /// </summary>
  /// <param name="CreateTime">创建日志的时间</param>
  /// <param name="Body">日志内容主体</param>
  /// <param name="FILE_PREFIX">文件名的关键主体</param>
    public static void WriteLog(DateTime CreateTime, string Body,string FILE_PREFIX)
    {
        try
        {
            lock (typeof(DebugLog))
            {
                string strLogPath = string.Empty;
                strLogPath = ConfigurationManager.AppSettings["LogPath"].ToString();
                if (string.IsNullOrEmpty(strLogPath))
                {
                    return;
                }

                //根据当天日期得出当天日志的文件名
                string strLogFileName = strLogPath + FILE_PREFIX + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter logWriter = null;

                //判断日志文件是否存在,存在则续行,不存在则创建并书写标题
                if (File.Exists(strLogFileName))
                {

                    logWriter = File.AppendText(strLogFileName);
                }
                else
                {
                    //创建文件
                    logWriter = File.CreateText(strLogFileName);

                }

                string strLogContent = string.Empty;
                string strLogId = string.Empty;
                strLogId = DateTime.Now.Ticks.ToString();
                strLogContent = "------------------------------------------------------------------------" + "\r\n";
                strLogContent = CreateTime + "\t" + "\r\n" + Body;
                strLogContent = strLogContent + "\r\n" + "------------------------------------------------------------------------";
                logWriter.WriteLine(strLogContent);
                logWriter.Close();
            }
        }
        catch { }
    }

}

转载于:https://www.cnblogs.com/Tiffany_2008/archive/2009/06/19/1506872.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值