-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLog.cs
187 lines (155 loc) · 4.47 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using WordLight.Extensions;
namespace WordLight
{
public static class Log
{
private enum LogLevels
{
Info,
Debug,
Warning,
Error
}
private static Microsoft.VisualStudio.OLE.Interop.IServiceProvider _serviceProvider;
private static object _serviceProviderSyncRoot = new object();
private static string _packageName;
private static DTE2 _application;
private static bool _enabled;
public static bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;
if (value)
Info("Enabled logging");
}
}
public static void Initialize(DTE2 application, string packageName)
{
if (application == null) throw new ArgumentNullException("application");
if (string.IsNullOrEmpty(packageName)) throw new ArgumentException("packageName");
_application = application;
_packageName = packageName;
if (_serviceProvider == null)
{
lock (_serviceProviderSyncRoot)
{
if (_serviceProvider == null)
_serviceProvider = application as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
}
}
#if DEBUG
Enabled = true;
#endif
}
#region "Message methods"
public static void Debug(string message)
{
#if DEBUG
LogMessage(LogLevels.Debug, message);
#endif
}
public static void Debug(string format, params object[] args)
{
#if DEBUG
LogMessage(LogLevels.Debug, string.Format(format, args));
#endif
}
public static void Info(string message)
{
LogMessage(LogLevels.Info, message);
}
public static void Info(string format, params object[] args)
{
LogMessage(LogLevels.Info, string.Format(format, args));
}
public static void Warning(string message)
{
LogMessage(LogLevels.Warning, message);
}
public static void Warning(string format, params object[] args)
{
LogMessage(LogLevels.Warning, string.Format(format, args));
}
public static void Error(string message)
{
LogMessage(LogLevels.Error, message);
}
public static void Error(string format, params object[] args)
{
LogMessage(LogLevels.Error, string.Format(format, args));
}
public static void Error(string message, Exception ex)
{
LogMessage(LogLevels.Error,
string.Format("{0} -> Exception: {1}. Message: {2}. Stack trace: {3}", message,
ex.GetType().ToString(), ex.Message, ex.StackTrace));
}
#endregion
private static IVsActivityLog GetActivityLog()
{
IVsActivityLog log = null;
lock (_serviceProviderSyncRoot)
{
if (_serviceProvider != null)
{
using (ServiceProvider wrapperSP = new ServiceProvider(_serviceProvider))
{
log = (IVsActivityLog)wrapperSP.GetService(typeof(SVsActivityLog));
}
}
}
return log;
}
private static OutputWindowPane GetLogPane()
{
string title = _packageName;
OutputWindowPanes panes = _application.ToolWindows.OutputWindow.OutputWindowPanes;
OutputWindowPane logPane;
try
{
logPane = panes.Item(title); // If the pane exists already, return it.
}
catch (ArgumentException)
{
// Create a new pane.
logPane = panes.Add(title);
logPane.WriteLine("Activity log for " + _packageName);
logPane.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().FullName);
logPane.WriteLine("----------------------------------------------------");
}
return logPane;
}
private static void LogMessage(LogLevels level, string message)
{
if (!_enabled)
return;
message = string.Format("{0}\t{1}:\t{2}", DateTime.Now, level.ToString(), message);
var outputPane = GetLogPane();
if (outputPane != null)
{
outputPane.WriteLine(message);
}
var log = GetActivityLog();
if (log != null)
{
var activityLogType = __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION;
if (level == LogLevels.Warning)
activityLogType = __ACTIVITYLOG_ENTRYTYPE.ALE_WARNING;
else if (level == LogLevels.Error)
activityLogType = __ACTIVITYLOG_ENTRYTYPE.ALE_ERROR;
log.LogEntry((UInt32)activityLogType, _packageName, message);
}
}
}
}