using System; using System.Globalization; using System.IO; using System.Reflection; using System.Security; using System.Security.Permissions; using Microsoft.Win32; namespace ArchestrA.Diagnostics; internal static class Logger { public static int ErrorCount { get { if (InitLoggerDll()) { int errorCount = 0; int warningCount = 0; long ftLastError = 0L; long ftLastWarning = 0L; if (NativeMethods.GetLoggerStats(string.Empty, ref errorCount, ref ftLastError, ref warningCount, ref ftLastWarning) <= 0) { return -1; } return errorCount; } return -1; } } public static bool IsLoaded { get; private set; } public static int WarningCount { get { if (InitLoggerDll()) { int errorCount = 0; int warningCount = 0; long ftLastError = 0L; long ftLastWarning = 0L; if (NativeMethods.GetLoggerStats(string.Empty, ref errorCount, ref ftLastError, ref warningCount, ref ftLastWarning) <= 0) { return -1; } return warningCount; } return -1; } } private static bool CheckRegistry { get; set; } private static bool DomainUnloaded { get; set; } private static int LoggerClientIdentity { get; set; } static Logger() { LoggerClientIdentity = 0; CheckRegistry = true; } public static void LogConnection(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogConnection(LoggerClientIdentity, errorMessage); } } public static void LogCtorDtor(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogCtorDtor(LoggerClientIdentity, errorMessage); } } public static void LogCustom(int cookie, string errorMessage) { if (Initialize()) { NativeMethods.InternalLogCustom(LoggerClientIdentity, cookie, errorMessage); } } public static void LogEntryExit(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogEntryExit(LoggerClientIdentity, errorMessage); } } public static void LogError(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogError(LoggerClientIdentity, errorMessage); } } public static void LogInfo(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogInfo(LoggerClientIdentity, errorMessage); } } public static void LogRefCount(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogRefCount(LoggerClientIdentity, errorMessage); } } public static int LogRegisterCustomFlag(string flagName) { if (!Initialize()) { return 0; } return NativeMethods.RegisterLogFlag(LoggerClientIdentity, 11, flagName); } public static int LogRegisterCustomFlagEx(string flagName, int defaultValue) { if (!Initialize()) { return 0; } return NativeMethods.RegisterLogFlagEx(LoggerClientIdentity, 11, flagName, defaultValue); } public static void LogSQL(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogSQL(LoggerClientIdentity, errorMessage); } } public static void LogSetIdentityName(string identityName) { if (Initialize()) { NativeMethods.SetIdentityName(LoggerClientIdentity, identityName); } } public static void LogStartStop(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogStartStop(LoggerClientIdentity, errorMessage); } } public static void LogThreadStartStop(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogThreadStartStop(LoggerClientIdentity, errorMessage); } } public static void LogTrace(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogTrace(LoggerClientIdentity, errorMessage); } } public static void LogWarning(string errorMessage) { if (Initialize()) { NativeMethods.InternalLogWarning(LoggerClientIdentity, errorMessage); } } public static void ResetLoggerCheck() { CheckRegistry = true; } private static bool InitLoggerDll() { bool flag = IsLoaded; if (flag) { return true; } if (CheckRegistry) { IntPtr intPtr = IntPtr.Zero; CheckRegistry = false; new RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\\Software\\ArchestrA\\Framework\\Logger").Assert(); try { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\ArchestrA\\Framework\\Logger", writable: false); if (registryKey != null) { string text = Convert.ToString(registryKey.GetValue("InstallPath", string.Empty), CultureInfo.InvariantCulture); if (text.Length > 0) { intPtr = NativeMethods.LoadLibraryExW(wParam: new IntPtr(0), lpMdoule: Path.Combine(text, "LoggerDll.dll"), flag: 8); } } } finally { CodeAccessPermission.RevertAssert(); } flag = intPtr != IntPtr.Zero; } IsLoaded = flag; return flag; } private static bool Initialize() { if (DomainUnloaded) { return false; } if (LoggerClientIdentity != 0) { return true; } if (InitLoggerDll()) { int hIdentity = 0; int num = NativeMethods.RegisterLoggerClient(ref hIdentity); LoggerClientIdentity = hIdentity; if (num != 0 && LoggerClientIdentity != 0) { new FileIOPermission(PermissionState.Unrestricted).Assert(); try { NativeMethods.SetIdentityName(LoggerClientIdentity, Assembly.GetExecutingAssembly().GetName().Name); } finally { CodeAccessPermission.RevertAssert(); } AppDomain.CurrentDomain.DomainUnload += OnCurrentDomainUnload; return true; } } return false; } private static void OnCurrentDomainUnload(object sender, EventArgs e) { UnInitialize(); DomainUnloaded = true; } private static void UnInitialize() { if (LoggerClientIdentity != 0) { NativeMethods.UnregisterLoggerClient(LoggerClientIdentity); LoggerClientIdentity = 0; } } }