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);
}
}

最新文章

  1. Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
  2. vmstat命令学习
  3. Win7、Ubuntu双系统正确卸载Ubuntu系统
  4. 【ASP.NET 基础】Page类和回调技术
  5. 如何建立批处理文件(.bat或.cmd)
  6. EL表达式获取数据
  7. ccache高速编译工具
  8. UVa 10054 The Necklace【欧拉回路】
  9. Java model 对象处理
  10. 基于stm32f103zet6的FAT16文件系统学习1(初识FAT16)
  11. [Leetcode][Python]53: Maximum Subarray
  12. 带你一起Piu Piu Piu~
  13. (转)认识java中的堆和栈
  14. ActiveMQ的运用
  15. 分布式事务解决方案以及 .Net Core 下的实现(上)
  16. luogu2336 喵星球上的点名 (SA+二分答案+树状数组)
  17. Java并发程序设计(十三)锁的性能优化
  18. p2042 维修数列(SPLAY)
  19. centos 7.0 lnmp成功安装过程(很乱)
  20. java代码行数计算器

热门文章

  1. K8S从入门到放弃系列-(3)部署etcd集群
  2. SQL数据库基础语法
  3. python第一个浏览器的自动执行程序
  4. 轻松搭建CAS 5.x系列(6)-在CAS Server上增加OAuth2.0协议
  5. (十六)客户端验证与struts2中的服务器端验证
  6. (七)springmvc之ModelAttribute注解
  7. 安装Docker step by step
  8. C#基础--go to
  9. Ubuntu 14.04 64位机上不带CUDA支持的Caffe
  10. css的导入与基础选择器