多线程日志类

csharp

浏览数:239

2019-1-8

    /// <summary>
    /// 多线程日志类
    /// </summary>
    public class MTLogger
    {
        private static readonly Dictionary<long, long> LockDic = new Dictionary<long, long>();
 
        /// <summary>
        ///     构造函数
        /// </summary> 
        /// <param name="fileName">文件全路径名</param>
        public MTLogger(string fileName)
        {
            FileName = fileName;
        }
 
        /// <summary>
        ///     获取或设置文件名称
        /// </summary>
        public string FileName { get; set; }
 
        /// <summary>
        ///     创建文件
        /// </summary>
        /// <param name="fileName"></param>
        public void Create(string fileName)
        {
            if (!File.Exists(fileName))
            {
                using (var fs = File.Create(fileName))
                {
                    fs.Close();
                }
            }
        }
 
        /// <summary>
        ///     写入文本
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="newLine"></param>
        private void Write(string content, string newLine)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                throw new Exception("FileName不能为空!");
            }
            using (var fs = new FileStream(FileName, FileMode.OpenOrCreate,
                    FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.Asynchronous))
            {
                //Byte[] dataArray = System.Text.Encoding.ASCII.GetBytes(System.DateTime.Now.ToString() + content + "/r/n");  
                var dataArray = Encoding.Default.GetBytes(content + newLine);
                var flag = true;
                long slen = dataArray.Length;
                long len = 0;
                while (flag)
                {
                    try
                    {
                        if (len >= fs.Length)
                        {
                            fs.Lock(len, slen);
                            LockDic[len] = slen;
                            flag = false;
                        }
                        else
                        {
                            len = fs.Length;
                        }
                    }
                    catch (Exception ex)
                    {
                        while (!LockDic.ContainsKey(len))
                        {
                            len += LockDic[len];
                        }
                    }
                }
                fs.Seek(len, SeekOrigin.Begin);
                fs.Write(dataArray, 0, dataArray.Length);
                fs.Close();
            }
        }
 
        /// <summary>
        ///     写入文件内容
        /// </summary>
        /// <param name="content"></param>
        public void WriteLine(string content)
        {
            Write(content, Environment.NewLine);
        }
 
        /// <summary>
        ///     写入文件
        /// </summary>
        /// <param name="content"></param>
        public void Write(string content)
        {
            Write(content, "");
        }
    }