Skip to content

Commit 2917dab

Browse files
committed
Add custom async file trace listener and improve disposal
Introduces TextTraceListener for asynchronous file logging, replacing TextWriterTraceListener in GeneralTracer. Ensures proper disposal of trace listeners in upgrade and strategy flows. Also removes unnecessary PublishAot properties and comments out Patch/BackUp options in client and upgrader programs for better configuration flexibility.
1 parent 8286e00 commit 2917dab

File tree

15 files changed

+113
-25
lines changed

15 files changed

+113
-25
lines changed

src/c#/GeneralUpdate.Bowl/GeneralUpdate.Bowl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="..\GeneralUpdate.Common\Shared\Object\VersionOSS.cs" Link="Common\VersionOSS.cs" />
8686
<Compile Include="..\GeneralUpdate.Common\Shared\Service\VersionService.cs" Link="Common\VersionService.cs" />
8787
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
88+
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\TextTraceListener.cs" Link="Common\TextTraceListener.cs" />
8889
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
8990
</ItemGroup>
9091

src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<LangVersion>default</LangVersion>
9-
<PublishAot>true</PublishAot>
109
</PropertyGroup>
1110

1211
<ItemGroup>

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ static async Task Main(string[] args)
5151
.SetConfig(configinfo)
5252
.Option(UpdateOption.DownloadTimeOut, 60)
5353
.Option(UpdateOption.Encoding, Encoding.UTF8)
54-
.Option(UpdateOption.Patch, false)
55-
.Option(UpdateOption.BackUp, false)
54+
//.Option(UpdateOption.Patch, false)
55+
//.Option(UpdateOption.BackUp, false)
5656
.LaunchAsync();
5757
Console.WriteLine($"主程序已启动,{DateTime.Now}!");
5858
}

src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,38 @@ await Task.Run(() =>
3535
var versionsFilePath = Path.Combine(basePath, configGlobalConfigInfo.VersionFileName);
3636
DownloadFile(configGlobalConfigInfo.Url, versionsFilePath);
3737
if (!File.Exists(versionsFilePath)) return;
38-
var versions = StorageManager.GetJson<List<VersionOSS>>(versionsFilePath, VersionOSSJsonContext.Default.ListVersionOSS);
38+
var versions = StorageManager.GetJson<List<VersionOSS>>(versionsFilePath,
39+
VersionOSSJsonContext.Default.ListVersionOSS);
3940
if (versions == null || versions.Count == 0) return;
4041
versions = versions.OrderByDescending(x => x.PubTime).ToList();
4142
var newVersion = versions.First();
4243
//Determine whether the current client version needs to be upgraded.
4344
GeneralTracer.Debug("3.Determine whether the current client version needs to be upgraded.");
44-
if (!IsUpgrade(configGlobalConfigInfo.CurrentVersion, newVersion.Version))
45+
if (!IsUpgrade(configGlobalConfigInfo.CurrentVersion, newVersion.Version))
4546
return;
46-
47+
4748
//If you confirm that an update is required, start the upgrade application.
4849
var appPath = Path.Combine(basePath, $"{upgradeAppName}");
49-
if (!File.Exists(appPath))
50+
if (!File.Exists(appPath))
5051
throw new Exception($"The application does not exist {upgradeAppName} !");
51-
52+
5253
GeneralTracer.Debug("4.Start upgrade app.");
53-
var json = JsonSerializer.Serialize(configGlobalConfigInfo, GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
54+
var json = JsonSerializer.Serialize(configGlobalConfigInfo,
55+
GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
5456
Environments.SetEnvironmentVariable("GlobalConfigInfoOSS", json);
5557
Process.Start(appPath);
5658
Process.GetCurrentProcess().Kill();
5759
}
5860
catch (Exception exception)
5961
{
60-
GeneralTracer.Error("The BaseStart method in the GeneralClientOSS class throws an exception.", exception);
62+
GeneralTracer.Error("The BaseStart method in the GeneralClientOSS class throws an exception.",
63+
exception);
6164
throw exception;
6265
}
66+
finally
67+
{
68+
GeneralTracer.Dispose();
69+
}
6370
});
6471
}
6572

src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="..\GeneralUpdate.Common\Shared\Object\VersionOSS.cs" Link="Common\VersionOSS.cs" />
7676
<Compile Include="..\GeneralUpdate.Common\Shared\Service\VersionService.cs" Link="Common\VersionService.cs" />
7777
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
78+
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\TextTraceListener.cs" Link="Common\TextTraceListener.cs" />
7879
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
7980
<Compile Include="..\GeneralUpdate.Differential\Binary\BinaryHandler.cs" Link="Common\BinaryHandler.cs" />
8081
<Compile Include="..\GeneralUpdate.Differential\Binary\BZip2Constants.cs" Link="Common\BZip2Constants.cs" />

