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 }
  • 相关阅读:
    openlayers + webpack
    openlayers Map 和 es6的容器Map重名
    git 代理
    剑魂史诗套配装
    剑魂卢克攻略
    DNF斩铁剑魂每日1-5及打团须知
    APP自识别安卓苹果
    各浏览器老板键
    Apache+mod_encoding解决URL中文编码问题
    linux命令之crontab定时执行任务
  • 原文地址:https://www.cnblogs.com/yy15611/p/13370586.html
Copyright © 2011-2022 走看看