代码实现了,就是有弊端, 输出 要是不加 \r\n 字符就会全在一行
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace LLog
{
/// <summary>
/// 与控制台交互
/// </summary>
static class XLog
{
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
/// <summary>
/// 修改控制台标题
/// </summary>
public static string Title
{
get
{
string result;
try
{
result = Console.Title;
}
catch
{
result = "";
}
return result;
}
set
{
Console.Title = value;
}
}
/// <summary>
/// 调用输出日志方法从c1-c15 共15种颜色
/// </summary>
/// <param name="code">[c1]蓝色 [c2]青色(蓝绿色) [c3]藏蓝色 [c4]深紫色(深蓝绿色) [c5]深灰色 [c6]深绿色 [c7]深紫红色 [c8]深红色 [c9]深黄色 [c10]灰色 [c11]绿色 [c12]紫红色 [c13]红色 [c14]白色 [c15]黄色 [c16]默认灰色</param>
public static void Logx(string code)
{
//①②③④⑤⑥⑦⑧⑨⑩⑾⑿⒀⒁⒂
string str = code;
string output = str.Replace("[/c1]", "①").Replace("[/c2]", "②").Replace("[/c3]", "③").Replace("[/c4]", "④").Replace("[/c5]", "⑤").Replace("[/c6]", "⑥").Replace("[/c7]", "⑦").Replace("[/c8]", "⑧").Replace("[/c9]", "⑨").Replace("[/c10]", "⑩").Replace("[/c11]", "⑾").Replace("[/c12]", "⑿").Replace("[/c13]", "⒀").Replace("[/c14]", "⒁").Replace("[/c15]", "⒂").Replace("[/c16]","⒃");
string display = "";
foreach (char c in output)
{
if (c == '①')
{
Console.ForegroundColor = ConsoleColor.Blue;
}
if (c == '②')
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
if (c == '③')
{
Console.ForegroundColor = ConsoleColor.DarkBlue;
}
if (c == '④')
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
}
if (c == '⑤')
{
Console.ForegroundColor = ConsoleColor.DarkGray;
}
if (c == '⑥')
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
}
if (c == '⑦')
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
}
if (c == '⑧')
{
Console.ForegroundColor = ConsoleColor.DarkRed;
}
if (c == '⑨')
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
}
if (c == '⑩')
{
Console.ForegroundColor = ConsoleColor.Gray;
}
if (c == '⑾')
{
Console.ForegroundColor = ConsoleColor.Green;
}
if (c == '⑿')
{
Console.ForegroundColor = ConsoleColor.Magenta;
}
if (c == '⒀')
{
Console.ForegroundColor = ConsoleColor.Red;
}
if (c == '⒁')
{
Console.ForegroundColor = ConsoleColor.White;
}
if (c == '⒂')
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
if (c == '⒃')
{
Console.ForegroundColor = ConsoleColor.Gray;
}
display = c.ToString();
display = display.Replace("①", "").Replace("②", "").Replace("③", "").Replace("④", "").Replace("⑤", "").Replace("⑥", "").Replace("⑦", "").Replace("⑧", "").Replace("⑨", "").Replace("⑩", "").Replace("⑾", "").Replace("⑿", "").Replace("⒀", "").Replace("⒁", "").Replace("⒂", "").Replace("⒃", "");
Console.Write(display);
}
}
}
}
|