Skip to content

Commit 4dbd590

Browse files
committed
写一行日志之后不关闭日志文件,直到第二天有新的日志文件时才关闭
1 parent 3b8cc9f commit 4dbd590

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

bin/svc.exe

1 KB
Binary file not shown.

src/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class Program
66
{
7-
private const string VERSION = "Easy Service: v1.0.8";
7+
private const string VERSION = "Easy Service: v1.0.9";
88

99
private const string USAGE =
1010
"Usage:\r\n" +

src/Worker.cs

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,20 @@ public void Stop()
145145

146146
if (RedirectMode && Conf.WaitSecondsForWorkerToExit > 0 && NotifyToExit(proc))
147147
{
148+
lock (OutLock)
149+
{
150+
CloseLogWriter();
151+
}
148152
return;
149153
}
150154

151155
proc.KillTree(Info);
152156
}
157+
158+
lock (OutLock)
159+
{
160+
CloseLogWriter();
161+
}
153162
}
154163

155164
private bool NotifyToExit(Process proc)
@@ -247,24 +256,84 @@ private void WriteOutput(string data)
247256
return;
248257
}
249258

250-
var outFile = Path.Combine(Conf.OutFileDir, $"{DateTime.Now:yyyy-MM-dd}.log");
259+
WriteLineToLogFile(data);
260+
251261
try
252262
{
253-
Libs.WriteLineToFile(outFile, data, true);
263+
Libs.WriteLineToFile(Conf.LastLineFile, data, false);
264+
}
265+
catch (Exception e)
266+
{
267+
Error($"Failed to write Worker's output to `{Conf.LastLineFile}`: {e.Message}");
268+
}
269+
}
270+
271+
private string logFile = null;
272+
273+
private StreamWriter logWriter = null;
274+
275+
private void GetStreamWriter()
276+
{
277+
if (logFile == null)
278+
{
279+
logFile = $"{DateTime.Now:yyyy-MM-dd}.log";
280+
logWriter = new StreamWriter(Path.Combine(Conf.OutFileDir, logFile), true);
281+
return;
282+
}
283+
284+
var curLogFile = $"{DateTime.Now:yyyy-MM-dd}.log";
285+
if (curLogFile != logFile)
286+
{
287+
logWriter.Close();
288+
logFile = curLogFile;
289+
logWriter = new StreamWriter(Path.Combine(Conf.OutFileDir, logFile), true);
290+
}
291+
}
292+
293+
private void WriteLineToLogFile(string line)
294+
{
295+
try
296+
{
297+
GetStreamWriter();
254298
}
255299
catch (Exception ex)
256300
{
257-
Error($"Failed to write Worker's output to `{outFile}`: {ex.Message}");
301+
Error($"Failed to get stream writer for logging: {ex.Message}");
302+
logFile = null;
303+
logWriter = null;
304+
return;
258305
}
259306

260307
try
261308
{
262-
Libs.WriteLineToFile(Conf.LastLineFile, data, false);
309+
logWriter.WriteLine(line);
310+
logWriter.Flush();
263311
}
264-
catch (Exception e)
312+
catch (Exception ex)
265313
{
266-
Error($"Failed to write Worker's output to `{Conf.LastLineFile}`: {e.Message}");
314+
Error($"Failed to write line to log file: {ex.Message}");
315+
CloseLogWriter();
316+
}
317+
}
318+
319+
private void CloseLogWriter()
320+
{
321+
if (logWriter == null)
322+
{
323+
return;
324+
}
325+
326+
try
327+
{
328+
logWriter.Close();
329+
}
330+
catch (Exception ex)
331+
{
332+
Error($"Failed to close stream writer of logging file: {ex.Message}");
267333
}
334+
335+
logFile = null;
336+
logWriter = null;
268337
}
269338

270339
private void DeleteLoop()

0 commit comments

Comments
 (0)