hopefully fix logging

This commit is contained in:
Mike Nolan 2022-05-04 08:23:06 -05:00
parent 3d961cfe03
commit a87aad366b
2 changed files with 35 additions and 17 deletions

View File

@ -18,19 +18,25 @@ using System.Text;
namespace Tesses.YouTubeDownloader namespace Tesses.YouTubeDownloader
{ {
internal class LockObj
{
}
public partial class TYTDStorage public partial class TYTDStorage
{ {
private Mutex mtx0=new Mutex(); internal static LockObj o=new LockObj();
private Logger _log=null; private Logger _log=null;
public Logger GetLogger() public Logger GetLogger()
{ {
mtx0.WaitOne(); lock(o){
if(_log == null) if(_log == null)
{ {
_log = new Logger(this); _log = new Logger(this);
}
return _log;
} }
mtx0.ReleaseMutex();
return _log;
} }
} }
public class LoggerProperties public class LoggerProperties
@ -48,7 +54,7 @@ namespace Tesses.YouTubeDownloader
} }
public class Logger public class Logger
{ {
Mutex mtx=new Mutex();
private string _filename; private string _filename;
private TYTDStorage _storage; private TYTDStorage _storage;
@ -94,7 +100,13 @@ namespace Tesses.YouTubeDownloader
else else
Console.WriteLine(message); Console.WriteLine(message);
} }
public async Task WriteAsync(string message,bool writeToConsole=false, bool isError=false,bool log=true) public async Task WriteAsync(string message,bool writeToConsole=false,bool isError=false,bool log=true)
{
await Task.Run(()=>{
Write(message,writeToConsole,isError,log);
});
}
public void Write(string message,bool writeToConsole=false, bool isError=false,bool log=true)
{ {
if(writeToConsole) WriteStd(message,isError); if(writeToConsole) WriteStd(message,isError);
@ -108,18 +120,24 @@ namespace Tesses.YouTubeDownloader
msg.AppendLine($"{dat.ToShortDateString()} at {dat.ToShortTimeString()}:"); msg.AppendLine($"{dat.ToShortDateString()} at {dat.ToShortTimeString()}:");
} }
msg.AppendLine(message); msg.AppendLine(message);
mtx.WaitOne(); lock(TYTDStorage.o){
using(var strm = await _storage.OpenOrCreateAsync(_filename)) try{
using(var strm = _storage.OpenOrCreateAsync(_filename).GetAwaiter().GetResult())
{ {
if(!strm.CanSeek) return; if(!strm.CanSeek) return;
strm.Seek(0,SeekOrigin.End); strm.Seek(0,SeekOrigin.End);
using(var sw = new StreamWriter(strm)) using(var sw = new StreamWriter(strm))
{ {
await sw.WriteLineAsync(msg.ToString()); sw.WriteLine(msg.ToString());
} }
} } }
mtx.ReleaseMutex(); catch(Exception ex)
{
_=ex;
}
//mtx.ReleaseMutex();
}
} }
public async Task WriteAsync(Exception ex) public async Task WriteAsync(Exception ex)

View File

@ -7,9 +7,9 @@
<PackageId>Tesses.YouTubeDownloader</PackageId> <PackageId>Tesses.YouTubeDownloader</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.2.3</Version> <Version>1.0.2.4</Version>
<AssemblyVersion>1.0.2.3</AssemblyVersion> <AssemblyVersion>1.0.2.4</AssemblyVersion>
<FileVersion>1.0.2.3</FileVersion> <FileVersion>1.0.2.4</FileVersion>
<Description>A YouTube Downloader</Description> <Description>A YouTube Downloader</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>