自己写的一个日志记录类

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Net;
None.gif
None.gif
namespace  Utils
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public class LogWritter
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 禁止创建同时存在多个对象
ExpandedSubBlockStart.gifContractedSubBlock.gif
        private LogWritter()dot.gif{}
InBlock.gif        
private static LogWritter m_logger = null;
InBlock.gif        
public static LogWritter CreateInstance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m_logger == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_logger 
= new LogWritter();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return m_logger;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//设置保存文件位置
InBlock.gif
        private string m_filePath = string.Empty;
InBlock.gif        
public void SetFilePath(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!m_filePath.Equals(fileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_filePath 
= fileName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写日志,时间,机器名,日志内容
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="content"></param>

InBlock.gif        public void WriteLog(string content)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
string hostName = Dns.GetHostName();
InBlock.gif            
if (!File.Exists(m_filePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FileStream fStream 
= new FileStream(m_filePath, FileMode.CreateNew);
InBlock.gif                fStream.Flush();
InBlock.gif                fStream.Close();
InBlock.gif                fStream 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            StreamWriter sWriter 
= new StreamWriter(m_filePath, true, System.Text.Encoding.Default);
InBlock.gif            
string txtTime = " \n" + "时间 : " + DateTime.Now.ToString();
InBlock.gif            
string txtHost = " \n" + "机器名 : " + hostName;
InBlock.gif            
string txtError = " \n" + "错误信息 : " + content;
InBlock.gif
InBlock.gif            
string textWrite = txtTime + txtHost + txtError;
InBlock.gif            sWriter.WriteLine(textWrite);
InBlock.gif            sWriter.Flush();            
InBlock.gif            sWriter.Close();
InBlock.gif            sWriter 
= null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

使用的时候呢,在需要记录的类里面新建一个全局变量,然后初始化,然后设置文件位置.之后就可以记录日志了.
例如:
None.gif class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
private Utils.LogWritter m_logger = null;
InBlock.gif  
public Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    m_logger 
= Utils.LogWritter.CreateInstance();
InBlock.gif    m_logger.SetFilePath(
@"C:\TestLog.txt");  
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
string str = "a";
          str.Substring(0, 2);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ee)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      m_logger.WriteLog(ee.ToString());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

今天在csdn上面看到有人找记录错误日志的类,可以记录机器名,时间..
正好有空,就胡乱写了一个.但是有一点还是不太清楚,日志类的记录方法到底静态的好呢?还是普通的好呢?欢迎朋友多提宝贵意见和看法.

转载于:https://www.cnblogs.com/snowlove67/archive/2005/11/10/273040.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值