我使用的是Spectre.Console,有很多AnsiConsole.MarkupLine命令,如下所示:
AnsiConsole.MarkupLine($"[lime]File size:[/] [bold]\t{file.Length,-10}[/]");我想以明文输出相同的文本,文本文件中没有颜色,如下
var msg = $"[lime]File size:[/] [bold]\t{file.Length,-10}[/]"
AnsiConsole.MarkupLine(msg);
var msgclean = AnsiConsole.StripTag(msg);
LogToFile(msgclean);有办法去掉标签吗?还是以某种方式将控制台输出重定向到文件?
发布于 2022-07-20 13:29:02
看看下面的语言扩展是否适用于您。Regex模式取自这个职位。试着通过多管乐器。
public static class StringExtensions
{
private static readonly Regex Whitespace = new(@"\s+");
public static string Flatten(this string value)
=> value is null or "" ?
value :
Whitespace.Replace(value.Trim(), " ");
public static string StripCodes(this string sender)
=> Regex.Replace(sender, @"\[[^]]*\]", "")
.Flatten();
}https://stackoverflow.com/questions/73051618
复制相似问题