public async Task pwsh_kernel_can_read_variables_from_other_kernels( string from, string codeToWrite, string codeToRead) { using var _ = await ConsoleLock.AcquireAsync(); using var kernel = CreateKernel(); await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}"); var results = await kernel.SubmitCodeAsync($"#!pwsh\n{codeToRead}"); results.KernelEvents.ToSubscribedList().Should() .ContainSingle <StandardOutputValueProduced>() .Which .FormattedValues .Should() .ContainSingle(v => v.MimeType == PlainTextFormatter.MimeType) .Which .Value .Trim() .Should() .Be("123:System.Int32"); }
private void LogException(Exception ex) { if (ex == null || ex is AggregateException) { return; } using (ConsoleLock.Lock(ConsoleColor.Red)) { Console.WriteLine($"STARTUP EXCEPTION: {ex}"); } }
public async Task pwsh_kernel_variables_shared_from_other_kernels_resolve_to_the_correct_runtime_type(string from, string codeToWrite, string codeToRead) { using var _ = await ConsoleLock.AcquireAsync(); using var kernel = CreateKernel(); using var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SubmitCodeAsync($"{from}\n{codeToWrite}"); await kernel.SubmitCodeAsync($"#!pwsh\n{codeToRead}\n$x + 1"); events.Should() .ContainSingle <StandardOutputValueProduced>() .Which .FormattedValues .Should() .ContainSingle(v => v.MimeType == PlainTextFormatter.MimeType) .Which .Value .Trim() .Should() .Be("2"); }
/// <summary> /// Asyncronously logs the log message. /// </summary> /// <param name="msg">The log message to log.</param> /// <param name="logFile">True if the log file should be written to.</param> /// <param name="errorFile">True if the error file should be written to.</param> public virtual async Task LogAsync(LogMessage msg, bool logFile = true, bool errorFile = false, bool noticeFile = false) { // Ignore these annoying messages if (msg.Exception?.InnerException is WebSocketException || msg.Exception?.InnerException is WebSocketClosedException) { return; } if (logFile) { if (!Directory.Exists(LogDirectory)) // Create the log directory if it doesn't exist { Directory.CreateDirectory(LogDirectory); } if (!File.Exists(LogFile)) // Create today's log file if it doesn't exist { File.Create(LogFile).Dispose(); } } if (errorFile || msg.Exception != null) { if (!Directory.Exists(ErrorDirectory)) // Create the error directory if it doesn't exist { Directory.CreateDirectory(ErrorDirectory); } if (!File.Exists(ErrorFile)) // Create today's error file if it doesn't exist { File.Create(ErrorFile).Dispose(); } } if (noticeFile) { if (!Directory.Exists(NoticeDirectory)) // Create the log directory if it doesn't exist { Directory.CreateDirectory(NoticeDirectory); } if (!File.Exists(NoticeFile)) // Create today's log file if it doesn't exist { File.Create(NoticeFile).Dispose(); } } string logText = FormatMessage(msg); if (logFile) { try { File.AppendAllText(LogFile, logText + Environment.NewLine); // Write the log text to a file } catch (IOException) { } } if (errorFile || msg.Exception != null) { try { File.AppendAllText(ErrorFile, logText + Environment.NewLine); // Write the error text to a file } catch (IOException) { } } if (noticeFile) { try { File.AppendAllText(NoticeFile, logText + Environment.NewLine); // Write the notice text to a file } catch (IOException) { } } bool shouldPrint = msg.Severity <= LogSeverity.Info || msg.Source == "Command"; switch (msg.Severity) { case LogSeverities.Debug: shouldPrint = logDebug; break; case LogSeverities.Trace: shouldPrint = logTrace; break; case LogSeverities.Print: shouldPrint = logPrint; break; case LogSeverities.Notice: shouldPrint = logNotice; break; case LogSeverities.Report: shouldPrint = true; break; } if (shouldPrint) { // Write the log text to the console ConsoleColor?severityColor = GetSeverityColor(msg.Severity); if (severityColor.HasValue) { using (ConsoleLock.Lock(severityColor)) Console.WriteLine(logText); } else { await Console.Out.WriteLineAsync(logText).ConfigureAwait(false); } } }