VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之.C#实现线程安全的简易日志记录方法

一般在实际项目的开发中,会要求涉及日志记录的问题,比较常用的有Log4Net,NLog等几个,而小项目小工具的话,则无需费此大驾。而譬如串口开发的话,需要记录串口过来的数据等等,这时候就要考虑日志记录上线程的问题。对此,为了方便后续使用,封装了下代码:

?
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
 
namespace CSharpUtilHelpV2
{
  /// <summary>
  /// 日志类型枚举
  /// </summary>
  public enum LogType
  {
    /// <summary>
    /// 一般输出
    /// </summary>
    Trace,
    /// <summary>
    /// 警告
    /// </summary>
    Warning,
    /// <summary>
    /// 错误
    /// </summary>
    Error,
    /// <summary>
    /// SQL
    /// </summary>
    SQL
  }
  /// <summary>
  /// 基于.NET 2.0日志工具类
  /// </summary>
  public class LogToolV2
  {
    private static readonly Thread LogTask;
    private static readonly ThreadSafeQueueV2<string> LogColQueue;//自定义线程安全的Queue
    private static readonly object SyncRoot;
    private static readonly string FilePath;
    private static readonly long BackFileSize_MB = 2;//超过2M就开始备份日志文件
    static LogToolV2()
    {
      SyncRoot = new object();
      FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
      LogTask = new Thread(WriteLog);
      LogColQueue = new ThreadSafeQueueV2<string>();
      LogTask.Start();
      Debug.WriteLine("Log Start......");
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="msg">日志内容</param>
    public static void Log(string msg)
    {
      string _msg = string.Format("{0} : {2}", DateTime.Now.ToString("HH:mm:ss"), msg);
      LogColQueue.Enqueue(msg);
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="msg">日志内容</param>
    /// <param name="type">日志类型</param>
    public static void Log(string msg, LogType type)
    {
      string _msg = string.Format("{0} {1}: {2}", DateTime.Now.ToString("HH:mm:ss"), type, msg);
      LogColQueue.Enqueue(_msg);
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="ex">异常</param>
    public static void Log(Exception ex)
    {
      if (ex != null)
      {
        string _newLine = Environment.NewLine;
        StringBuilder _builder = new StringBuilder();
        _builder.AppendFormat("{0}: {1}{2}", DateTime.Now.ToString("HH:mm:ss"), ex.Message, _newLine);
        _builder.AppendFormat("{0}{1}", ex.GetType(), _newLine);
        _builder.AppendFormat("{0}{1}", ex.Source, _newLine);
        _builder.AppendFormat("{0}{1}", ex.TargetSite, _newLine);
        _builder.AppendFormat("{0}{1}", ex.StackTrace, _newLine);
        LogColQueue.Enqueue(_builder.ToString());
      }
    }
    private static void WriteLog()
    {
      while (true)
      {
        if (LogColQueue.Count() > 0)
        {
          string _msg = LogColQueue.Dequeue();
          Monitor.Enter(SyncRoot);
          if (!CreateDirectory()) continue;
          string _path = string.Format("{0}{1}.log", FilePath, DateTime.Now.ToString("yyyyMMdd"));
          Monitor.Exit(SyncRoot);
          lock (SyncRoot)
          {
            if (CreateFile(_path))
              ProcessWriteLog(_path, _msg);//写入日志到文本
          }
          ProcessBackLog(_path);//日志备份
        }
      }
    }
    private static void ProcessBackLog(string path)
    {
      lock (SyncRoot)
      {
        if (FileToolV2.GetMBSize(path) > BackFileSize_MB)
        {
          FileToolV2.CopyToBak(path);
        }
      }
    }
    private static void ProcessWriteLog(string path, string msg)
    {
      try
      {
        StreamWriter _sw = File.AppendText(path);
        _sw.WriteLine(msg);
        _sw.Flush();
        _sw.Close();
      }
      catch (Exception ex)
      {
        Debug.WriteLine(string.Format("写入日志失败,原因:{0}", ex.Message));
      }
    }
    private static bool CreateFile(string path)
    {
      bool _result = true;
      try
      {
        if (!File.Exists(path))
        {
          FileStream _files = File.Create(path);
          _files.Close();
        }
      }
      catch (Exception)
      {
        _result = false;
      }
      return _result;
    }
    private static bool CreateDirectory()
    {
      bool _result = true;
      try
      {
        if (!Directory.Exists(FilePath))
        {
          Directory.CreateDirectory(FilePath);
        }
      }
      catch (Exception)
      {
        _result = false;
      }
      return _result;
    }
 
  }
}

测试代码如下:

?
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
using CSharpUtilHelpV2;
using System;
using System.Diagnostics;
using System.Threading;
 
namespace LogUtilHelpV2Test
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        Debug.WriteLine("-------------");
        Action _writeLog = delegate()
        {
          for (int i = 0; i < 10000; i++)
            LogToolV2.Log(Guid.NewGuid().ToString(), LogType.Trace);
        };
        Thread _wireteLogTask1 = new Thread(new ThreadStart(_writeLog));
        _wireteLogTask1.Start();
 
        Thread _wireteLogTask2 = new Thread(new ThreadStart(_writeLog));
        _wireteLogTask2.Start();
 
        //throw new Exception("test  aaa bb cc");
      }
      catch (Exception ex)
      {
        LogToolV2.Log(ex);
        Console.WriteLine(ex.Message.Trim());
      }
      finally
      {
        Console.WriteLine("ok");
        Console.ReadLine();
      }
    }
  }
}

代码运行效果如下所示:

感兴趣的读者可以自己测试运行一下,希望能对大家起到一点帮助!


相关教程