1.新建一个WPF的应用;

 

 2.拖拽控件并布局好:

【小技巧】选中控件,点击”回形针“即可让该控件跟随窗口自动调整大小;

 

 3.编写代码:

主程序代码如下:

namespace WpfApp1
{
    delegate void ShowMsgDelegate(ref TextBox textbox1, string msg);//定义委托类型
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ShowMsgDelegate showMsgDelegate = null;//定义委托变量
        public MainWindow()
        {
            InitializeComponent();
            TextBox1.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            this.MyButton1.Click += ButtonClicked;//button1是手动代码挂接事件,Button2是IDE双击自动挂接,效果相同
           
            showMsgDelegate += ShowMsg;//关联委托
        }

        LogSave logSave1 = new LogSave();
        private void ButtonClicked(object sender, RoutedEventArgs e)
        {
            if (sender == this.MyButton1)
            {
                string msg1 = $"{MyButton1.Name}按钮被按下"+"\r\n";
                this.MyTextBox.Text = msg1;
                logSave1.AddMsg(msg1);
                showMsgDelegate.Invoke(ref TextBox1, msg1);
            }
            else if (sender == this.MyButton2)
            {
                string msg1 = $"{MyButton2.Name}按钮被按下" + "\r\n";
                this.MyTextBox.Text = msg1;
                logSave1.AddMsg(msg1);
                showMsgDelegate.Invoke(ref TextBox1, msg1);
            }
            else if (sender == this.MyButton3)
            {
                TextBox1.Clear();
            }
        }
        public void  ShowMsg(ref TextBox textbox1, string msg)
        {
            string timestring = DateTime.Now.ToString();
            msg = timestring + "|" + msg;
            textbox1.Text=textbox1.Text.Insert(0, msg);
        }

    }
}

View Code

log记录的代码:

namespace WpfApp1
{
    class LogSave
    {
            FileStream LogFile;
            string str_date;  //log文件日期  
            List<string> str_list = new List<string>();
            public void AddMsg(string MsgStr)
            {
                byte[] byData;

                if (LogFile == null || 0 != string.Compare(str_date, DateTime.Now.ToString("yyyy-MM-dd")))
                {
                    String str;
                    str = Path.GetFullPath("..") + "\\log\\";
                    if (!Directory.Exists(str))
                    {
                        //文件夹不存在则创建
                        Directory.CreateDirectory(str);
                    }
                    try
                    {
                        if (LogFile != null)
                        {
                            LogFile.Close();
                            LogFile = null;
                        }
                        str += DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                        if (!File.Exists(str))
                        {
                            LogFile = new FileStream(str, FileMode.Create);
                        }
                        else
                        {
                            LogFile = new FileStream(str, FileMode.Open);

                        }
                        str_date = DateTime.Now.ToString("yyyy-MM-dd");
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
                if (LogFile != null)
                {
                    string StrTime = DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss.fff");
                    MsgStr = $"<{StrTime}>>{MsgStr}\n";//加入log   

                    //MsgStr = MsgStr + Environment.NewLine;//分行
                    byData = System.Text.Encoding.Default.GetBytes(MsgStr);//设置格式
                    if (LogFile.CanWrite) LogFile.Write(byData, 0, byData.Length);
                    str_list.Add(MsgStr);
                    LogFile.Seek(0, SeekOrigin.End);//在第一行打印
                }
            }
    }
}

View Code

项目:

 

 最终运行效果:

【小技巧】让最新的log消息显示在首行;