1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 using System.IO;
8
9 namespace User_Log
10 {
11 //形如:"C://ABC//AppName_2222_02_22.log"
12 public class UserLogger
13 {
14 private string filePath = string.Empty; //形如:"C://ABC"
15 private string appName = string.Empty; //形如:"AppName"
16 private string startTag = "******************************";
17 public string FilePath
18 {
19 set { this.filePath = value; }
20 }
21
22 public string AppName
23 {
24 set => appName = value;
25 }
26
27 public void WriteLog(string msg)
28 {
29 try
30 {
31 string dt = DateTime.Now.ToString("yyyy_MM_dd");
32 string logFileName = string.Format("{0}//{1}_{2}.log", filePath, appName, dt);
33 if (Directory.Exists(filePath) == false)
34 {
35 Directory.CreateDirectory(filePath);
36 }
37 if (File.Exists(logFileName) == false)
38 {
39 File.Create(logFileName);
40 }
41
42 using (StreamWriter sw = new StreamWriter(logFileName, true, Encoding.UTF8))
43 {
44 sw.WriteLine(startTag);
45 sw.WriteLine(DateTime.Now.ToString("[HH:mm:ss]"));
46 sw.WriteLine(msg);
47 sw.Write(Environment.NewLine);
48 sw.Flush();
49 sw.Close();
50 }
51 }
52 catch { }
53 }
54 }
55 }