1
wtt
3 天以前 b371b604a464e7249634bbdb45cda85dd5bd1f3a
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WebAPI
{
    public class LogService
    {
        private static readonly object lockObj = new object();
 
        public static void WriteAsync(object obj, string filePath = "Vlog", bool isAppend = true)
        {
            Task.Run(() =>
            {
                Write(obj, filePath, isAppend);
            });
        }
 
        public static void WriteAsync<T>(object obj, string filePath = "Vlog", bool isAppend = true)
        {
            Task.Run(() =>
            {
                Write(obj, $@"{filePath}\{typeof(T).Name}", isAppend);
            }); 
        }
 
        #region 日志
        public static void Write(object obj, string filePath = "Vlog", bool isAppend = true)
        {
            try
            {
                lock (lockObj)
                {
                    filePath = $@"{filePath}\webapi{DateTime.Now.ToString("yyyyMMdd")}.txt";
 
                    filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
 
                    if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
 
                    bool fileExists = System.IO.File.Exists(filePath);
                    //不存在 则创建该文件
                    if (!fileExists)
                    {
                        System.IO.File.Create(filePath).Close();
                    }
 
                    using (StreamWriter writer = new StreamWriter(filePath, isAppend))
                    {
                        //存在的时候才写一行
                        if (fileExists && isAppend)
                        {
                            writer.WriteLine();
                        }
 
                        var content = obj is string ? obj : JsonConvert.SerializeObject(obj);
                        writer.WriteLine($"{DateTime.Now} {content}");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion
 
        #region 自定义文件名
        public static void CustomWriteLog(object obj, string FileName, string filePath = "Vlog" , bool isAppend = true)
        {
            try
            {
                lock (lockObj)
                {
                    filePath = $@"{filePath}\{FileName}.txt";
 
                    filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
 
                    if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
 
                    bool fileExists = System.IO.File.Exists(filePath);
                    //不存在 则创建该文件
                    if (!fileExists)
                    {
                        System.IO.File.Create(filePath).Close();
                    }
 
                    using (StreamWriter writer = new StreamWriter(filePath, isAppend))
                    {
                        //存在的时候才写一行
                        if (fileExists && isAppend)
                        {
                            writer.WriteLine();
                        }
 
                        var content = obj is string ? obj : JsonConvert.SerializeObject(obj);
                        writer.WriteLine($"{DateTime.Now} {content}");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion
    }
}