-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLog.cs
More file actions
83 lines (72 loc) · 2.26 KB
/
Log.cs
File metadata and controls
83 lines (72 loc) · 2.26 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
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace CorionisServiceManager.NET
{
/// <summary>
/// Log class.
/// Handles logging to the Log tab and optional log file
/// </summary>
public class Log
{
private Config cfg;
public String logBuffer { get; set; } = "";
private TextBox parent;
public Log(Config theCfg, TextBox theParent)
{
cfg = theCfg;
parent = theParent;
}
public void Clear()
{
logBuffer = "";
parent.Text = logBuffer;
}
public string GetLogFilename()
{
string file = Assembly.GetEntryAssembly().GetName().Name;
string path = AppDomain.CurrentDomain.BaseDirectory;
// path = Path.Combine(path, file); // add directory
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = Path.Combine(path, file); // add filename
path = path + ".log";
return path;
}
public void Save()
{
// don't double-log entries
if (!cfg.LogToFile)
{
File.AppendAllText(GetLogFilename(), logBuffer);
}
else
{
// If the buffer was just cleared assume they want to truncate the log file
if (logBuffer.Length == 0)
{
File.WriteAllText(GetLogFilename(), logBuffer);
}
}
}
public void Write(String line)
{
// keep the logBuffer from overflowing
if (logBuffer.Length > 2145000000)
{
String replacement = logBuffer.Substring(1072500);
logBuffer = String.Copy(replacement);
}
var entry = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + line + "\r\n";
logBuffer += entry;
parent.Text = logBuffer;
if (cfg.LogToFile == true)
{
File.AppendAllText(GetLogFilename(), entry);
}
}
}
}