zoukankan      html  css  js  c++  java
  • 写Log日志的方法 减少插件引用

     1 public class Log
     2 {
     3 /// <summary>
     4 /// 记录状态
     5 /// </summary>
     6 /// <param name="str">信息内容</param>
     7 public static void WriteLog(string Message)
     8 {
     9 try
    10 {
    11 string path = System.Windows.Forms.Application.StartupPath;
    12 DateTime dt = DateTime.Now;
    13 string str = Message;
    14 if (!Directory.Exists(path + @"ErrLog"))
    15 Directory.CreateDirectory(path + @"ErrLog");
    16 using (var sw = new StreamWriter(path + @"ErrLog" + (dt.Year + "-" + dt.Month + "-" + dt.Day).ToString() + "_InfoLog.log", true))
    17 {
    18 sw.WriteLine(dt.ToString() + "---------------------------------------------------------");
    19 sw.WriteLine(str);
    20 sw.WriteLine("---------------------------------------------------------");
    21 sw.Close();
    22 }
    23 }
    24 catch (Exception)
    25 {
    26 
    27 }
    28 }
    29 
    30 /// <summary>
    31 /// 将异常打印到LOG文件
    32 /// </summary>
    33 /// <param name="ex">异常</param>
    34 /// <param name="LogAddress">日志文件地址</param>
    35 public static void WriteLog(Exception ex)
    36 {
    37 try
    38 {
    39 string path = System.Windows.Forms.Application.StartupPath;
    40 //在Debug目录下新建 YYYY-mm-dd_Log.log文件
    41 if (!Directory.Exists(path + @"ErrLog"))
    42 Directory.CreateDirectory(path + @"ErrLog");
    43 DateTime dt = DateTime.Now;
    44 //把异常信息输出到文件
    45 StreamWriter sw = new StreamWriter(
    46 path + @"ErrLog" +
    47 (dt.Year + "-" + dt.Month + "-" + dt.Day).ToString() +
    48 "_ErrorLog.log", true);
    49 sw.WriteLine("当前时间:" + dt.ToString());
    50 sw.WriteLine("异常信息:" + ex.Message);
    51 sw.WriteLine("异常对象:" + ex.Source);
    52 sw.WriteLine("调用堆栈:
    " + ex.StackTrace.Trim());
    53 sw.WriteLine("触发方法:" + ex.TargetSite);
    54 sw.WriteLine("---------------------------------------------------------");
    55 sw.WriteLine();
    56 sw.Close();
    57 }
    58 catch (Exception)
    59 {
    60 
    61 }
    62 }
    63 }
  • 相关阅读:
    POJ 1724 ROADS【最短路/搜索/DP】
    UVA 12716 GCD XOR【异或】
    UVA 10375 Choose and divide【唯一分解定理】
    UVA 12169 Disgruntled Judge【扩展欧几里德】
    UVA 11582 Colossal Fibonacci Numbers!【数学】
    011.progit笔记---git变基rebase
    010.progit笔记---git多个远程分支
    009.progit笔记---git单个远程分支
    008.progit笔记---git分支
    007.progit笔记---git别名
  • 原文地址:https://www.cnblogs.com/yy15611/p/13370586.html
Copyright © 2011-2022 走看看