简体   繁体   English

C#输出日志填充

[英]C# Output Log Padding

I'm writing a log parser and want to sort the output data into columns, and at the moment use this code: 我正在编写一个日志解析器,并希望将输出数据分类为列,此刻使用此代码:

string OEMVersion = string.Format("{0,-20} {1}", "OEM Version", GetXMLData(doc, "OemMarkerVersion"));
string OEMID = string.Format("{0,-20} {1}", "OEM ID", GetXMLData(doc, "OemId"));
string OEMTableID = string.Format("{0,-20} {1}", "OEM TableID", GetXMLData(doc, "OemTableId"));
string Manufacturer = string.Format("{0,-20} {1}", "Manufacturer", GetXMLData(doc, "Manufacturer"));
string Model = string.Format("{0,-20} {1}", "Model", GetXMLData(doc, "Model"));

To sort the padding out but the output only looks okay with certain fonts (Consolas, Lucinda Console etc.). 要整理出填充,但是输出仅在使用某些字体(Consolas,Lucinda Console等)时看起来还可以。 How can I pad the columns with tabs rather than spaces to overcome this? 如何用制表符而不是空格来填充列以克服这一问题? string.Format() doesn't seem to allow me to do this. string.Format()似乎不允许我这样做。 Thanks. 谢谢。

Just add a tab character to your formats? 只是在格式中添加制表符?

string OEMVersion = string.Format("{0,-20}\t{1}", "OEM Version", GetXMLData(doc, "OemMarkerVersion"));
string OEMID = string.Format("{0,-20}\t{1}", "OEM ID", GetXMLData(doc, "OemId"));
string OEMTableID = string.Format("{0,-20}\t{1}", "OEM TableID", GetXMLData(doc, "OemTableId"));
string Manufacturer = string.Format("{0,-20}\t{1}", "Manufacturer", GetXMLData(doc, "Manufacturer"));
string Model = string.Format("{0,-20}\t{1}", "Model", GetXMLData(doc, "Model"));

You may want to keep the space padding to reduce the risk of tabs after shorter strings not going past the tabs of longer strings. 您可能需要保留空格,以减少较短的字符串不越过较长的字符串的制表符后出现制表符的风险。 Won;t be 100% accurate with proportional fonts but should be cleaner. 不会;使用比例字体不会100%准确,但应该更干净。

If you want to align properly your variables on a video output when a variable width font is used, then you need the help of some kind of tabular control like a ListView or a DataGridView 如果要在使用可变宽度字体时在视频输出上正确对齐变量,则需要某种列表控件(如ListViewDataGridView
(I am assuming a WinForm app but the concepts are the same for other technologies). (我假设使用WinForm应用程序,但其他技术的概念相同)。

The use of a control is preferable because the alternative is to measure the length in pixels of every string and use Graphics.DrawString to position your string parts at the exact pixel position. 最好使用控件,因为替代方法是测量每个字符串的像素长度,并使用Graphics.DrawString将字符串部分定位在确切的像素位置。

Let me show an example using a ListView 让我展示一个使用ListView的示例

// Create a new ListView control. (or use the designer)
ListView listView1 = new ListView();
// Position and size of the ListView
listView1.Bounds = new Rectangle(new Point(10,10), new Size(500,200));
// Some display feature....
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.GridLines = false;

// Add OEM Version value
ListViewItem item = new ListViewItem("OEM Version");
item.SubItems.Add(GetXMLData(doc, "OemMarkerVersion"));
listView1.Items.Add(item);

// Add OEM ID value
item = new ListViewItem("OEM ID");
item.SubItems.Add( GetXMLData(doc, "OemId"));
listView1.Items.Add(item);

... and so on for the others values and labels ....

// Some header formatting
listView1.Columns.Add("Label", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Data", 400, HorizontalAlignment.Left);

// Put the ListView on video....
yourForm.Controls.Add(listView1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM