|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Security; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace ExcelDna.Integration |
| 8 | +{ |
| 9 | + // This class supports intenal logging. |
| 10 | + // Internal logging is implemented with the System.Diagnostics tracing implementation. |
| 11 | + |
| 12 | + // Add a trace listener for the ExcelDna.Integration source which logs warnings and errors to the LogDisplay |
| 13 | + // (only popping up the window for errors). |
| 14 | + // Verbose logging can be configured via the .config file |
| 15 | + |
| 16 | + // We define a TraceSource called ExcelDna.Integration (that is also exported to ExcelDna.Loader and called from there) |
| 17 | + // We consolidate the two assemblies against a single TraceSource, since ExcelDna.Integration is the only public contract, |
| 18 | + // and we expect to move more of the registration into the ExcelDna.Integration assembly in future. |
| 19 | + |
| 20 | + #region Microsoft License |
| 21 | + // The logging helper implementation here is adapted from the Logging.cs file for System.Net |
| 22 | + // Taken from https://github.com/Microsoft/referencesource/blob/c697a4b9782dc8c85c02344a847cb68163702aa7/System/net/System/Net/Logging.cs |
| 23 | + // Under the following license: |
| 24 | + // |
| 25 | + // The MIT License (MIT) |
| 26 | + |
| 27 | + // Copyright (c) Microsoft Corporation |
| 28 | + |
| 29 | + // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 30 | + // of this software and associated documentation files (the "Software"), to deal |
| 31 | + // in the Software without restriction, including without limitation the rights |
| 32 | + // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 33 | + // copies of the Software, and to permit persons to whom the Software is |
| 34 | + // furnished to do so, subject to the following conditions: |
| 35 | + |
| 36 | + // The above copyright notice and this permission notice shall be included in all |
| 37 | + // copies or substantial portions of the Software. |
| 38 | + |
| 39 | + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 40 | + // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 41 | + // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 42 | + // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 43 | + // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 44 | + // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 45 | + // SOFTWARE. |
| 46 | + #endregion |
| 47 | + |
| 48 | + enum IntegrationTraceEventId |
| 49 | + { |
| 50 | + RegistrationInitialize = 1025, |
| 51 | + RegistrationEvent = 1026 // Everything is miscellaneous |
| 52 | + } |
| 53 | + |
| 54 | + class TraceLogging |
| 55 | + { |
| 56 | + static volatile bool s_LoggingEnabled = true; |
| 57 | + static volatile bool s_LoggingInitialized; |
| 58 | + static volatile bool s_AppDomainShutdown; |
| 59 | + const string TraceSourceName = "ExcelDna.Integration"; |
| 60 | + internal static TraceSource IntegrationTraceSource; // Retrieved from ExcelDna.Loader through ExcelIntegration |
| 61 | + |
| 62 | + public static void Initialize() |
| 63 | + { |
| 64 | + if (!s_LoggingInitialized) |
| 65 | + { |
| 66 | + bool loggingEnabled = false; |
| 67 | + IntegrationTraceSource = new TraceSource(TraceSourceName, SourceLevels.All); |
| 68 | + //GlobalLog.Print("Initalizating tracing"); |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + loggingEnabled = (IntegrationTraceSource.Switch.ShouldTrace(TraceEventType.Critical)); |
| 73 | + } |
| 74 | + catch (SecurityException) |
| 75 | + { |
| 76 | + // These may throw if the caller does not have permission to hook up trace listeners. |
| 77 | + // We treat this case as though logging were disabled. |
| 78 | + Close(); |
| 79 | + loggingEnabled = false; |
| 80 | + } |
| 81 | + if (loggingEnabled) |
| 82 | + { |
| 83 | + AppDomain currentDomain = AppDomain.CurrentDomain; |
| 84 | + //currentDomain.UnhandledException += UnhandledExceptionHandler; |
| 85 | + currentDomain.DomainUnload += AppDomainUnloadEvent; |
| 86 | + currentDomain.ProcessExit += ProcessExitEvent; |
| 87 | + } |
| 88 | + s_LoggingEnabled = loggingEnabled; |
| 89 | + s_LoggingInitialized = true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static bool ValidateSettings(TraceSource traceSource, TraceEventType traceLevel) |
| 94 | + { |
| 95 | + if (!s_LoggingEnabled) |
| 96 | + { |
| 97 | + return false; |
| 98 | + } |
| 99 | + if (!s_LoggingInitialized) |
| 100 | + { |
| 101 | + Initialize(); |
| 102 | + } |
| 103 | + if (traceSource == null || !traceSource.Switch.ShouldTrace(traceLevel)) |
| 104 | + { |
| 105 | + return false; |
| 106 | + } |
| 107 | + if (s_AppDomainShutdown) |
| 108 | + { |
| 109 | + return false; |
| 110 | + } |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + static void ProcessExitEvent(object sender, EventArgs e) |
| 115 | + { |
| 116 | + Close(); |
| 117 | + s_AppDomainShutdown = true; |
| 118 | + } |
| 119 | + |
| 120 | + private static void AppDomainUnloadEvent(object sender, EventArgs e) |
| 121 | + { |
| 122 | + Close(); |
| 123 | + s_AppDomainShutdown = true; |
| 124 | + } |
| 125 | + |
| 126 | + static void Close() |
| 127 | + { |
| 128 | + if (IntegrationTraceSource != null) |
| 129 | + IntegrationTraceSource.Close(); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + class RegistrationLogging |
| 135 | + { |
| 136 | + public static void Log(TraceEventType eventType, string message, params object[] args) |
| 137 | + { |
| 138 | + Debug.Write(string.Format("RegistrationLogging: {0:yyyy-MM-dd HH:mm:ss} {1} {2}\r\n", DateTime.Now, eventType, string.Format(message, args))); |
| 139 | + |
| 140 | + TraceLogging.IntegrationTraceSource.TraceEvent(eventType, (int)IntegrationTraceEventId.RegistrationEvent, message, args); |
| 141 | + } |
| 142 | + |
| 143 | + public static void Info(string message, params object[] args) |
| 144 | + { |
| 145 | + Log(TraceEventType.Information, message, args); |
| 146 | + } |
| 147 | + |
| 148 | + public static void Warn(string message, params object[] args) |
| 149 | + { |
| 150 | + Log(TraceEventType.Warning, message, args); |
| 151 | + } |
| 152 | + |
| 153 | + public static void Error(string message, params object[] args) |
| 154 | + { |
| 155 | + Log(TraceEventType.Error, message, args); |
| 156 | + } |
| 157 | + |
| 158 | + public static void ErrorException(string message, Exception ex) |
| 159 | + { |
| 160 | + Log(TraceEventType.Error, "{0} : {1} - {2}", message, ex.GetType().Name.ToString(), ex.Message); |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | +} |
0 commit comments