记录日志的工具类
2015-08-05 09:15
337 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Zhihe.SmartSoftware.Product.Common
{
public enum LogLevel
{
DEBUG,
INFO,
WARN,
ERROR
};
public class Log
{
private static object m_lockObj = new object();
private static string m_strLogFile = null;
private static FileInfo m_file = null;
private static StreamWriter m_writer = null;
public static void InitLog(string _processName)
{
string strCurrentPath = Directory.GetCurrentDirectory();
string strLogPath = strCurrentPath + "\\log\\";
if (!Directory.Exists(strLogPath))
{
Directory.CreateDirectory(strLogPath);
}
m_strLogFile = strLogPath + _processName + "_" +
DateTime.Now.ToString("yyyyMMdd") + ".log";
m_file = new FileInfo(m_strLogFile);
m_writer = m_file.AppendText();
}
public static void Error(string _msg)
{
log(LogLevel.ERROR, _msg);
}
public static void Error(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.ERROR, strMsg);
}
public static void Debug(string _msg)
{
log(LogLevel.DEBUG, _msg);
}
public static void Debug(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.DEBUG, strMsg);
}
public static void Info(string _msg)
{
log(LogLevel.INFO, _msg);
}
public static void Info(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.INFO, strMsg);
}
public static void Warning(string _msg)
{
log(LogLevel.WARN, _msg);
}
private static void log(LogLevel _level, string _msg)
{
if (_msg == null || _msg == "")
{
return;
}
StringBuilder sb = new StringBuilder();
// Time
sb.Append("[");
sb.Append(DateTime.Now.ToString());
sb.Append("] ");
// Level
sb.Append("[");
switch (_level)
{
case LogLevel.DEBUG:
sb.Append("DEBUG");
break;
case LogLevel.INFO:
sb.Append("INFO");
break;
case LogLevel.WARN:
sb.Append("WARN");
break;
default:
sb.Append("ERROR");
break;
}
sb.Append("] ");
// Content
sb.Append(_msg);
writeFile(sb.ToString());
}
private static void writeFile(string _strLine)
{
lock (m_lockObj)
{
try
{
m_writer.WriteLine(_strLine);
m_writer.Flush();
}
catch (Exception)
{ }
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Zhihe.SmartSoftware.Product.Common
{
public enum LogLevel
{
DEBUG,
INFO,
WARN,
ERROR
};
public class Log
{
private static object m_lockObj = new object();
private static string m_strLogFile = null;
private static FileInfo m_file = null;
private static StreamWriter m_writer = null;
public static void InitLog(string _processName)
{
string strCurrentPath = Directory.GetCurrentDirectory();
string strLogPath = strCurrentPath + "\\log\\";
if (!Directory.Exists(strLogPath))
{
Directory.CreateDirectory(strLogPath);
}
m_strLogFile = strLogPath + _processName + "_" +
DateTime.Now.ToString("yyyyMMdd") + ".log";
m_file = new FileInfo(m_strLogFile);
m_writer = m_file.AppendText();
}
public static void Error(string _msg)
{
log(LogLevel.ERROR, _msg);
}
public static void Error(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.ERROR, strMsg);
}
public static void Debug(string _msg)
{
log(LogLevel.DEBUG, _msg);
}
public static void Debug(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.DEBUG, strMsg);
}
public static void Info(string _msg)
{
log(LogLevel.INFO, _msg);
}
public static void Info(string _className, string _msg)
{
string strMsg = _className + ": " + _msg;
log(LogLevel.INFO, strMsg);
}
public static void Warning(string _msg)
{
log(LogLevel.WARN, _msg);
}
private static void log(LogLevel _level, string _msg)
{
if (_msg == null || _msg == "")
{
return;
}
StringBuilder sb = new StringBuilder();
// Time
sb.Append("[");
sb.Append(DateTime.Now.ToString());
sb.Append("] ");
// Level
sb.Append("[");
switch (_level)
{
case LogLevel.DEBUG:
sb.Append("DEBUG");
break;
case LogLevel.INFO:
sb.Append("INFO");
break;
case LogLevel.WARN:
sb.Append("WARN");
break;
default:
sb.Append("ERROR");
break;
}
sb.Append("] ");
// Content
sb.Append(_msg);
writeFile(sb.ToString());
}
private static void writeFile(string _strLine)
{
lock (m_lockObj)
{
try
{
m_writer.WriteLine(_strLine);
m_writer.Flush();
}
catch (Exception)
{ }
}
}
}
}
相关文章推荐
- 程序员禅的10条法则
- c++中堆、栈内存分配
- Groovy
- 在ubuntu12.04中安装wine和sourceinsight
- SDUToj1730数字三角形问题
- iOS开发_常用的正则表达式
- 当设置RESULT_CACHE_MAX_SIZE参数并且重启过database后,Query Result Cache 还是被禁用的。
- ${pageContext.request.contextPath}
- 南邮 OJ 1128 An Industrial Spy
- proguard 提示Warning: can't find referenced field/method '...' in library class ...等问题
- poj解题报告——2234
- 大数模板
- AJAX--前后台交互
- 关于 Pragma 的使用总结
- APP测试要点
- 面试题:post和get的区别
- css中table-layout:fixed 属性的用法
- 函数闭包问题的学习
- jQuery遍历Json数组
- dda的fpga实现(转载)