如何将转义码转换为 html 或删除
How to convert escape codes to html or remove
ASP.NET 中的 Kestrel 5 Debian 中的核心 MVC 应用程序将转义码写入 /var/log/syslog 文本文件,如
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Now listening on: http://localhost:5000
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Application started. Press Ctrl+C to shut down.
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Hosting environment: Production
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Content root path: /var/www/store5
此文件由 MVC 控制器读取并发送给视图中的用户。
如何将此文件转换为 html(例如,使用彩色或斜体线条)或从文件中删除转义码以便文件更易于阅读?
或者如何强制 kestrel 输出没有转义码的纯文本?
控制器代码:
public IActionResult Syslog()
{
return new ContentResult()
{
Content = FileToStr("/var/log/syslog")
};
}
static string FileToStr(string cFileName)
{
StreamReader oReader = File.OpenText(cFileName);
string lcString = oReader.ReadToEnd();
oReader.Close();
return lcString;
}
转义序列定义为:
- 一个\x1b
- 一个[
- 零个或多个参数字节 0x30-0x3f
- 零个或多个中间字节 0x20-0x2f
- 最后一个字节 0x40-0x7f
更新
答案中的代码将跨度添加到每一行的开头并且不删除转义序列:
<span style="color: blue">Apr 8 00:00:05 ew rsyslogd: [origin software="rsyslogd" swVersion="8.1901.0" x-pid="573" x-info="https://www.rsyslog.com"] rsyslogd was HUPed</span>
<span style="color: blue">Apr 8 00:00:05 ew systemd[1]: logrotate.service: Succeeded.</span>
<span style="color: blue">Apr 8 00:00:05 ew systemd[1]: Started Rotate log files.</span>
<span style="color: blue">Apr 8 00:00:10 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:00:12 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:00:14 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:05:01 ew CRON[23838]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)</span>
<span style="color: blue">Apr 8 00:15:01 ew CRON[24082]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)</span>
<span style="color: blue">Apr 8 00:17:01 ew CRON[24128]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: WebOptimizer.AssetMiddleware[1000]</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: Request started for '/c/version.js'</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: #033[41m#033[30mfail#033[39m#033[22m#033[49m: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: An unhandled exception has occurred while executing the request.</span>
预期输出为:
希望这对您有所帮助。我用rextester.com写了一个格式化代码。但是文件测试在那里不起作用。
如有必要,请更正此 post。也不是最短的版本,但我认为是最清晰的。
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
using (var reader = new StreamReader(@"c:\your_input_file.txt"))
using (var writer = new StreamWriter(@"c:\your_output_file.html"))
{
WriteHtmlHeader(writer);
string line;
while((line = reader.ReadLine()) != null)
{
line = AddSpan(line);
}
WriteHtmlFooter(writer);
}
}
private static void WriteHtmlHeader(StreamWriter outfile)
{
// writing all the header and the start of body...
// you can take it from another file
}
private static void WriteHtmlFooter(StreamWriter outfile)
{
// writing the footer of your html file
}
private static string AddSpan(string line)
{
if (Regex.IsMatch(line, "\x1b"))
{
return AddStyledSpan(line, "color: green");
}
else if (Regex.IsMatch(line, "\["))
{
return AddStyledSpan(line, "color: blue");
}
// ...
else
{
// Mark this span as error or standard
return AddStyledSpan(line, "color: red");
}
}
private static string AddStyledSpan(string line, string style)
{
return "<span style=\"" + style + "\">" + line + "</span>";
}
}
}
希望这对您有所帮助。还看看
regex101 - online regex tester
和
debuggex - regex visualization。
(但请谨慎使用,正则表达式有时会有所不同)。
ASP.NET 中的 Kestrel 5 Debian 中的核心 MVC 应用程序将转义码写入 /var/log/syslog 文本文件,如
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Now listening on: http://localhost:5000
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Application started. Press Ctrl+C to shut down.
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Hosting environment: Production
Apr 5 22:02:21 ew kestrel-store[31907]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: Microsoft.Hosting.Lifetime[0]
Apr 5 22:02:21 ew kestrel-store[31907]: Content root path: /var/www/store5
此文件由 MVC 控制器读取并发送给视图中的用户。
如何将此文件转换为 html(例如,使用彩色或斜体线条)或从文件中删除转义码以便文件更易于阅读? 或者如何强制 kestrel 输出没有转义码的纯文本?
控制器代码:
public IActionResult Syslog()
{
return new ContentResult()
{
Content = FileToStr("/var/log/syslog")
};
}
static string FileToStr(string cFileName)
{
StreamReader oReader = File.OpenText(cFileName);
string lcString = oReader.ReadToEnd();
oReader.Close();
return lcString;
}
转义序列定义为:
- 一个\x1b
- 一个[
- 零个或多个参数字节 0x30-0x3f
- 零个或多个中间字节 0x20-0x2f
- 最后一个字节 0x40-0x7f
更新
答案中的代码将跨度添加到每一行的开头并且不删除转义序列:
<span style="color: blue">Apr 8 00:00:05 ew rsyslogd: [origin software="rsyslogd" swVersion="8.1901.0" x-pid="573" x-info="https://www.rsyslog.com"] rsyslogd was HUPed</span>
<span style="color: blue">Apr 8 00:00:05 ew systemd[1]: logrotate.service: Succeeded.</span>
<span style="color: blue">Apr 8 00:00:05 ew systemd[1]: Started Rotate log files.</span>
<span style="color: blue">Apr 8 00:00:10 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:00:12 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:00:14 ew colord[1172]: failed to get session [pid 23699]: No data available</span>
<span style="color: blue">Apr 8 00:05:01 ew CRON[23838]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)</span>
<span style="color: blue">Apr 8 00:15:01 ew CRON[24082]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)</span>
<span style="color: blue">Apr 8 00:17:01 ew CRON[24128]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: #033[40m#033[32minfo#033[39m#033[22m#033[49m: WebOptimizer.AssetMiddleware[1000]</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: Request started for '/c/version.js'</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: #033[41m#033[30mfail#033[39m#033[22m#033[49m: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]</span>
<span style="color: blue">Apr 8 00:21:49 ew kestrel-store[22413]: An unhandled exception has occurred while executing the request.</span>
预期输出为:
希望这对您有所帮助。我用rextester.com写了一个格式化代码。但是文件测试在那里不起作用。
如有必要,请更正此 post。也不是最短的版本,但我认为是最清晰的。
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
using (var reader = new StreamReader(@"c:\your_input_file.txt"))
using (var writer = new StreamWriter(@"c:\your_output_file.html"))
{
WriteHtmlHeader(writer);
string line;
while((line = reader.ReadLine()) != null)
{
line = AddSpan(line);
}
WriteHtmlFooter(writer);
}
}
private static void WriteHtmlHeader(StreamWriter outfile)
{
// writing all the header and the start of body...
// you can take it from another file
}
private static void WriteHtmlFooter(StreamWriter outfile)
{
// writing the footer of your html file
}
private static string AddSpan(string line)
{
if (Regex.IsMatch(line, "\x1b"))
{
return AddStyledSpan(line, "color: green");
}
else if (Regex.IsMatch(line, "\["))
{
return AddStyledSpan(line, "color: blue");
}
// ...
else
{
// Mark this span as error or standard
return AddStyledSpan(line, "color: red");
}
}
private static string AddStyledSpan(string line, string style)
{
return "<span style=\"" + style + "\">" + line + "</span>";
}
}
}
希望这对您有所帮助。还看看 regex101 - online regex tester 和 debuggex - regex visualization。 (但请谨慎使用,正则表达式有时会有所不同)。