From 4739c9f539eee689deb8090baafaebc4b0f20c3f Mon Sep 17 00:00:00 2001 From: Wizou Date: Thu, 30 Dec 2021 23:43:00 +0100 Subject: [PATCH] Fix risk of having only WTelegram.session.tmp if killing the program in the middle of Delete/Move (that's what File.Replace was trying to avoid!!) --- EXAMPLES.md | 17 +++++++---------- src/Session.cs | 4 ++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index c203d3d..69f928e 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -364,22 +364,19 @@ client.TcpHandler = async (address, port) => By default, WTelegramClient logs are displayed on the Console screen. If you are not in a Console app or don't want the logs on screen, you can redirect them as you prefer: -* Log to VS Output debugging pane in addition to default Console screen logging: ```csharp +// • Log to VS Output debugging pane in addition to default Console screen logging: WTelegram.Helpers.Log += (lvl, str) => System.Diagnostics.Debug.WriteLine(str); -``` -* Log to file in replacement of default Console screen logging: -```csharp + +// • Log to file in replacement of default Console screen logging: WTelegram.Helpers.Log = (lvl, str) => File.AppendAllText("WTelegram.log", str + Environment.NewLine); -``` -* More efficient example with a static variable and detailed logging to file: -```csharp + +// • More efficient example with a static variable and detailed logging to file: static StreamWriter WTelegramLogs = new StreamWriter("WTelegram.log", true, Encoding.UTF8) { AutoFlush = true }; ... WTelegram.Helpers.Log = (lvl, str) => WTelegramLogs.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}"); -``` -* In an ASP.NET service, you will typically send logs to a `ILogger`: -```csharp + +// • In an ASP.NET service, you will typically send logs to a `ILogger`: WTelegram.Helpers.Log = (lvl, str) => _logger.Log((LogLevel)lvl, str); ``` diff --git a/src/Session.cs b/src/Session.cs index 1a675cb..67db7ab 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -91,8 +91,8 @@ namespace WTelegram lock (this) { File.WriteAllBytes(tempPathname, output); - File.Delete(_pathname); - File.Move(tempPathname, _pathname); + File.Copy(tempPathname, _pathname, true); + File.Delete(tempPathname); } } }