Skip to content

Commit e48244b

Browse files
committed
Added logging configuration to support .NET 6+
Supports environment variable / registry based overrides
1 parent 9a85d7e commit e48244b

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

Source/ExcelDna.IntelliSense.Host/App.config

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
<source name="ExcelDna.IntelliSense" switchValue="All">
77
<listeners>
88
<!--<remove name="Default" />-->
9-
<!--<add name="LogDisplay" type="ExcelDna.Logging.LogDisplayTraceListener,ExcelDna.Integration">
10-
<filter type="System.Diagnostics.EventTypeFilter" initializeData="All"/>
11-
</add>-->
12-
<!-- <add name="File" type="System.Diagnostics.TextWriterTraceListener" initializeData="ExcelDna.IntelliSense.log" >
13-
<filter type="System.Diagnostics.EventTypeFilter" initializeData="All"/>
14-
</add> -->
9+
<add name="File" type="System.Diagnostics.TextWriterTraceListener" initializeData="ExcelDna.IntelliSense.log" >
10+
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Information"/>
11+
</add>
1512
</listeners>
1613
</source>
1714
</sources>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Diagnostics;
2+
3+
namespace ExcelDna.Logging
4+
{
5+
internal class DiagnosticsFilter : TraceFilter
6+
{
7+
private TraceEventType filterLevel;
8+
9+
public DiagnosticsFilter(TraceEventType filterLevel)
10+
{
11+
this.filterLevel = filterLevel;
12+
}
13+
14+
public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] args, object data1, object[] data)
15+
{
16+
return eventType <= filterLevel;
17+
}
18+
}
19+
}

Source/ExcelDna.IntelliSense/IntelliSenseServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ExcelDna.IntelliSense
3030
// REMEMBER: COM events are not necessarily safe macro contexts.
3131
public static class IntelliSenseServer
3232
{
33-
const string ServerVersion = "1.7.3"; // TODO: Define and manage this somewhere else
33+
const string ServerVersion = "1.8.0"; // TODO: Define and manage this somewhere else
3434

3535
// NOTE: Do not change these constants in custom versions.
3636
// They are part of the co-operative safety mechanism allowing different add-ins providing IntelliSense to work together safely.

Source/ExcelDna.IntelliSense/Logging.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Globalization;
77
using System.Security;
8+
using ExcelDna.Logging;
89

910
namespace ExcelDna.IntelliSense
1011
{
@@ -77,10 +78,22 @@ public static void Initialize()
7778
{
7879
if (!s_LoggingInitialized)
7980
{
81+
LoggingSettings settings = new LoggingSettings();
82+
8083
bool loggingEnabled = false;
8184
// DOCUMENT: By default the TraceSource is configured to source only Warning, Error and Fatal.
8285
// the configuration can override this.
83-
IntelliSenseTraceSource = new TraceSource(TraceSourceName, SourceLevels.Warning);
86+
IntelliSenseTraceSource = new TraceSource(TraceSourceName, settings.SourceLevel);
87+
88+
Debug.Print("{0} TraceSource created. Listeners:", TraceSourceName);
89+
foreach (TraceListener tl in IntelliSenseTraceSource.Listeners)
90+
{
91+
Debug.Print(" {0} - {1}", tl.Name, tl.TraceOutputOptions);
92+
if (tl.Name == "Default" && settings.DebuggerLevel.HasValue)
93+
{
94+
tl.Filter = new DiagnosticsFilter(settings.DebuggerLevel.Value);
95+
}
96+
}
8497

8598
try
8699
{
@@ -100,8 +113,19 @@ public static void Initialize()
100113
Close();
101114
loggingEnabled = false;
102115
}
116+
103117
if (loggingEnabled)
104118
{
119+
if (!string.IsNullOrWhiteSpace(settings.FileName))
120+
{
121+
Trace.AutoFlush = true;
122+
123+
TextWriterTraceListener textWriterTraceListener = new TextWriterTraceListener(settings.FileName, "FileWriter");
124+
if (settings.FileLevel.HasValue)
125+
textWriterTraceListener.Filter = new DiagnosticsFilter(settings.FileLevel.Value);
126+
IntelliSenseTraceSource.Listeners.Add(textWriterTraceListener);
127+
}
128+
105129
AppDomain currentDomain = AppDomain.CurrentDomain;
106130
//currentDomain.UnhandledException += UnhandledExceptionHandler;
107131
currentDomain.DomainUnload += AppDomainUnloadEvent;
@@ -147,8 +171,14 @@ static void AppDomainUnloadEvent(object sender, EventArgs e)
147171

148172
static void Close()
149173
{
174+
AppDomain currentDomain = AppDomain.CurrentDomain;
175+
currentDomain.DomainUnload -= AppDomainUnloadEvent;
176+
currentDomain.ProcessExit -= ProcessExitEvent;
177+
150178
if (IntelliSenseTraceSource != null)
151179
IntelliSenseTraceSource.Close();
180+
181+
s_LoggingInitialized = false;
152182
}
153183

154184
}
@@ -249,4 +279,5 @@ public void Error(Exception ex, string message, params object[] args)
249279
static internal Logger Display { get; } = new Logger(IntelliSenseTraceEventId.Display);
250280
static internal Logger Monitor { get; } = new Logger(IntelliSenseTraceEventId.Monitor);
251281
}
282+
252283
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Diagnostics;
4+
5+
namespace ExcelDna.Logging
6+
{
7+
internal class LoggingSettings
8+
{
9+
public SourceLevels SourceLevel { get; }
10+
public TraceEventType? DebuggerLevel { get; }
11+
public TraceEventType? FileLevel { get; }
12+
public string FileName { get; }
13+
14+
public LoggingSettings()
15+
{
16+
SourceLevel = Enum.TryParse(GetCustomSetting("SOURCE_LEVEL", "SourceLevel"), out SourceLevels sourceLevelResult) ? sourceLevelResult : SourceLevels.Warning;
17+
18+
if (Enum.TryParse(GetCustomSetting("DEBUGGER_LEVEL", "DebuggerLevel"), out TraceEventType debuggerLevelResult))
19+
DebuggerLevel = debuggerLevelResult;
20+
21+
if (Enum.TryParse(GetCustomSetting("FILE_LEVEL", "FileLevel"), out TraceEventType fileLevelResult))
22+
FileLevel = fileLevelResult;
23+
24+
FileName = GetCustomSetting("FILE_NAME", "FileName");
25+
}
26+
27+
private static string GetCustomSetting(string environmentName, string registryName)
28+
{
29+
return Environment.GetEnvironmentVariable($"EXCELDNA_INTELLISENSE_DIAGNOSTICS_{environmentName}") ??
30+
(Registry.GetValue(@"HKEY_CURRENT_USER\Software\ExcelDna\IntelliSense\Diagnostics", registryName, null) as string ??
31+
Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\ExcelDna\IntelliSense\Diagnostics", registryName, null) as string);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)