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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
 
namespace iWareWebservice.AppUtil
{
    public class LogHelper
    {
        private readonly string _logPath = ConfigurationManager.AppSettings["LogDir"].ToString();
        public static object Lock = new object();
 
 
 
        public static LogHelper Instance = null;
 
        ///<summary>
        ///获取单例的方法
        ///</summary>
        ///<returns>单例实体</returns>
        public static LogHelper GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new LogHelper();
                    }
                }
            }
            return Instance;
        }
 
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="folder">日志目录</param>
        /// <param name="methodName">方法名</param>
        /// <param name="format">内容格式</param>
        /// <param name="args">内容参数</param>
        public void WriteLog(string methodName, string format, params object[] args)
        {
            try
            {
                if (!Directory.Exists(_logPath))
                {
                    Directory.CreateDirectory(_logPath);
                }
                string message = string.Format("【{0}】【{1}】{2}\r\n", DateTime.Now.ToString("HH:mm:ss"), methodName,string.Format(format, args));
                File.AppendAllText(Path.Combine(_logPath, DateTime.Now.ToString("yyyyMMdd") + ".txt"), message);
            }
            catch (Exception) { }
        }
    }
}