Exemplo n.º 1
0
        protected override bool DoSaveLog(LogEntity logEntity)
        {
            try
            {
                //读取配置文件固定位置的日志级别
                string logLevel = WRSetting.Set().getSettings("LogLevel");
                if (logLevel.Equals(""))
                {
                    logLevel = LogLevel.ERROR;
                }

                foreach (string level in logEntity.Levels)
                {
                    if (!LogLevel.needLog(level, logLevel))
                    {
                        continue;
                    }

                    string logfile = string.Format(@"{0}\{1}", logPath, level);
                    if (!Directory.Exists(logfile))
                    {
                        Directory.CreateDirectory(logfile); //不存在则创建文件夹
                    }

                    logfile = string.Format(@"{0}\{1}.log", logfile, DateTime.Now.ToString("yyyyMMdd"));

                    FileStream fs;
                    if (File.Exists(logfile))
                    {
                        fs = new FileStream(logfile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 8, FileOptions.Asynchronous);
                    }
                    else
                    {
                        fs = new FileStream(logfile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, FileOptions.Asynchronous);
                    }

                    Byte[] dataArray = Encoding.Default.GetBytes(logEntity.Content.Message + Environment.NewLine);
                    bool   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
                        {
                            while (!lockDic.ContainsKey(len))
                            {
                                len += lockDic[len];
                            }
                        }
                    }
                    fs.Seek(len, SeekOrigin.Begin);
                    fs.Write(dataArray, 0, dataArray.Length);
                    fs.Close();
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 执行保存
 /// </summary>
 /// <param name="logEntity"></param>
 /// <returns></returns>
 protected abstract bool DoSaveLog(LogEntity logEntity);