-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingConfig.cs
66 lines (50 loc) · 1.91 KB
/
LoggingConfig.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
using Serilog;
using Serilog.Sinks.File;
using Serilog.Sinks.SystemConsole;
using System;
using System.IO;
using System.Text;
namespace PC2MQTT
{
public static class LoggingConfig
{
#region Public Fields
public static string? logFileFullPath;
#endregion Public Fields
#region Public Methods
public static void Configure()
{
var filePathHook = new CaptureFilePathHook();
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string folderPath = Path.Combine(appDataFolder, "PC2MQTT");
var logFilePath = Path.Combine(folderPath, "PC2MQTT_Log.log");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(logFilePath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 10_000_000, // 10 MB file size limit
retainedFileCountLimit: 1, // Retain only the last file
hooks: filePathHook,
rollOnFileSizeLimit: true) // Roll over on file size limit
.CreateLogger();
Log.Information("Logger Created");
logFileFullPath = filePathHook.Path;
}
#endregion Public Methods
}
internal class CaptureFilePathHook : FileLifecycleHooks
{
#region Public Properties
public string? Path { get; private set; }
#endregion Public Properties
#region Public Methods
public override Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding)
{
Path = path;
return base.OnFileOpened(path, underlyingStream, encoding);
}
#endregion Public Methods
}
}