Blame view

OS.Spin/OS.Spin.Common/LogisTrac.cs 3.04 KB
8ca6e89d   Tuo Wenbo   20211021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  using System;
  using System.IO;
  using System.Xml;
  
  namespace OS.Spin.Common
  {
      /// <summary>
      /// 日志记录类(记录到文本文件中)
      /// </summary>
      public static class LogisTrac
      {
          private static readonly string LOG_DIR = "Log";
          private static readonly string LOG_FILE = string.Format("{0}\\log{1}.txt", LOG_DIR, System.DateTime.Now.ToString("yyyy-MM-dd"));
          private const string LOG4NET_CONFIG = "log4net_config.xml";
          private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(typeof(LogisTrac));
  
  
          static LogisTrac()
          {
              try
              {
                  ConfigureLoad();
              }
              catch { }
          }
  
          /// <summary>
          /// 返回ILog接口
          /// </summary>
          private static log4net.ILog Log
          {
              get
              {
                  return m_log;
              }
          }
  
          /// <summary>
          /// 输出日志
          /// </summary>
          /// <param name="sInfo"></param>
          public static void WriteLog(string sInfo)
          {
              m_log.Error(sInfo);
          }
  
          public static void WriteInfoLog(string sInfo)
          {
              m_log.Info(sInfo);
          }
  
          /// <summary>
          /// 记录debug信息
          /// </summary>
          /// <param name="e"></param>
          public static void WriteLog(Exception e)
          {
              WriteLog("--------------------------------------[本次异常开始]--------------------------------------");
              WriteLog("Message : " + e.Message);
              WriteLog("Source : " + e.Source);
              WriteLog("StackTrace : " + e.StackTrace);
              WriteLog("TargetSite : " + e.TargetSite);
              WriteLog("--------------------------------------[本次异常结束]--------------------------------------\r\n");
          }
  
          /// <summary>
          /// 配置log4net环境
          /// </summary>
          private static void ConfigureLoad()
          {
              XmlDocument doc = new XmlDocument();
              //使用当前dll路径
              string sPath = FilesOperate.GetAssemblyPath();
  
              if (!sPath.EndsWith("\\"))
              {
                  sPath += "\\";
              }
  
              //查看Log文件夹是否存在,如果不存在,则创建
              string sLogDir = sPath + LOG_DIR;
              if (!Directory.Exists(sLogDir))
              {
                  Directory.CreateDirectory(sLogDir);
              }
              string sLogFile = sPath + LOG_FILE;
              sPath += LOG4NET_CONFIG;
              doc.Load(@sPath);
              XmlElement myElement = doc.DocumentElement;
  
              //修改log.txt的路径
              XmlNode pRollingFileAppenderNode = myElement.SelectSingleNode("descendant::appender[@name='RollingFileAppender']/file");
              // Create an attribute collection from the element.
              XmlAttributeCollection attrColl = pRollingFileAppenderNode.Attributes;
              attrColl[1].Value = sLogFile;
  
              log4net.Config.XmlConfigurator.Configure(myElement);
          }
  
      }
  }