-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoggerService.cs
More file actions
131 lines (98 loc) · 4.49 KB
/
LoggerService.cs
File metadata and controls
131 lines (98 loc) · 4.49 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
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 Cycode.VisualStudio.Extension.Shared.DTO;
using Microsoft.VisualStudio.Shell.Interop;
namespace Cycode.VisualStudio.Extension.Shared.Services;
public interface ILoggerService {
void Initialize();
void Info(string message, params object[] args);
void Info(Exception exception, string message, params object[] args);
void Info(string message, Exception exception, params object[] args);
void Warn(string message, params object[] args);
void Warn(Exception exception, string message, params object[] args);
void Warn(string message, Exception exception, params object[] args);
void Error(string message, params object[] args);
void Error(Exception exception, string message, params object[] args);
void Error(string message, Exception exception, params object[] args);
void Debug(string message, params object[] args);
void Debug(Exception exception, string message, params object[] args);
void Debug(string message, Exception exception, params object[] args);
}
public class LoggerService : ILoggerService {
private const string _paneTitle = "Cycode";
private static readonly Guid _paneGuid = new("AF772BC0-5CBF-4A44-AE52-7F428C5659A1");
private IVsOutputWindowPane _pane;
public void Initialize() {
InitializePane();
}
public void Debug(string message, params object[] args) {
Log(LogLevel.Debug, null, message, args);
}
public void Debug(Exception exception, string message, params object[] args) {
Log(LogLevel.Debug, exception, message, args);
}
public void Debug(string message, Exception exception, params object[] args) {
Log(LogLevel.Debug, exception, message, args);
}
public void Error(string message, params object[] args) {
Log(LogLevel.Error, null, message, args);
}
public void Error(Exception exception, string message, params object[] args) {
Log(LogLevel.Error, exception, message, args);
}
public void Error(string message, Exception exception, params object[] args) {
Log(LogLevel.Error, exception, message, args);
}
public void Warn(string message, params object[] args) {
Log(LogLevel.Warning, null, message, args);
}
public void Warn(Exception exception, string message, params object[] args) {
Log(LogLevel.Warning, exception, message, args);
}
public void Warn(string message, Exception exception, params object[] args) {
Log(LogLevel.Warning, exception, message, args);
}
public void Info(string message, params object[] args) {
Log(LogLevel.Info, null, message, args);
}
public void Info(string message, Exception exception, params object[] args) {
Log(LogLevel.Info, exception, message, args);
}
public void Info(Exception exception, string message, params object[] args) {
Log(LogLevel.Info, exception, message, args);
}
private void InitializePane() {
ThreadHelper.ThrowIfNotOnUIThread();
if (Package.GetGlobalService(typeof(SVsOutputWindow)) is not IVsOutputWindow outputWindow)
throw new InvalidOperationException("Cannot get SVsOutputWindow service.");
outputWindow.CreatePane(_paneGuid, _paneTitle, 1, 1);
outputWindow.GetPane(_paneGuid, out _pane);
}
private static string GetLogLevelPrefix(LogLevel level) {
return level switch {
LogLevel.Debug => "DEBUG",
LogLevel.Info => "INFO",
LogLevel.Warning => "WARNING",
LogLevel.Error => "ERROR",
_ => "UNKNOWN"
};
}
private void Log(LogLevel level, Exception exception, string message, params object[] args) {
if (_pane == null) throw new InvalidOperationException("Output pane is not initialized.");
string formattedMessage = message;
try {
formattedMessage = string.Format(message, args);
} catch (FormatException) {
// ignore. CLI verbose logs
}
if (exception != null) {
// log traceback in separate output window pane
exception.Log();
// add dot if not exists
if (!formattedMessage.EndsWith(".")) formattedMessage += ".";
formattedMessage += $" {exception.GetType()} - {exception.Message}";
}
string logLevel = GetLogLevelPrefix(level);
string logMessage = $"{DateTime.Now:o} [{logLevel}] {formattedMessage}{Environment.NewLine}";
_pane?.OutputString(logMessage);
Console.Write(logMessage);
}
}