• 写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 }
  • 相关阅读:
    ASP.NET MVC 4高级编程(第4版)
    Unity3d报告奇怪的错误CompareBaseObjectsInternal can only be called from the main thread.
    HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)
    0,22, 47, 120,(? ),290
    它们的定义Activity跳转动画
    ssh探头安全
    Netty:一种非易失堵塞client/server相框
    Swift类和结构
    php+mysql+nginx于linux部署对环境
    javascript 次序li
  • 原文地址:https://www.cnblogs.com/yy15611/p/13370586.html
Copyright © 2020-2023  润新知