public override void Run() { // There are four levels of log messages Info, Warning, Error, Debug. MyLog.Info("Info from Run"); for (int i = 0; i < 10; i++) { MyLog.Debug("Debug {0} from Run", i); // MyLog.X works like string.Format with regards to arguments. } MyLog.Warning("Warning from Run"); MyLog.Error("Error from Run"); // The Log can accept a Stopwatch Object to be used for timing analysis. Stopwatch sw1 = Stopwatch.StartNew(); TapThread.Sleep(100); MyLog.Info(sw1, "Info from Run"); Stopwatch sw2 = Stopwatch.StartNew(); TapThread.Sleep(200); MyLog.Error(sw2, "Error from step"); // Tracebar can be used to show results in the MyLog. var traceBar = new TraceBar(); traceBar.LowerLimit = -3.0; for (var i = -2; i < 11; i++) { traceBar.UpperLimit = i < 5 ? 3 : 15; // GetBar returns a string with value, low limit, a dashed line // indicating magnitude, the upper limit, and (if failing), a fail indicator. string temp = traceBar.GetBar(i); MyLog.Info("MyResult: " + temp); TapThread.Sleep(200); } // Sample output shown below. // MyResult: 2.00 - 3-------------------------|----- 3 // MyResult: 3.00 - 3------------------------------ | 3 // MyResult: 4.00 - 3------------------------------ > 3 Fail // MyResult: 5.00 - 3------------ -| -----------------15 // MyResult: 6.00 - 3-------------- -| ---------------15 // TraceBar remembers if any results failed, so it can be used for the verdict. UpgradeVerdict(traceBar.CombinedVerdict); // The log also supports showing stack traces. // Useful for debugging. try { throw new Exception("My exception"); } catch (Exception e) { MyLog.Error("Caught exception: '{0}'", e.Message); MyLog.Debug(e); // Prints the stack trace to the MyLog. } }
/// <summary> /// Initializes the logging. /// </summary> public static void Initialize(string tempLogFileName) { if (currentLogFile == null) { Rename(tempLogFileName); SystemInfoTask = Task.Factory // Ensure that the plugin manager is loaded before running SystemInfo. // this ensures that System.Runtime.InteropServices.RuntimeInformation.dll is loaded. (issue #4000). .StartNew(PluginManager.Load) // then get the system info on a separate thread (it takes ~1s) .ContinueWith(tsk => SystemInfo()); AppDomain.CurrentDomain.ProcessExit += FlushOnExit; AppDomain.CurrentDomain.UnhandledException += FlushOnExit; } else { if (currentLogFile != tempLogFileName) { Rename(tempLogFileName); } } currentLogFile = tempLogFileName; // Log debugging information of the current process. log.Debug($"Running '{Environment.CommandLine}' in '{Directory.GetCurrentDirectory()}'."); }
void Propagate(IResultListener rt, ResultTable result) { try { rt.OnResultPublished(stepRun.Id, result); } catch (Exception e) { log.Warning("Caught exception in result handling task."); log.Debug(e); planRun.RemoveFaultyResultListener(rt); } }
///<summary>Searches for plugins.</summary> public static void Search() { searchTask.Reset(); searcher = null; assemblyResolver.Invalidate(DirectoriesToSearch); ChangeID++; try { IEnumerable <string> fileNames = assemblyResolver.GetAssembliesToSearch(); searcher = SearchAndAddToStore(fileNames); } catch (Exception e) { log.Error("Caught exception while searching for plugins: '{0}'", e.Message); log.Debug(e); searcher = null; } finally { searchTask.Set(); } }
/// <summary> /// Blocks the thread while closing all resources in parallel. /// </summary> private void CloseAllResources() { Dictionary <IResource, List <IResource> > dependencies = openedResources.ToDictionary(r => r.Resource, r => new List <IResource>()); // this dictionary will hold resources with dependencies (keys) and what resources depend on them (values) foreach (var n in openedResources) { foreach (var dep in n.StrongDependencies) { dependencies[dep].Add(n.Resource); } } Dictionary <IResource, Task> closeTasks = new Dictionary <IResource, Task>(); foreach (var r in openedResources) { closeTasks[r.Resource] = new Task(o => { ResourceNode res = (ResourceNode)o; // Wait for the resource to open to open before closing it. // in rare cases, another instrument failing open will cause close to be called. try { openTasks[res.Resource].Wait(); } catch { } // wait for resources that depend on this resource (res) to close before closing this Task.WaitAll(dependencies[res.Resource].Select(x => closeTasks[x]).ToArray()); var reslog = GetLogSource(res.Resource); Stopwatch timer = Stopwatch.StartNew(); try { res.Resource.Close(); } catch (Exception e) { log.Error("Error while closing \"{0}\": {1}", res.Resource.Name, e.Message); log.Debug(e); } if (reslog != null) { reslog.Info(timer, "Resource \"{0}\" closed.", res.Resource); } }, r); } closeTasks.Values.ToList().ForEach(t => t.Start()); void complainAboutWait() { log.Debug("Waiting for resources to close:"); foreach (var res in openTasks.Keys) { if (res.IsConnected) { log.Debug(" - {0}", res); } } } using (TimeoutOperation.Create(complainAboutWait)) Task.WaitAll(closeTasks.Values.ToArray()); }