forked from TechieGuy12/PlexServerAutoUpdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cs
131 lines (115 loc) · 3.64 KB
/
Log.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using static System.Environment;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TE
{
/// <summary>
/// Contains the properties and methods to write a log file.
/// </summary>
public static class Log
{
/// <summary>
/// The name of the updater log file.
/// </summary>
private const string LogFileName = "plex-updater.txt";
private static string _defaultFolder;
/// <summary>
/// Gets the the full path to the log file.
/// </summary>
public static string FilePath { get; private set; }
/// <summary>
/// Gets the folder to the log file.
/// </summary>
public static string Folder { get; private set; }
/// <summary>
/// Initializes an instance of the <see cref="Log"/> class.
/// </summary>
static Log()
{
_defaultFolder = Path.GetTempPath();
Folder = _defaultFolder;
// Set the full path to the log file
FilePath = Path.Combine(Folder, LogFileName);
}
/// <summary>
/// Gets the formatted timestamp value.
/// </summary>
/// <returns>
/// A string representation of the timestamp.
/// </returns>
private static string GetTimeStamp()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
}
/// <summary>
/// Deletes the log file.
/// </summary>
public static void Delete()
{
File.Delete(FilePath);
}
/// <summary>
/// Sets the full path to the log file.
/// </summary>
/// <param name="path">
/// The full path to the log file.
/// </param>
public static void SetFolder(string path)
{
try
{
// Call this to validate the path
Path.GetFullPath(path);
Folder = Path.GetDirectoryName(path);
if (!Directory.Exists(Folder))
{
Directory.CreateDirectory(Folder);
}
FilePath = Path.Combine(Folder, LogFileName);
}
catch
{
Folder = _defaultFolder;
FilePath = Path.Combine(Folder, LogFileName);
}
}
/// <summary>
/// Writes a string value to the log file.
/// </summary>
public static void Write(string text, bool appendDate = true)
{
string timeStamp = string.Empty;
if (appendDate)
{
timeStamp = GetTimeStamp();
}
File.AppendAllText(FilePath, $"{timeStamp}{text}{NewLine}");
}
/// <summary>
/// Writes information about an exception to the log file.
/// </summary>
/// <param name="ex">
/// The <see cref="Exception"/> object that contains information to write to
/// the log file.
/// </param>
public static void Write(Exception ex, bool appendDate = true)
{
if (ex == null)
{
return;
}
string timeStamp = string.Empty;
if (appendDate)
{
timeStamp = GetTimeStamp();
}
File.AppendAllText(
FilePath,
$"{timeStamp}Message:{NewLine}{ex.Message}{NewLine}{NewLine}Inner Exception:{NewLine}{ex.InnerException}{NewLine}{NewLine}Stack Trace:{NewLine}{ex.StackTrace}{NewLine}");
}
}
}