-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLogger.cs
More file actions
64 lines (58 loc) · 1.98 KB
/
Logger.cs
File metadata and controls
64 lines (58 loc) · 1.98 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
using System;
using System.Diagnostics;
using System.IO;
namespace DocumentTranslationService.Core
{
public class Logger
{
private const string AppName = "Document Translation";
private const string logFileName = "doctrLog.txt";
private readonly StreamWriter streamWriter;
private readonly string filename;
internal Logger()
{
filename = Path.GetTempFileName();
streamWriter = File.CreateText(filename);
streamWriter.AutoFlush = true;
}
~Logger()
{
Close();
}
internal void WriteLine(string line)
{
Debug.WriteLine(line);
streamWriter.WriteLine($"{DateTime.Now}: {line}");
}
internal void WriteLine()
{
streamWriter.WriteLine();
}
internal void Close()
{
string finalfilename = GetFinalFilename();
if (File.Exists(filename))
try
{
streamWriter.Close();
File.Copy(filename, finalfilename, true);
File.Delete(filename);
}
catch { };
}
private static string GetFinalFilename()
{
string finalfilename;
if (OperatingSystem.IsWindows())
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName);
finalfilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + logFileName;
}
else
{
finalfilename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + AppName + "_" + Path.DirectorySeparatorChar + logFileName;
}
return finalfilename;
}
}
}