forked from ScottLilly/SOSCSRPG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingService.cs
More file actions
46 lines (39 loc) · 1.5 KB
/
LoggingService.cs
File metadata and controls
46 lines (39 loc) · 1.5 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
using System;
using System.IO;
namespace SOSCSRPG.Core
{
public static class LoggingService
{
private const string LOG_FILE_DIRECTORY = "Logs";
static LoggingService()
{
string logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LOG_FILE_DIRECTORY);
if(!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
}
public static void Log(Exception exception, bool isInnerException = false)
{
using(StreamWriter sw = new StreamWriter(LogFileName(), true))
{
sw.WriteLine(isInnerException ? "INNER EXCEPTION" : $"EXCEPTION: {DateTime.Now}");
sw.WriteLine(new string(isInnerException ? '-' : '=', 40));
sw.WriteLine($"{exception.Message}");
sw.WriteLine($"{exception.StackTrace}");
sw.WriteLine(); // Blank line, to make the log file easier to read
}
if(exception.InnerException != null)
{
Log(exception.InnerException, true);
}
}
private static string LogFileName()
{
// This will create a separate log file for each day.
// Not that we're hoping to have many days of errors.
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LOG_FILE_DIRECTORY,
$"SOSCSRPG_{DateTime.Now:yyyyMMdd}.log");
}
}
}