Esempio n. 1
0
        // 重新用不同详细级别获取事项内容
        // parameters:
        //      item    要被重新获取内容的对象
        //      nLevel  内容详细级别。0/1/2。0 为最详细级
        public OperLogItem LoadOperLogItem(OperLogItem item, int nLevel)
        {
            OperLogItemLoader loader = new OperLogItemLoader();

            loader.Stop            = this.Stop;
            loader.Channel         = this.Channel;
            loader.estimate        = null;//
            loader.FileName        = item.Date + ".log";
            loader.Level           = nLevel;
            loader.lServerFileSize = -1;
            loader.Range           = item.Index.ToString() + "-" + item.Index.ToString();
            loader.AutoCache       = this.AutoCache;
            loader.lProgressValue  = 0;
            loader.lSize           = 0;
            loader.Filter          = this.Filter;
            loader.LogType         = this.LogType;

#if NO
            loader.Prompt -= new MessagePromptEventHandler(loader_Prompt);
            loader.Prompt += new MessagePromptEventHandler(loader_Prompt);
#endif
            foreach (OperLogItem new_item in loader)
            {
                return(new_item);
            }

            return(null);
        }
Esempio n. 2
0
        // 重新用不同详细级别获取事项内容
        // parameters:
        //      item    要被重新获取内容的对象
        //      nLevel  内容详细级别。0/1/2。0 为最详细级
        public OperLogItem LoadOperLogItem(OperLogItem item, int nLevel)
        {
            OperLogItemLoader loader = new OperLogItemLoader();
            loader.Stop = this.Stop;
            loader.Channel = this.Channel;
            loader.estimate = null;//
            loader.FileName = item.Date + ".log";
            loader.Level = nLevel;
            loader.lServerFileSize = -1;
            loader.Range = item.Index.ToString() + "-" + item.Index.ToString();
            loader.AutoCache = this.AutoCache;
            loader.lProgressValue = 0;
            loader.lSize = 0;
            loader.Filter = this.Filter;
            loader.LogType = this.LogType;

#if NO
                    loader.Prompt -= new MessagePromptEventHandler(loader_Prompt);
                    loader.Prompt += new MessagePromptEventHandler(loader_Prompt);
#endif
            foreach (OperLogItem new_item in loader)
            {
                return new_item;
            }

            return null;
        }
