C#写log文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace log文件
{
    public partial class mlog : UserControl
    {
         FileStream LogFile;
         string str_date;  //log文件日期  
        List<string> str_list = new List<string>();
        int MsgMaxLine = 300;//最大行数
        public mlog()
        {
            InitializeComponent();
        }
        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;//分行
                                                      
                LogFile.Seek(0, SeekOrigin.End);//在第一行打印
                byData = System.Text.Encoding.Default.GetBytes(MsgStr);//设置格式
                if (LogFile.CanWrite) LogFile.Write(byData, 0, byData.Length);
                //  PrintMsg = MsgStr;//传递信息给界面显示
                str_list.Add(MsgStr.ToString());
                richTextBox1.AppendText(MsgStr.ToString() );
              //  richTextBox1.SelectedText = MsgStr.ToString() ;

                 richTextBox1.SelectionColor = System.Drawing.Color.DarkGray;
                if (richTextBox1.Lines.Count() > MsgMaxLine ) richTextBox1.Lines[0].Remove(0);

                richTextBox1.SelectionStart = richTextBox1.Text.Length;
            }

        }

        private void listBox1_Click(object sender, EventArgs e)
        {

        }

        private void mlog_Load(object sender, EventArgs e)
        {
            AddMsg("tset");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值