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,21 +18,27 @@ using System.Text;
namespace Tesses.YouTubeDownloader
{
internal class LockObj
{
}
public partial class TYTDStorage
{
private Mutex mtx0=new Mutex();
internal static LockObj o=new LockObj();
private Logger _log=null;
public Logger GetLogger()
{
mtx0.WaitOne();
lock(o){
if(_log == null)
{
_log = new Logger(this);
}
mtx0.ReleaseMutex();
return _log;
}
}
}
public class LoggerProperties
{
public bool UseLogs {get;set;}
@ -48,7 +54,7 @@ namespace Tesses.YouTubeDownloader
}
public class Logger
{
Mutex mtx=new Mutex();
private string _filename;
private TYTDStorage _storage;
@ -95,6 +101,12 @@ namespace Tesses.YouTubeDownloader
Console.WriteLine(message);
}
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);
@ -108,18 +120,24 @@ namespace Tesses.YouTubeDownloader
msg.AppendLine($"{dat.ToShortDateString()} at {dat.ToShortTimeString()}:");
}
msg.AppendLine(message);
mtx.WaitOne();
using(var strm = await _storage.OpenOrCreateAsync(_filename))
lock(TYTDStorage.o){
try{
using(var strm = _storage.OpenOrCreateAsync(_filename).GetAwaiter().GetResult())
{
if(!strm.CanSeek) return;
strm.Seek(0,SeekOrigin.End);
using(var sw = new StreamWriter(strm))
{
await sw.WriteLineAsync(msg.ToString());
sw.WriteLine(msg.ToString());
}
} }
catch(Exception ex)
{
_=ex;
}
//mtx.ReleaseMutex();
}
mtx.ReleaseMutex();
}
public async Task WriteAsync(Exception ex)

View File

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