Commit 094093a6 authored by tr's avatar tr

1、日志查看:增加编码选择(默认(GB2312)、UTF-8、ASCII)

parent 121a941d
......@@ -3,7 +3,7 @@
<appSettings>
<add key="LoginInfo" value="wwF/xpBzWek=" />
<add key="LoginVerify" value="v86f5Q3Xa7U=" />
<add key="UpdateTime" value="2021-10-11 18:11:12.770" />
<add key="UpdateTime" value="2021-10-12 13:45:01.513" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
......
......@@ -36,6 +36,9 @@
this.textBox1 = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.toolStripLabel2 = new System.Windows.Forms.ToolStripLabel();
this.tscbEncoding = new System.Windows.Forms.ToolStripComboBox();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
......@@ -46,6 +49,9 @@
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripLabel2,
this.tscbEncoding,
this.toolStripSeparator1,
this.toolStripLabel1,
this.tstbLogPath,
this.tsbSelectPath});
......@@ -123,6 +129,23 @@
this.progressBar1.Size = new System.Drawing.Size(535, 2);
this.progressBar1.TabIndex = 3;
//
// toolStripLabel2
//
this.toolStripLabel2.Name = "toolStripLabel2";
this.toolStripLabel2.Size = new System.Drawing.Size(44, 22);
this.toolStripLabel2.Text = "编码:";
//
// tscbEncoding
//
this.tscbEncoding.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.tscbEncoding.Name = "tscbEncoding";
this.tscbEncoding.Size = new System.Drawing.Size(75, 25);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
//
// wLogViewer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
......@@ -156,5 +179,8 @@
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ToolStripLabel toolStripLabel2;
private System.Windows.Forms.ToolStripComboBox tscbEncoding;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
}
}
\ No newline at end of file
......@@ -28,15 +28,25 @@ namespace Tools.UI
//窗体:加载
private void wLogViewer_Load(object sender, EventArgs e)
{
var list = new List<string> {
Encoding.Default.HeaderName.ToUpper(),
Encoding.UTF8.HeaderName.ToUpper(),
Encoding.ASCII.HeaderName.ToUpper(),
};
this.tscbEncoding.Items.AddRange(list.Distinct().ToArray());
this.tscbEncoding.SelectedIndex = 0;
}
//窗体:显示完成
private void wLogViewer_Shown(object sender, EventArgs e)
{
this.tsbSelectPath.Click += TsbSelectPath_Click;
this.tstbLogPath.TextChanged += TstbLogPath_TextChanged;
this.tscbEncoding.SelectedIndexChanged += TscbEncoding_SelectedIndexChanged;
this.listBox1.SelectedIndexChanged += ListBox1_SelectedIndexChanged;
}
/// <summary>
/// 选择路径
/// </summary>
......@@ -56,15 +66,59 @@ namespace Tools.UI
this.InitListBox();
}
Task _task = null;
CancellationTokenSource _tokenSource = new CancellationTokenSource();
/// <summary>
/// 编码切换:重新解析日志文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TscbEncoding_SelectedIndexChanged(object sender, EventArgs e)
{
this.OpenLogFile();
}
/// <summary>
/// 列表切换:加载选中日志文件
/// </summary>
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.OpenLogFile();
}
/// <summary>
/// 日志文件类型(兼容原有的txt日志和log4net日志)
/// </summary>
List<string> LogExtensions = new List<string> { ".txt", ".log" };
/// <summary>
/// 初始化列表框
/// </summary>
private void InitListBox()
{
//清空原有项
this.textBox1.Text = string.Empty;
this.listBox1.Items.Clear();
//重新加载
if (Directory.Exists(this.LogPath))
{
var dirInfo = new DirectoryInfo(this.LogPath);
var files = dirInfo.GetFiles().Where(f => this.LogExtensions.Contains(f.Extension.ToLower())).Select(f => f.Name);
this.listBox1.Items.AddRange(files.ToArray());
}
else
{
Utils.Tip("路径不存在,请检查!");
}
}
Task _task = null; //当前任务
CancellationTokenSource _tokenSource = new CancellationTokenSource(); //取消令牌
/// <summary>
/// 打开日志文件
/// </summary>
private void OpenLogFile()
{
var fileName = this.listBox1.SelectedItem.ToString();
var isDefault = fileName.ToLower().EndsWith(".log");
var encode = Encoding.Default;
if (!string.IsNullOrEmpty(this.tscbEncoding.Text))
encode = Encoding.GetEncoding(this.tscbEncoding.Text);
//清空信息
this.textBox1.Text = string.Empty;
//如果上一个任务还未结束,则取消
......@@ -81,8 +135,8 @@ namespace Tools.UI
//读取日志内容
var buffer = new byte[1024];
var fs = new FileStream($@"{this.LogPath}\{fileName}", FileMode.Open, FileAccess.Read);
//设定
this.AsyncUI(() => {
//初始化进度条
this.AsyncUI(() => {
this.progressBar1.Maximum = Math.Max(1, (int)(fs.Length / 1024));
this.progressBar1.Value = 0;
});
......@@ -94,7 +148,7 @@ namespace Tools.UI
if (this.progressBar1.Value < this.progressBar1.Maximum)
this.progressBar1.Value += 1;
});
sb.Append(isDefault ? Encoding.Default.GetString(buffer) : Encoding.UTF8.GetString(buffer));
sb.Append(encode.GetString(buffer));
}
//显示日志内容
this.AsyncUI(() => this.textBox1.Text = sb.ToString());
......@@ -103,31 +157,5 @@ namespace Tools.UI
fs.Close();
}, token);
}
/// <summary>
/// 日志文件类型(兼容原有的txt日志和log4net日志)
/// </summary>
List<string> LogExtensions = new List<string> { ".txt", ".log" };
/// <summary>
/// 初始化列表框
/// </summary>
private void InitListBox()
{
//清空原有项
this.textBox1.Text = string.Empty;
this.listBox1.Items.Clear();
//重新加载
if (Directory.Exists(this.LogPath))
{
var dirInfo = new DirectoryInfo(this.LogPath);
var files = dirInfo.GetFiles().Where(f => this.LogExtensions.Contains(f.Extension.ToLower())).Select(f => f.Name);
this.listBox1.Items.AddRange(files.ToArray());
}
else
{
Utils.Tip("路径不存在,请检查!");
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment