分享一个简单的日志记录静态类
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);
}
{
int i = 8;
int j = 0;
int c = i / j;
}
catch (Exception ex)
{
Log.OutputError(ex.Message);
}
输出:
代码:
/****************************** 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
{
public static class Log
{
private static object lockobj=new object();
private static void WriteToFile(string filename, string content)
{
using (StreamWriter sw = new StreamWriter(filename, true))
{
sw.WriteLine(content);
sw.Close();
}
}
private static string GetDateTime()
{
return "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
}
public static void Output(string msg)
{
lock (lockobj)
{
Log.WriteToFile(Directory.GetCurrentDirectory()+ @"\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", GetDateTime() + msg);
}
}
public static void OutputError(string msg)
{
lock (lockobj)
{
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());
}
}
}
}
* 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
{
public static class Log
{
private static object lockobj=new object();
private static void WriteToFile(string filename, string content)
{
using (StreamWriter sw = new StreamWriter(filename, true))
{
sw.WriteLine(content);
sw.Close();
}
}
private static string GetDateTime()
{
return "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
}
public static void Output(string msg)
{
lock (lockobj)
{
Log.WriteToFile(Directory.GetCurrentDirectory()+ @"\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", GetDateTime() + msg);
}
}
public static void OutputError(string msg)
{
lock (lockobj)
{
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());
}
}
}
}