zoukankan      html  css  js  c++  java
  • .net 本地日志的添加

     1 /// <summary>  
     2 /// 写入日志到文本文件  
     3 /// </summary>  
     4 /// <param name="userName">用户名称</param>  
     5 /// <param name="action">动作</param>  
     6 /// <param name="content">日志内容</param>  
     7 /// <param name="result">操作结果或报错信息</param>  
     8 /// <param name="msg">报错信息</param>  
     9 public static void WriteTextLog(string userName, string action, string content, string result, string msg)
    10 {
    11     try
    12     {
    13         DateTime time = DateTime.Now;
    14         string path = AppDomain.CurrentDomain.BaseDirectory + "/Log/" + time.ToString("yyyy-MM") + "/";
    15         if (!Directory.Exists(path))
    16             Directory.CreateDirectory(path);
    17 
    18         string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".txt";
    19         StringBuilder str = new StringBuilder();
    20         str.Append("用户:  " + userName + "
    ");
    21         str.Append("时间:  " + time.ToString() + "
    ");
    22         str.Append("类型:  " + action + "
    ");
    23         str.Append("内容:  " + content + "
    ");
    24         str.Append("结果:  " + result + "
    ");
    25         str.Append("备注:  " + msg + "
    ");
    26         str.Append("-----------------------------------------------------------
    
    ");
    27         StreamWriter sw;
    28         if (!File.Exists(fileFullPath))
    29         {
    30             sw = File.CreateText(fileFullPath);
    31         }
    32         else
    33         {
    34             sw = File.AppendText(fileFullPath);
    35         }
    36         sw.WriteLine(str.ToString());
    37         sw.Close();
    38     }
    39     catch (Exception)
    40     {
    41                 
    42     }
    43}
  • 相关阅读:
    python列表--查找集合中重复元素的个数
    python3-打印一个进度条
    python3-sys模块
    python3-字符串操作
    python3-深浅复制
    python3-os模块
    接口和抽象类有什么区别
    集合知识
    面向对象的特征有哪些方面
    javadoc时候乱码-编码 GBK 的不可映射字符
  • 原文地址:https://www.cnblogs.com/xujunbao/p/8340388.html
Copyright © 2011-2022 走看看