Log 日志收集
class Log
{
private readonly static String DateFormat = "yyyyMMdd";
private readonly static String path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"bin\Log";
static Log()
{
try
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
WriteLog("日志路径:", path);
}
catch { }
}
public static void WriteLog(String errorLevel, String log)
{
Console.WriteLine(log+"\n输出位置:Log");
Console.WriteLine();
try
{
DateTime dateTime = DateTime.Now;
String logFile = path + @"\log" + dateTime.ToString(DateFormat) + ".txt";
FileStream fileStream;
StreamWriter streamWriter;
if (File.Exists(logFile))
{
fileStream = new FileStream(logFile, FileMode.Append, FileAccess.Write);
streamWriter = new StreamWriter(fileStream);
streamWriter.WriteLine(dateTime);//开始写入值
streamWriter.WriteLine("ErrorLevel:" + errorLevel);
streamWriter.WriteLine(log);
streamWriter.WriteLine();
streamWriter.Close();
fileStream.Close();
}
else
{
fileStream = new FileStream(logFile, FileMode.Create, FileAccess.Write);
streamWriter = new StreamWriter(fileStream);
streamWriter.WriteLine(dateTime);//开始写入值
streamWriter.WriteLine("ErrorLevel:" + errorLevel);
streamWriter.WriteLine(log);
streamWriter.WriteLine();
streamWriter.Close();
fileStream.Close();
}
}
catch { }
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="message"></param>
public static void warn(Exception exception)
{
WriteLog("warn", exception.ToString());
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="message"></param>
public static void warn(String message)
{
WriteLog("warn", message);
}
public static void error(Exception exception)
{
WriteLog("error", exception.ToString());
}
/// <summary>
/// 一般错误信息
/// </summary>
/// <param name="message"></param>
public static void error(String message)
{
WriteLog("error", message);
}
/// <summary>
/// 致命的错误信息
/// </summary>
/// <param name="exception"></param>
public static void fatal(Exception exception)
{
WriteLog("fatal", exception.ToString());
}
/// <summary>
/// 致命的错误信息
/// </summary>
/// <param name="message"></param>
public static void fatal(String message)
{
WriteLog("fatal", message);
}
}
最新文章
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
- vmstat命令学习
- Win7、Ubuntu双系统正确卸载Ubuntu系统
- 【ASP.NET 基础】Page类和回调技术
- 如何建立批处理文件(.bat或.cmd)
- EL表达式获取数据
- ccache高速编译工具
- UVa 10054 The Necklace【欧拉回路】
- Java model 对象处理
- 基于stm32f103zet6的FAT16文件系统学习1(初识FAT16)
- [Leetcode][Python]53: Maximum Subarray
- 带你一起Piu Piu Piu~
- (转)认识java中的堆和栈
- ActiveMQ的运用
- 分布式事务解决方案以及 .Net Core 下的实现(上)
- luogu2336 喵星球上的点名 (SA+二分答案+树状数组)
- Java并发程序设计(十三)锁的性能优化
- p2042 维修数列(SPLAY)
- centos 7.0 lnmp成功安装过程(很乱)
- java代码行数计算器