用文本简单的记录日志

前天,小皇帝教了一个日志功能,之前编程从来都没有想过这个问题,那就是如果程序部署到服务器之后出现错误怎么办?写一个日志跟踪就解决了大家的问题了。

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.IO;

namespace  CoreProxy
{
    
public    class  LogService
    {
        
private    bool  m_isWriteLog  =   true ;   // 是否写日志

        
private   String m_logFilePath  =  String.Empty;   // 日志路径

        
private   String m_time;   // 时间字符串

        
public    void  InitLog(String layerIndex, String logPath)
        {
            
// 日志写入路径
             if  ( ! String.IsNullOrEmpty(logPath))
            {
                
// 赋值时间字串
                m_time  =  GetTimeStr();
                
if  ( ! String.IsNullOrEmpty(logPath))
                {
                    
// 路径不存在则创建一个
                     if  ( ! Directory.Exists(logPath))
                    {
                        Directory.CreateDirectory(logPath);
                    }
                    
// 名字用实时的还是历史的
                    String logName  =   " _log_ " ;

                    
// 这样组成一个文件路径
                    String filePath  =  logPath  +   " \\ "   +  layerIndex  +  logName  +  m_time  +   " .txt " ;
                    
// 付给文件路径的地址
                    m_logFilePath  =  filePath;
                    
                }
            }
        }

        
public    void  WriteLog(String msg)
        {
            
try
            {
                
lock  ( this )
                {
                    
if  (m_isWriteLog)
                    {
                        
if  ( ! File.Exists(m_logFilePath))
                        {
                            
using  (FileStream fs  =  File.Create(m_logFilePath))
                            {
                                fs.Close();
                                fs.Dispose();
                            }
                        }
                        
using  (StreamWriter sw  =  File.AppendText(m_logFilePath))
                        {
                            sw.WriteLine(msg);
                            sw.Flush();
                            sw.Close();
                        }
                    }
                }
            }
            
catch  { }
        }
        
public   String GetTimeStr()
        {
            
return  DateTime.Now.Year  +   " _ "   +  DateTime.Now.Month  +   " _ "   +  DateTime.Now.Day;
        }

    }
}

下面带有一个简单的源代码/Files/SissyNong/Upload.rar

 

转载于:https://www.cnblogs.com/SissyNong/archive/2009/09/21/1568704.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值