-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.Console.cs
More file actions
88 lines (70 loc) · 1.93 KB
/
Copy pathApp.Console.cs
File metadata and controls
88 lines (70 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
namespace MarvinsAIRA
{
public partial class App : Application
{
private ReaderWriterLock _console_readerWriterLock = new();
private FileStream? _console_fileStream = null;
public void InitializeConsole()
{
WriteLine( "InitializeConsole called.", true );
var filePath = Path.Combine( DocumentsFolder, "Console.log" );
if ( File.Exists( filePath ) )
{
var lastWriteTime = File.GetLastWriteTime( filePath );
if ( lastWriteTime.CompareTo( DateTime.Now.AddMinutes( -15 ) ) < 0 )
{
File.Delete( filePath );
}
}
_console_fileStream = new FileStream( filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite );
}
public void StopConsole()
{
WriteLine( "StopConsole called.", true );
_console_fileStream?.Close();
_console_fileStream?.Dispose();
_console_fileStream = null;
}
public void WriteLine( string message, bool addBlankLine = false )
{
var blankLine = addBlankLine ? "\r\n" : string.Empty;
var messageWithTime = $"{blankLine}{DateTime.Now} {message}";
Debug.WriteLine( message );
if ( _console_fileStream != null )
{
try
{
_console_readerWriterLock.AcquireWriterLock( 250 );
try
{
var bytes = new UTF8Encoding( true ).GetBytes( $"{messageWithTime}\r\n" );
_console_fileStream.Write( bytes, 0, bytes.Length );
_console_fileStream.Flush();
}
finally
{
_console_readerWriterLock.ReleaseWriterLock();
}
}
catch ( ApplicationException )
{
}
}
Dispatcher.BeginInvoke( () =>
{
var mainWindow = MarvinsAIRA.MainWindow.Instance;
if ( mainWindow != null )
{
mainWindow.Console_TextBox.Text += $"{messageWithTime}\r\n";
mainWindow.Console_TextBox.CaretIndex = mainWindow.Console_TextBox.Text.Length;
mainWindow.Console_TextBox.ScrollToEnd();
}
} );
}
}
}