Esempio n. 3
0
        public IEnumerator GetEnumerator()
        {
            string strError = "";
            int    nRet     = 0;

            long lRet = 0;

            if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0 &&
                (this.LogType & dp2Circulation.LogType.OperLog) != 0)
            {
                throw new ArgumentException("OperLogItemLoader 的 LogType 只能使用一种类型");
            }

            if (this.Stop != null && this.estimate != null)
            {
                this.Stop.SetMessage("正在装入日志文件 " + this.FileName + " 中的记录。"
                                     + "剩余时间 " + ProgressEstimate.Format(estimate.Estimate(lProgressValue)) + " 已经过时间 " + ProgressEstimate.Format(estimate.delta_passed));
            }

            string strXml = "";
            long   lAttachmentTotalLength = 0;

            byte[] attachment_data = null;

            long lFileSize = 0;

            if (lServerFileSize == -1)
            {
                long _lServerFileSize = 0;

                string strStyle = "level-" + Level.ToString();
                if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0)
                {
                    strStyle += ",accessLog";
                }

                // 获得服务器端日志文件尺寸
                lRet = this.Channel.GetOperLog(
                    this.Stop,
                    this.FileName,
                    -1, // lIndex,
                    -1, // lHint,
                    strStyle,
                    "", // strFilter
                    out strXml,
                    out _lServerFileSize,
                    0,  // lAttachmentFragmentStart,
                    0,  // nAttachmentFramengLength,
                    out attachment_data,
                    out lAttachmentTotalLength,
                    out strError);
                // 2015/11/25
                if (lRet == -1)
                {
                    throw new Exception(strError);
                }
                this.lServerFileSize = _lServerFileSize;

                if (lRet == 0)
                {
                    yield break;
                }
                // 2015/11/25
                if (_lServerFileSize == -1)
                {
                    yield break;    // 此类型的日志尚未启用
                }
            }

            Stream stream           = null;
            bool   bCacheFileExist  = false;
            bool   bRemoveCacheFile = false; // 是否要自动删除未全部完成的本地缓存文件

            bool bAutoCache = this.AutoCache;

            if (bAutoCache == true)
            {
                string strFileName = this.FileName;
                if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0)
                {
                    strFileName = this.FileName + ".a";
                }

                nRet = PrepareCacheFile(
                    this.CacheDir,
                    strFileName,    // this.FileName,
                    lServerFileSize,
                    out bCacheFileExist,
                    out stream,
                    out strError);
                if (nRet == -1)
                {
                    throw new Exception(strError);
                }

                if (bCacheFileExist == false && stream != null)
                {
                    bRemoveCacheFile = true;
                }
            }

            try
            {
                if (bCacheFileExist == true)
                {
                    lFileSize = stream.Length;
                }
                else
                {
                    lFileSize = lServerFileSize;
                }

                // stop.SetProgressRange(0, lTotalSize);

                if (String.IsNullOrEmpty(Range) == true)
                {
                    Range = "0-" + (long.MaxValue - 1).ToString();    // "0-9999999999";
                }
                RangeList rl = new RangeList(Range);

                for (int i = 0; i < rl.Count; i++)
                {
                    RangeItem ri = (RangeItem)rl[i];

                    // 让 100- 这样的 range 可以使用
                    if (ri.lLength == -1)
                    {
                        ri.lLength = long.MaxValue - ri.lStart;
                    }

                    OperLogInfo[] records       = null;
                    long          lStartRecords = 0;

                    long lHint     = -1;
                    long lHintNext = -1;
                    for (long lIndex = ri.lStart; lIndex < ri.lStart + ri.lLength; lIndex++)
                    {
                        Application.DoEvents();

                        if (this.Stop != null && this.Stop.State != 0)
                        {
                            strError = "用户中断";
                            throw new Exception(strError);
                            // yield break; ?
                        }

                        if (lIndex == ri.lStart)
                        {
                            lHint = -1;
                        }
                        else
                        {
                            lHint = lHintNext;
                        }

                        if (bCacheFileExist == true)
                        {
                            if (lHint == -1)
                            {
                                // return:
                                //      -1  error
                                //      0   成功
                                //      1   到达文件末尾或者超出
                                nRet = LocationRecord(stream,
                                                      lIndex,
                                                      out strError);
                                if (nRet == -1)
                                {
                                    throw new Exception(strError);
                                }
                            }
                            else
                            {
                                // 根据暗示找到
                                if (lHint == stream.Length)
                                {
                                    break;
                                }

                                if (lHint > stream.Length)
                                {
                                    strError = "lHint参数值不正确";
                                    throw new Exception(strError);
                                }
                                if (stream.Position != lHint)
                                {
                                    stream.Seek(lHint, SeekOrigin.Begin);
                                }
                            }

                            nRet = ReadCachedEnventLog(
                                stream,
                                out strXml,
                                out lAttachmentTotalLength,
                                out strError);
                            if (nRet == -1)
                            {
                                throw new Exception(strError);
                            }
                            lHintNext = stream.Position;
                        }
                        else
                        {
                            if (records == null || lIndex /*- ri.lStart*/ >= lStartRecords + records.Length)
                            {
                                int nCount = -1;
                                if (ri.lLength >= Int32.MaxValue)
                                {
                                    nCount = -1;    // 500;   // -1;
                                }
                                else
                                {
                                    nCount = (int)ri.lLength;   // Math.Min(500, (int)ri.lLength);
                                }
                                string strStyle = "level-" + Level.ToString();
                                if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0)
                                {
                                    strStyle += ",accessLog";
                                }
REDO:
                                // 获得日志
                                // return:
                                //      -1  error
                                //      0   file not found
                                //      1   succeed
                                //      2   超过范围,本次调用无效
                                lRet = this.Channel.GetOperLogs(
                                    this.Stop,
                                    this.FileName,
                                    lIndex,
                                    lHint,
                                    nCount,
                                    strStyle,
                                    this.Filter, // strFilter
                                    out records,
                                    out strError);
                                if (lRet == -1)
                                {
#if NO
                                    DialogResult result = MessageBox.Show(owner,
                                                                          "获取日志信息 (" + this.FileName + " " + lIndex.ToString() + ") 的操作发生错误: " + strError + "\r\n\r\n是否重试操作?\r\n\r\n(是: 重试;  否: 跳过本次操作,继续后面的操作; 放弃: 停止全部操作)",
                                                                          "OperLogItemLoader",
                                                                          MessageBoxButtons.YesNoCancel,
                                                                          MessageBoxIcon.Question,
                                                                          MessageBoxDefaultButton.Button1);
                                    if (result == DialogResult.Yes)
                                    {
                                        goto REDO;
                                    }
                                    if (result == DialogResult.Cancel)
                                    {
                                        throw new Exception(strError);
                                    }
                                    else
                                    {
                                        // TODO: 是否要在listview中装入一条表示出错的行?
                                        lHintNext = -1;
                                        continue;
                                    }
#endif
                                    if (this.Prompt != null)
                                    {
                                        MessagePromptEventArgs e = new MessagePromptEventArgs();
                                        e.MessageText = "获取 " + this._logType.ToString() + " 日志信息 (" + this.FileName + " " + lIndex.ToString() + ") 的操作发生错误: " + strError;
                                        e.Actions     = "yes,no,cancel";
                                        this.Prompt(this, e);
                                        if (e.ResultAction == "cancel")
                                        {
                                            throw new Exception(strError);
                                        }
                                        else if (e.ResultAction == "yes")
                                        {
                                            if (this.Stop != null)
                                            {
                                                this.Stop.Continue();
                                            }
                                            goto REDO;
                                        }
                                        else
                                        {
                                            lHintNext = -1;
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception(strError);
                                    }
                                }
                                if (lRet == 0)
                                {
                                    yield break;
                                }

                                if (lRet == 2)
                                {
                                    break;
                                }

                                // records数组表示的起点位置
                                lStartRecords = lIndex /* - ri.lStart*/;
                            }
                            OperLogInfo info = records[lIndex - lStartRecords];

                            strXml    = info.Xml;
                            lHintNext = info.HintNext;
                            lAttachmentTotalLength = info.AttachmentLength;

                            // 写入本地缓存的日志文件
                            if (stream != null)
                            {
                                try
                                {
                                    WriteCachedEnventLog(
                                        stream,
                                        strXml,
                                        lAttachmentTotalLength);
                                }
                                catch (Exception ex)
                                {
                                    strError = "写入本地缓存文件的时候出错: " + ex.Message;
                                    throw new Exception(strError);
                                }
                            }
                        }

#if NO
                        // 2011/12/30
                        // 日志记录可能动态地增加了,超过了原先为ProgressBar设置的范围
                        if (lFizeTotalSize < (int)lHintNext)
                        {
                            lFizeTotalSize = lHintNext;

                            stop.SetProgressRange(0, lFizeTotalSize);
                        }
#endif
                        if (lHintNext >= 0)
                        {
                            // 校正
                            if (lProgressValue + lHintNext > lSize)
                            {
                                lSize = lProgressValue + lHintNext;

                                if (this.Stop != null && this.estimate != null)
                                {
                                    this.Stop.SetProgressRange(0, lSize);
                                    estimate.SetRange(0, lSize);
                                }
                            }

                            this.Stop.SetProgressValue(lProgressValue + lHintNext);
                        }

                        if (lIndex % 100 == 0)
                        {
                            if (this.Stop != null && this.estimate != null)
                            {
                                estimate.Text = "剩余时间 " + ProgressEstimate.Format(estimate.Estimate(lProgressValue + lHintNext)) + " 已经过时间 " + ProgressEstimate.Format(estimate.delta_passed);
                                this.Stop.SetMessage("正在装入日志文件 " + this.FileName + " 中的记录 " + lIndex.ToString() + " 。"
                                                     + estimate.Text);
                            }
                        }

                        {
                            OperLogItem item = new OperLogItem();
                            item.Xml   = strXml;
                            item.Index = lIndex;
                            item.Date  = this.FileName.Substring(0, 8);
                            yield return(item);
                        }
                    }
                }

                // 创建本地缓存的日志文件的metadata文件
                if (bCacheFileExist == false && stream != null)
                {
                    string strFileName = this.FileName;
                    if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0)
                    {
                        strFileName = this.FileName + ".a";
                    }

                    nRet = CreateCacheMetadataFile(
                        this.CacheDir,
                        strFileName,    // this.FileName,
                        lServerFileSize,
                        out strError);
                    if (nRet == -1)
                    {
                        throw new Exception(strError);
                    }
                }

                bRemoveCacheFile = false;   // 不删除
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (bRemoveCacheFile == true)
                {
                    string strFileName = this.FileName;
                    if ((this.LogType & dp2Circulation.LogType.AccessLog) != 0)
                    {
                        strFileName = this.FileName + ".a";
                    }

                    string strError1 = "";
                    nRet = DeleteCacheFile(
                        this.CacheDir,
                        strFileName,    // this.FileName,
                        out strError1);
                    if (nRet == -1)
                    {
                        // MessageBox.Show(owner, strError1);
                        if (this.Prompt != null)
                        {
                            MessagePromptEventArgs e = new MessagePromptEventArgs();
                            e.MessageText = strError1;
                            e.Actions     = "ok";
                            this.Prompt(this, e);
                        }
                    }
                }
            }

            lProgressValue += lFileSize;
        }
