-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoggerContent.cs
More file actions
120 lines (118 loc) · 4.36 KB
/
LoggerContent.cs
File metadata and controls
120 lines (118 loc) · 4.36 KB
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
/* ===============================================
* 功能描述:AspNetCore.Logging.LoggerContent
* 创 建 者:WeiGe
* 创建日期:1/2/2019 11:30:07 PM
* ===============================================*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AspNetCore.FileLog
{
internal class LoggerContent : IDisposable
{
#region static write information to file
internal static readonly ConcurrentBag<LoggerContent> Contents = new ConcurrentBag<LoggerContent>();
static LoggerContent()
{
AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) =>
{
Task.WaitAll(WriteToFile(Contents.ToList()));
};
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
Logger.Error<AppDomain>($"{sender}; {e.ExceptionObject.ToJson()}");
};
System.Timers.Timer timer = new System.Timers.Timer(1000 * 0.8);
timer.Elapsed += async (sender, e) =>
{
if (!Contents.IsEmpty)
{
var list = new List<LoggerContent>();
while (!Contents.IsEmpty)
{
if (Contents.TryTake(out LoggerContent content))
{
list.Add(content);
}
}
await WriteToFile(list);
list = null;
}
};
timer.Start();
}
static async Task WriteToFile(List<LoggerContent> contents)
{
if (contents.Count > 0)
{
var list = contents.GroupBy(t => t.PathHash).Select(t => new
{
Path = t.Min(x => x.Path),
List = t.ToList()
});
foreach (var content in list)
{
await Task.Run(async () =>
{
await Task.Yield();
var file = new FileInfo(content.Path);
if (!file.Directory.Exists)
{
file.Directory.Create();
}
bool retreid = false;
RETRY:
try
{
using (var write = file.AppendText())
{
foreach (var f in content.List.OrderBy(t => t.Ticks))
{
using (f)
{
write.WriteLine(f.Message);
}
}
write.Flush();
}
}
catch
{
if (!retreid)
{
file = new FileInfo(System.IO.Path.Combine(file.DirectoryName, $"{System.IO.Path.GetFileNameWithoutExtension(file.Name)}_fail{file.Extension}"));
retreid = true;
goto RETRY;
}
}
});
}
}
}
#endregion
public LoggerContent(string path, string message)
{
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(message))
{
return;
}
this.Path = path;
this.Message = message.Trim();
this.PathHash = this.Path.GetHashCode();
Contents.Add(this);
}
public long Ticks { get; } = DateTime.Now.Ticks;
public string Path { get; private set; }
public int PathHash { get; }
public string Message { get; private set; }
public void Dispose()
{
this.Path = null;
this.Message = null;
}
}
}