/// <summary> /// LogMethodInvocation /// </summary> /// <param name="serviceCode"></param> /// <param name="opCode"></param> /// <param name="callStart"></param> /// <param name="callCompleted"></param> private void LogMethodInvocation(int serviceCode, int opCode, DateTime callStart, bool callCompleted) { if (!LogServiceCalls) { return; } string serviceName = ((Native.ServiceCodes)serviceCode).ToString(); string msg; Type t = // is there a better way to find the correct assembly? Type.GetType("Mobius.Services.Native.ServiceOpCodes." + serviceName); string opName = Enum.GetName(t, opCode); if (!callCompleted) { msg = "[ServiceCallBegin] - " + serviceName + "." + opName; } else { msg = "[ServiceCallEnd] - " + serviceName + "." + opName + ", Time: " + (int)(DateTime.Now.Subtract(callStart).TotalMilliseconds) + " ms"; } ServicesLog.Message(msg); return; }
/// <summary> /// Set the ServicesLog to write to the proper file /// </summary> static void InitializeServicesLogFileName() { try { string iniFilePath = NativeSessionInitializer.GetMobiusServicesIniFileLocation(); if (Lex.IsUndefined(iniFilePath) || !File.Exists(iniFilePath)) { return; } IniFile iniFile = new IniFile(iniFilePath, "Mobius"); string logDir = iniFile.Read("LogDirectory"); if (!Lex.IsDefined(logDir)) { return; } string logFile = logDir + @"\" + CommonConfigInfo.ServicesLogFileName; // log file name ServicesLog.Initialize(logFile); // initialize for logging } catch (Exception ex) { ServicesLog.Message(DebugLog.FormatExceptionMessage(ex)); } }
/// <summary> /// Catch an log any unhandled exception /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = (Exception)e.ExceptionObject; string msg = "Unhandled exception: " + DebugLog.FormatExceptionMessage(ex); ServicesLog.Message(msg); if (NativeDebugMode) { Console.WriteLine("Press <ENTER>"); Console.ReadLine(); } }
public void Execute(string command) { try { command = command.Replace("%20", " "); // convert any special URL characters (todo: more complete translation) DebugLog.Message("MobiusClientIntegrationPoint Command: " + Lex.AddDoubleQuotes(command)); CommandExec.PostCommand(command); } catch (Exception ex) { string msg = "Error executing external command: " + command + "\n\n" + DebugLog.FormatExceptionMessage(ex); ServicesLog.Message(msg); MessageBoxMx.ShowError(msg); } }
private void Search_Click(object sender, EventArgs e) { try { DialogResult dr = ProcessInput(); if (dr == DialogResult.OK) { DialogResult = dr; } return; } catch (Exception ex) { string msg = "Unexpected error: " + ex.Message + "\r\n" + DebugLog.FormatExceptionMessage(ex); ServicesLog.Message(msg); MessageBoxMx.ShowError(msg); } }
/// <summary> /// Main method for native session host /// </summary> /// <param name="args"></param> public static void StartUp(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); bool debug = (args.Length == 0); InitializeServicesLogFileName(); try { // Debug mode if (debug) { LogServiceCalls = true; NativeDebugMode = true; ServicesLog.LogToConsole = true; Initialize(); OpenServiceHost(); // Start the service host int processId = Process.GetCurrentProcess().Id; string endpointAddress = NativeSessionEndpointAddress.Build(processId); ServicesLog.Message("NativeSessionHost started in debug mode at endpoint address: " + endpointAddress); StartIdleMonitor(); // debug idle monitor } // Started by HostingApp else { ServiceHostProcessId = int.Parse(args[0]); string creatorClassTypeName = args[1]; int sessionId = int.Parse(args[2]); AppSettingsReader asr = new AppSettingsReader(); string enableLoggingStr = asr.GetValue("EnableNativeSessionLogging", typeof(string)) as string; bool.TryParse(enableLoggingStr, out LogServiceCalls); Initialize(); OpenServiceHost(); // Start the service host listening for requests RegisterSession(ServiceHostProcessId, sessionId); // register session with the service host that started us string msg = "MobiusNativeSession started for session " + sessionId + " in process " + Process.GetCurrentProcess().Id; Version version = Assembly.GetExecutingAssembly().GetName().Version; string tok = VersionMx.FormatVersion(version); msg += ", Services: " + tok; ServicesLog.Message(msg); // log initial message StartIdleMonitor(); // start thread that will exit if idle too long } return; } catch (Exception ex) { string msg = DebugLog.FormatExceptionMessage(ex); if (debug) { MessageBox.Show(msg); } ServicesLog.Message(msg); return; } }
/// <summary> /// IdleMonitor - Loop to check if session is idle /// - IdleTime - Amount of time since last service call /// - UnfreshenedTime - Amount of time since last Freshen() call /// </summary> private static void IdleMonitor() { TimeSpan maxIdleTime, maxUnfreshenedTime, checkInterval, idleTime = TimeSpan.Zero, unfreshenedTime = TimeSpan.Zero; int maxIdleMinutes = 10; int maxUnfreshenedMinutes = 0; // not defined bool isNative = (InternalSession != null && InternalSession.IsNativeSession); // MobiusClient sessions are Native sessions if (isNative) { maxIdleMinutes = ServicesIniFile.IniFile.ReadInt("MaxNativeSessionIdleTime", 0); // max idle time for native Mobius client (0 = no limit) maxUnfreshenedMinutes = ServicesIniFile.IniFile.ReadInt("MaxNativeUnfreshenedTime", 0); // max unrefreshed time for native Mobius client } else { maxIdleMinutes = ServicesIniFile.IniFile.ReadInt("MaxNonnativeSessionIdleTime", 0); // max idle time for non-native clients maxUnfreshenedMinutes = ServicesIniFile.IniFile.ReadInt("MaxNonnativeUnfreshenedTime", 0); // max unrefreshed time for non-native clients (0 = no limit) } //DebugLog.Message(InternalSession.ToString()); // debug //DebugLog.Message("MaxIdleTime: " + minutes); maxIdleTime = TimeSpan.FromMinutes(maxIdleMinutes); // max idle time before exiting process maxUnfreshenedTime = TimeSpan.FromMinutes(maxUnfreshenedMinutes); // max unfreshened time before exiting process checkInterval = TimeSpan.FromMinutes(1); // time between checks DateTime now = DateTime.Now; DateTime lastCheck = now; LastFreshen = now; LastInvocationFinish = now; DateTime lastInvocationTimeSentToServiceHost = now; DateTime lastInvocationActiveTime = now; while (true) { Thread.Sleep(checkInterval); if (InvocationDepth > 0) { lastInvocationActiveTime = DateTime.Now; } else { lastInvocationActiveTime = LastInvocationFinish; } now = DateTime.Now; idleTime = now.Subtract(lastInvocationActiveTime); // how long since last method invocation unfreshenedTime = now.Subtract(LastFreshen); // how long since last freshen // Check of idle for too long if (maxIdleMinutes != 0 && idleTime.CompareTo(maxIdleTime) >= 0) // exit if idle limit defined and idling too long { // and also not freshened within freshen limit if (maxUnfreshenedMinutes == 0 || unfreshenedTime.CompareTo(maxUnfreshenedTime) >= 0) { string msg = "Idle time limit (" + maxIdleTime + ") exceeded, exiting session process " + Process.GetCurrentProcess().Id + " for user: "******"Unrefreshed idle time limit (" + maxUnfreshenedTime + ") exceeded, exiting session process " + Process.GetCurrentProcess().Id + " for user: "******", (disabled, MaxNativeUnfreshenedTime = " + ServicesIniFile.IniFile.ReadInt("MaxNativeUnfreshenedTime", 10) + ")"; ServicesLog.Message(msg); } } if (!lastInvocationActiveTime.Equals(lastInvocationTimeSentToServiceHost)) { UpdateServiceHostWithLastInvocationTime(lastInvocationActiveTime); lastInvocationTimeSentToServiceHost = lastInvocationActiveTime; } lastCheck = now; } // Dispose the session //ServicesLog.Message("IdleTime: " + idleTime); // debug //ServicesLog.Message("MaxIdleTime: " + maxIdleTime); //ServicesLog.Message("CheckInterval: " + checkInterval); //ServicesLog.Message("LastCheck: " + lastCheck); //ServicesLog.Message("LastInvocation: " + LastInvocation); DisposeSession(); System.Environment.Exit(0); // be sure exiting }