Esempio n. 4
0
        public IEnumerator GetEnumerator()
        {
            string strError = "";
            int nRet = 0;

            long lRet = 0;

            this.Stop.SetMessage("正在装入日志文件 " + this.FileName + " 中的记录。"
                + "剩余时间 " + ProgressEstimate.Format(estimate.Estimate(lProgressValue)) + " 已经过时间 " + ProgressEstimate.Format(estimate.delta_passed));

            string strXml = "";
            long lAttachmentTotalLength = 0;
            byte[] attachment_data = null;

            long lFileSize = 0;
            if (lServerFileSize == -1)
            {
                long _lServerFileSize = 0;

                // 获得服务器端日志文件尺寸
                lRet = this.Channel.GetOperLog(
                    this.Stop,
                    this.FileName,
                    -1,    // lIndex,
                    -1, // lHint,
                        "level-" + nLevel.ToString(),
                        "", // strFilter
                    out strXml,
                    out _lServerFileSize,
                    0,  // lAttachmentFragmentStart,
                    0,  // nAttachmentFramengLength,
                    out attachment_data,
                    out lAttachmentTotalLength,
                    out strError);

                this.lServerFileSize = _lServerFileSize;

                if (lRet == 0)
                    yield break;
            }

            Stream stream = null;
            bool bCacheFileExist = false;
            bool bRemoveCacheFile = false;  // 是否要自动删除未全部完成的本地缓存文件

            bool bAutoCache = this.AutoCache;

            if (bAutoCache == true)
            {
                nRet = PrepareCacheFile(
                    this.CacheDir,
                    this.FileName,
                    lServerFileSize,
                    out bCacheFileExist,
                    out stream,
                    out strError);
                if (nRet == -1)
                    throw new Exception(strError);

                if (bCacheFileExist == false && stream != null)
                    bRemoveCacheFile = true;
            }

            try
            {
                if (bCacheFileExist == true)
                    lFileSize = stream.Length;
                else
                    lFileSize = lServerFileSize;

                // stop.SetProgressRange(0, lTotalSize);

                if (String.IsNullOrEmpty(Range) == true)
                    Range = "0-" + (long.MaxValue - 1).ToString();    // "0-9999999999";

                RangeList rl = new RangeList(Range);

                for (int i = 0; i < rl.Count; i++)
                {
                    RangeItem ri = (RangeItem)rl[i];

                    // 让 100- 这样的 range 可以使用
                    if (ri.lLength == -1)
                        ri.lLength = long.MaxValue - ri.lStart;

                    OperLogInfo[] records = null;
                    long lStartRecords = 0;

                    long lHint = -1;
                    long lHintNext = -1;
                    for (long lIndex = ri.lStart; lIndex < ri.lStart + ri.lLength; lIndex++)
                    {
                        Application.DoEvents();

                        if (this.Stop != null && this.Stop.State != 0)
                        {
                            strError = "用户中断";
                            throw new Exception(strError);
                            // yield break; ?
                        }


                        if (lIndex == ri.lStart)
                            lHint = -1;
                        else
                            lHint = lHintNext;

                        if (bCacheFileExist == true)
                        {
                            if (lHint == -1)
                            {
                                // return:
                                //      -1  error
                                //      0   成功
                                //      1   到达文件末尾或者超出
                                nRet = LocationRecord(stream,
                    lIndex,
                    out strError);
                                if (nRet == -1)
                                    throw new Exception(strError);
                            }
                            else
                            {
                                // 根据暗示找到
                                if (lHint == stream.Length)
                                    break;

                                if (lHint > stream.Length)
                                {
                                    strError = "lHint参数值不正确";
                                    throw new Exception(strError);
                                }
                                if (stream.Position != lHint)
                                    stream.Seek(lHint, SeekOrigin.Begin);
                            }

                            nRet = ReadCachedEnventLog(
                                stream,
                                out strXml,
                                out lAttachmentTotalLength,
                                out strError);
                            if (nRet == -1)
                                throw new Exception(strError);
                            lHintNext = stream.Position;

                        }
                        else
                        {
                            if (records == null || lIndex /*- ri.lStart*/ >= lStartRecords + records.Length)
                            {
                                int nCount = -1;
                                if (ri.lLength >= Int32.MaxValue)
                                    nCount = -1;    // 500;   // -1;
                                else
                                    nCount = (int)ri.lLength;   // Math.Min(500, (int)ri.lLength);

                            REDO:
                                // 获得日志
                                // return:
                                //      -1  error
                                //      0   file not found
                                //      1   succeed
                                //      2   超过范围,本次调用无效
                                lRet = this.Channel.GetOperLogs(
                                    this.Stop,
                                    this.FileName,
                                    lIndex,
                                    lHint,
                                    nCount,
                                    "level-" + nLevel.ToString(),
                                    this.Filter, // strFilter
                                    out records,
                                    out strError);
                                if (lRet == -1)
                                {
#if NO
                                    DialogResult result = MessageBox.Show(owner,
    "获取日志信息 ("+this.FileName + " " +lIndex.ToString() + ") 的操作发生错误: " + strError + "\r\n\r\n是否重试操作?\r\n\r\n(是: 重试;  否: 跳过本次操作,继续后面的操作; 放弃: 停止全部操作)",
    "OperLogItemLoader",
    MessageBoxButtons.YesNoCancel,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button1);
                                    if (result == DialogResult.Yes)
                                        goto REDO;
                                    if (result == DialogResult.Cancel)
                                        throw new Exception(strError);
                                    else
                                    {
                                        // TODO: 是否要在listview中装入一条表示出错的行?
                                        lHintNext = -1;
                                        continue;
                                    }
#endif
                                    if (this.Prompt != null)
                                    {
                                        MessagePromptEventArgs e = new MessagePromptEventArgs();
                                        e.MessageText = "获取日志信息 ("+this.FileName + " " +lIndex.ToString() + ") 的操作发生错误: " + strError;
                                        e.Actions = "yes,no,cancel";
                                        this.Prompt(this, e);
                                        if (e.ResultAction == "cancel")
                                            throw new Exception(strError);
                                        else if (e.ResultAction == "yes")
                                        {
                                            if (this.Stop != null)
                                                this.Stop.Continue();
                                            goto REDO;
                                        }
                                        else
                                        {
                                            lHintNext = -1;
                                            continue;
                                        }
                                    }
                                    else
                                        throw new Exception(strError);
                                }
                                if (lRet == 0)
                                    yield break;

                                if (lRet == 2)
                                    break;

                                // records数组表示的起点位置
                                lStartRecords = lIndex /* - ri.lStart*/;
                            }
                            OperLogInfo info = records[lIndex - lStartRecords];

                            strXml = info.Xml;
                            lHintNext = info.HintNext;
                            lAttachmentTotalLength = info.AttachmentLength;

                            // 写入本地缓存的日志文件
                            if (stream != null)
                            {
                                try
                                {
                                    WriteCachedEnventLog(
                                        stream,
                                        strXml,
                                        lAttachmentTotalLength);
                                }
                                catch (Exception ex)
                                {
                                    strError = "写入本地缓存文件的时候出错: " + ex.Message;
                                    throw new Exception(strError);
                                }
                            }
                        }

#if NO
                            // 2011/12/30
                            // 日志记录可能动态地增加了,超过了原先为ProgressBar设置的范围
                            if (lFizeTotalSize < (int)lHintNext)
                            {
                                lFizeTotalSize = lHintNext;

                                stop.SetProgressRange(0, lFizeTotalSize);
                            }
#endif
                        if (lHintNext >= 0)
                        {
                            // 校正
                            if (lProgressValue + lHintNext > lSize)
                            {
                                lSize = lProgressValue + lHintNext;

                                this.Stop.SetProgressRange(0, lSize);
                                estimate.SetRange(0, lSize);
                            }

                            this.Stop.SetProgressValue(lProgressValue + lHintNext);
                        }

                        if (lIndex % 100 == 0)
                        {
                            estimate.Text = "剩余时间 " + ProgressEstimate.Format(estimate.Estimate(lProgressValue + lHintNext)) + " 已经过时间 " + ProgressEstimate.Format(estimate.delta_passed);
                            this.Stop.SetMessage("正在装入日志文件 " + this.FileName + " 中的记录 " + lIndex.ToString() + " 。"
                                + estimate.Text);
                        }

                        {
                            OperLogItem item = new OperLogItem();
                            item.Xml = strXml;
                            item.Index = lIndex;
                            item.Date = this.FileName.Substring(0, 8);
                            yield return item;
                        }

                    }
                }

                // 创建本地缓存的日志文件的metadata文件
                if (bCacheFileExist == false && stream != null)
                {
                    nRet = CreateCacheMetadataFile(
                        this.CacheDir,
                        this.FileName,
                        lServerFileSize,
                        out strError);
                    if (nRet == -1)
                        throw new Exception(strError);
                }

                bRemoveCacheFile = false;   // 不删除
            }
            finally
            {
                if (stream != null)
                    stream.Close();

                if (bRemoveCacheFile == true)
                {
                    string strError1 = "";
                    nRet = DeleteCacheFile(
                        this.CacheDir,
                        this.FileName,
                        out strError1);
                    if (nRet == -1)
                    {
                        // MessageBox.Show(owner, strError1);
                        if (this.Prompt != null)
                        {
                            MessagePromptEventArgs e = new MessagePromptEventArgs();
                            e.MessageText = strError1;
                            e.Actions = "ok";
                            this.Prompt(this, e);
                        }
                    }
                }
            }

            lProgressValue += lFileSize;
        }