分享一个简单的日志记录静态类

LanceZhang.Common.Log静态类

作用:

日志记录

适用情景:

一般日志记录,以及Windows Service、Web Service等部署后难以调试的应用程序。

主要输出:

文件系统IO

方法:

1. Output(string)

用于普通日志记录,记录的信息将在程序目录下根据日期建立文本文件“log_20090408.txt”

并在该文本文件中追加要输出的普通日志信息:

 

2.OutputError(string)

用于错误日志输出,与Output方法类似,不但输出日志信息,还输出当前的文件名、行、列号以及当前执行的方法名,以便进行分析。

如:

 

     try
    {
        
int  i  =   8 ;
        
int  j  =   0 ;
        
int  c  =  i  /  j;
    }
    
catch  (Exception ex)
    {
        Log.OutputError(ex.Message);
    }

 

输出:

 

 

代码: 

 

ExpandedBlockStart.gif ContractedBlock.gif /**/ /****************************** Module Header ******************************
* Module Name:    Log.cs
* Project:    LanceZhang.Common
* Copyright (c) Lance Zhang (blodfox777@hotmail.com)

* History:
* * 3/7/2009 2:50 Lance Zhang Created
**************************************************************************
*/


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

namespace  LanceZhang.Common
ExpandedBlockStart.gifContractedBlock.gif
{
    
public static class Log
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private static object lockobj=new object();

        
private static void WriteToFile(string filename, string content)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
using (StreamWriter sw = new StreamWriter(filename, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sw.WriteLine(content);
                sw.Close();
            }

        }


        
private static string GetDateTime()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "[" + DateTime.Now.ToString("HH:mm:ss"+ "";
        }


        
public static void Output(string msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
lock (lockobj)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Log.WriteToFile(Directory.GetCurrentDirectory()
+ @"\log_" + DateTime.Now.ToString("yyyyMMdd"+ ".txt", GetDateTime() + msg);
            }

        }


        
public static void OutputError(string msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
lock (lockobj)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                StackFrame sf 
= new StackTrace(true).GetFrame(1);
                StringBuilder sb 
= new StringBuilder(GetDateTime());
                sb.Append(
"ERROR:");
                sb.AppendLine(msg);
                sb.AppendLine(
"Error from:");
                sb.AppendLine(sf.GetFileName());
                sb.Append(
"Line:");
                sb.AppendLine(sf.GetFileLineNumber().ToString());
                sb.Append(
"Column:");
                sb.AppendLine(sf.GetFileColumnNumber().ToString());
                sb.Append(
"Method:");
                sb.Append(sf.GetMethod().Name);
                Log.WriteToFile(Directory.GetCurrentDirectory() 
+ @"\log_" + DateTime.Now.ToString("yyyyMMdd"+ ".txt",  sb.ToString());
            }

        }

    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值