带有彩色微词的扩展.NET控制台,多行固定,写入位置和最有用的utils。它的设计重点是用于在复杂任务中的专业使用:大量异步日志,需要突出显示重要和PIN摘要数据。 Xconsole可用于多任务处理,可安全可用于控制台缓冲区溢出(9000多行),易于使用。
主要功能如下图所示:
通过软件包管理器或将软件包引用添加到 *.csproj文件中。在Nuget Gallery上查看更多方法。
PM> Install-Package XConsole
< PackageReference Include = " XConsole " Version = " 1.5.* " />开始使用XConsole的简单方法是将using添加到您的代码中。之后,您可以像往常一样编写Console ,并获得Xconsole的所有功能。另外,您可以通过将以下行添加到 *.csproj文件来立即升级到XConsole。
// Safely upgrade a single code file to XConsole
using Console = Chubrik . XConsole . XConsole ; < ItemGroup >
<!-- Safely upgrade an entire project to XConsole -->
< Using Include = " Chubrik.XConsole " />
< Using Include = " Chubrik.XConsole.XConsole " Alias = " Console " />
</ ItemGroup >此技巧非常适合升级现有的常规控制台应用程序,因为XConsole与标准控制台兼容。升级后,除非您想开始使用XConsole功能,否则该应用程序将不会更改其工作方式。
要用标准控制台颜色为文本着色,您需要在字符串中添加前缀。前缀由一个或两个字母组成,表示颜色,然后是尾随的` ,然后文本立即开始。让我们将文本彩色字母表示为t ,而背景色字母则表示为b 。在这种情况下,有三个前缀模式: T`仅更改文本颜色, TB`以更改文本和背景颜色, B` (开头的空间)以仅更改背景颜色。要使多色消息将其分为具有单个颜色前缀的部分,并将其传递到单个WriteLine方法。确保您的消息不会被其他线程打破。
Console . WriteLine ( "G`This line is colored using simple microsyntax" ) ; // Single color
Console . WriteLine ( [ "C`It is easy " , "Wb`to use many" , "R` colors in " , "Y`one message" ] ) ; // Multicolor下表显示了所有标准控制台颜色及其字母名称:
如果您使用.NET 5+在Windows 10+上运行您的应用程序,则可以使用扩展颜色:
using Chubrik . XConsole . StringExtensions ;
// Different ways to the same 24-bit color
Console . WriteLine ( "Orange text" . Color ( Color . Orange ) ) ; // Color structure
Console . WriteLine ( "Orange text" . Color ( 255 , 165 , 0 ) ) ; // Red, green & blue
Console . WriteLine ( "Orange text" . Color ( 0xFFA500 ) ) ; // Hexadecimal number
Console . WriteLine ( "Orange text" . Color ( "#FFA500" ) ) ; // Hexadecimal string
// Combinations of text and background colors
Console . WriteLine ( "Orange with an indigo background" . Color ( Color . Orange ) . BgColor ( Color . Indigo ) ) ;
Console . WriteLine ( ( "Lime with " + "a brown" . BgColor ( Color . Brown ) + " background" ) . Color ( Color . Lime ) ) ;
Console . WriteLine ( $ "Aqua with { "a navy" . BgColor ( Color . Navy ) } background" . Color ( Color . Aqua ) ) ; Xconsole具有环境变量和属性的NO_COLOR标准。
Console . NO_COLOR = true ; // Disable all colors 您可以使用Pin方法固定一些常规日志消息的文本。固定文本可以是静态的或动态的,包含一条或多个线,当然可以颜色。每次调用Write或WriteLine方法时,动态引脚都会自动更新。引脚可抵抗线条包裹和控制台缓冲区溢出(9000多个日志线)。
Console . Pin ( "Simple static pin" ) ; // Simple static pin
Console . Pin ( [ "Y`Multicolor" , " static pin" ] ) ; // Multicolor static pin
Console . Pin ( "Multiline n static pin" ) ; // Multiline static pin
Console . Pin ( ( ) => "Simple pin, value=" + value ) ; // Simple dynamic pin
Console . Pin ( ( ) => [ "Y`Multicolor" , " pin, value=" , "C`" + value ] ) ; // Multicolor dynamic pin
Console . Pin ( ( ) => [ "Multiline pin, n value=" , "C`" + value ] ) ; // Multiline dynamic pin您还可以使用UpdatePin方法手动更新动态PIN。要删除PIN,请致电Unpin方法。
Console . UpdatePin ( ) ; // Update dynamic pin manually
Console . Unpin ( ) ; // Remove pin Xconsole提供了一个ConsolePosition结构,该结构是控制台缓冲区区域内的位置。如果您有很多日志消息,这是一个重要功能。该结构可抵抗控制台缓冲区溢出(9000多个日志线),并且始终指向控制台缓冲区区域内的正确位置。每个Write和WriteLine方法都会返回一个开始和终点位置。您可以保存它们并以后使用以重写文本或将文本添加到消息末尾。为此, ConsolePosition有自己的Write方法,它返回了新的终点位置。
NB! WriteLine的末端位置是指截止线断裂之前的最后一个字符之后的位置。
var ( begin , end ) = Console . WriteLine ( "Hello, World!" ) ; // Write message and save positions
begin . Write ( "C`HELLO" ) ; // Rewrite part of text
end = end . Write ( " I'm here" ) ; // Add text to the end and update end position
end . Write ( "Y`!" ) ;
您还可以使用CursorPosition属性获得或设置光标位置:
ConsolePosition position = Console . CursorPosition ; // Get cursor position
Console . CursorPosition = new ConsolePosition ( left : 15 , top : 5 ) ; // Set cursor position Readline还有两个其他模式用于安全输入:掩盖和隐藏。蒙版符号是可自定义的。
Console . ReadLine ( ConsoleReadLineMode . Masked ) ; // Masked ReadLine
Console . ReadLine ( ConsoleReadLineMode . Masked , maskChar : '#' ) ; // Masked ReadLine with custom mask
Console . ReadLine ( ConsoleReadLineMode . Hidden ) ; // Hidden ReadLine XConsole是根据MIT许可证获得许可的。