src/c#/GeneralUpdate.ClientCore/Strategys/LinuxStrategy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public override void StartApp()
117117
}
118118
finally
119119
{
120+
GeneralTracer.Dispose();
120121
Process.GetCurrentProcess().Kill();
121122
}
122123
}

src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public override void StartApp()
112112
}
113113
finally
114114
{
115+
GeneralTracer.Dispose();
115116
Process.GetCurrentProcess().Kill();
116117
}
117118
}

src/c#/GeneralUpdate.Common/Shared/Trace/GeneralTracer.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class GeneralTracer
1010
private static readonly object _lockObj = new();
1111
private static bool _isTracingEnabled;
1212
private static string _currentLogDate;
13-
private static TextWriterTraceListener _fileListener;
13+
private static TextTraceListener _fileListener;
1414

1515
static GeneralTracer()
1616
{
@@ -51,7 +51,8 @@ private static void InitializeFileListener()
5151
Directory.CreateDirectory(logDir);
5252

5353
var logFileName = Path.Combine(logDir, $"generalupdate-trace {today}.log");
54-
_fileListener = new TextWriterTraceListener(logFileName) { Name = "FileListener" };
54+
//_fileListener = new TextWriterTraceListener(logFileName) { Name = "FileListener" };
55+
_fileListener = new TextTraceListener(logFileName);
5556

5657
Trace.Listeners.Add(_fileListener);
5758
_currentLogDate = today;
@@ -140,17 +141,24 @@ private static void WriteTraceMessage(TraceLevel level, string message)
140141

141142
public static void Dispose()
142143
{
143-
lock (_lockObj)
144+
try
144145
{
145-
if (_fileListener is not null)
146+
lock (_lockObj)
146147
{
147-
_fileListener.Flush();
148-
_fileListener.Close();
149-
_fileListener.Dispose();
150-
_fileListener = null;
148+
if (_fileListener is not null)
149+
{
150+
_fileListener.Flush();
151+
_fileListener.Close();
152+
_fileListener.Dispose();
153+
_fileListener = null;
154+
}
155+
156+
Trace.Listeners.Clear();
151157
}
152-
153-
Trace.Listeners.Clear();
158+
}
159+
catch
160+
{
161+
// ignored
154162
}
155163
}
156164
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Collections.Concurrent;
5+
using System.Threading;
6+
7+
public class TextTraceListener : TraceListener, IDisposable
8+
{
9+
private readonly string _filePath;
10+
private readonly BlockingCollection<string> _messageQueue;
11+
private readonly Thread _loggingThread;
12+
private volatile bool _isDisposed;
13+
14+
public TextTraceListener(string filePath)
15+
{
16+
_filePath = filePath;
17+
_messageQueue = new BlockingCollection<string>();
18+
_loggingThread = new Thread(ProcessQueue);
19+
_loggingThread.IsBackground = true;
20+
_loggingThread.Start();
21+
}
22+
23+
public override void Write(string? message)
24+
{
25+
QueueMessage(message);
26+
}
27+
28+
public override void WriteLine(string? message)
29+
{
30+
QueueMessage(message + Environment.NewLine);
31+
}
32+
33+
private void QueueMessage(string? message)
34+
{
35+
if (!_isDisposed && message != null)
36+
{
37+
_messageQueue.Add(message);
38+
}
39+
}
40+
41+
private void ProcessQueue()
42+
{
43+
foreach (var message in _messageQueue.GetConsumingEnumerable())
44+
{
45+
using (var fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
46+
using (var writer = new StreamWriter(fileStream))
47+
{
48+
writer.Write(message);
49+
}
50+
}
51+
}
52+
53+
public void Dispose()
54+
{
55+
if (!_isDisposed)
56+
{
57+
_isDisposed = true;
58+
_messageQueue.CompleteAdding();
59+
_loggingThread.Join();
60+
_messageQueue.Dispose();
61+
}
62+
}
63+
}

src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="..\GeneralUpdate.Common\Shared\Object\VersionOSS.cs" Link="Common\VersionOSS.cs" />
7979
<Compile Include="..\GeneralUpdate.Common\Shared\Service\VersionService.cs" Link="Common\VersionService.cs" />
8080
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
81+
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\TextTraceListener.cs" Link="Common\TextTraceListener.cs" />
8182
<Compile Include="..\GeneralUpdate.Common\Shared\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
8283
<Compile Include="..\GeneralUpdate.Differential\Binary\BinaryHandler.cs" Link="Common\BinaryHandler.cs" />
8384
<Compile Include="..\GeneralUpdate.Differential\Binary\BZip2Constants.cs" Link="Common\BZip2Constants.cs" />

0 commit comments

Comments
 (0)