コード例 #1
0
        private void LoadFile()
        {
            textboxFileName.Text = FileName;
            _Entries.Clear();
            listView1.ItemsSource = null;

            DateTime dt      = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string   sXml    = string.Empty;
            string   sBuffer = string.Empty;
            int      iIndex  = 1;

            Clear();

            try
            {
                FileStream   oFileStream   = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
                StreamReader oStreamReader = new StreamReader(oFileStream);
                sBuffer = string.Format("<root>{0}</root>", oStreamReader.ReadToEnd());
                oStreamReader.Close();
                oFileStream.Close();

                #region Read File Buffer
                ////////////////////////////////////////////////////////////////////////////////
                StringReader  oStringReader  = new StringReader(sBuffer);
                XmlTextReader oXmlTextReader = new XmlTextReader(oStringReader);
                oXmlTextReader.Namespaces = false;
                while (oXmlTextReader.Read())
                {
                    if ((oXmlTextReader.NodeType == XmlNodeType.Element) && (oXmlTextReader.Name == "log4j:event"))
                    {
                        LogEntry logentry = new LogEntry();

                        logentry.Item = iIndex;

                        double dSeconds = Convert.ToDouble(oXmlTextReader.GetAttribute("timestamp"));
                        logentry.TimeStamp = dt.AddMilliseconds(dSeconds).ToLocalTime();
                        logentry.Thread    = oXmlTextReader.GetAttribute("thread");

                        #region get level
                        ////////////////////////////////////////////////////////////////////////////////
                        logentry.Level = oXmlTextReader.GetAttribute("level");
                        switch (logentry.Level)
                        {
                        case "ERROR":
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.ERROR);
                            break;
                        }

                        case "INFO":
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.INFO);
                            break;
                        }

                        case "DEBUG":
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.DEBUG);
                            break;
                        }

                        case "WARN":
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.WARN);
                            break;
                        }

                        case "FATAL":
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.FATAL);
                            break;
                        }

                        default:
                        {
                            logentry.Image = LogEntry.Images(LogEntry.IMAGE_TYPE.CUSTOM);
                            break;
                        }
                        }
                        ////////////////////////////////////////////////////////////////////////////////
                        #endregion

                        #region read xml
                        ////////////////////////////////////////////////////////////////////////////////
                        while (oXmlTextReader.Read())
                        {
                            if (oXmlTextReader.Name == "log4j:event")   // end element
                            {
                                break;
                            }
                            else
                            {
                                switch (oXmlTextReader.Name)
                                {
                                case ("log4j:message"):
                                {
                                    logentry.Message = oXmlTextReader.ReadString();
                                    break;
                                }

                                case ("log4j:data"):
                                {
                                    switch (oXmlTextReader.GetAttribute("name"))
                                    {
                                    case ("log4jmachinename"):
                                    {
                                        logentry.MachineName = oXmlTextReader.GetAttribute("value");
                                        break;
                                    }

                                    case ("log4net:HostName"):
                                    {
                                        logentry.HostName = oXmlTextReader.GetAttribute("value");
                                        break;
                                    }

                                    case ("log4net:UserName"):
                                    {
                                        logentry.UserName = oXmlTextReader.GetAttribute("value");
                                        break;
                                    }

                                    case ("log4japp"):
                                    {
                                        logentry.App = oXmlTextReader.GetAttribute("value");
                                        break;
                                    }
                                    }
                                    break;
                                }

                                case ("log4j:throwable"):
                                {
                                    logentry.Throwable = oXmlTextReader.ReadString();
                                    break;
                                }

                                case ("log4j:locationInfo"):
                                {
                                    logentry.Class  = oXmlTextReader.GetAttribute("class");
                                    logentry.Method = oXmlTextReader.GetAttribute("method");
                                    logentry.File   = oXmlTextReader.GetAttribute("file");
                                    logentry.Line   = oXmlTextReader.GetAttribute("line");
                                    break;
                                }
                                }
                            }
                        }
                        ////////////////////////////////////////////////////////////////////////////////
                        #endregion

                        _Entries.Add(logentry);
                        iIndex++;

                        #region Show Counts
                        ////////////////////////////////////////////////////////////////////////////////
                        int ErrorCount =
                            (
                                from entry in Entries
                                where entry.Level == "ERROR"
                                select entry
                            ).Count();

                        if (ErrorCount > 0)
                        {
                            labelErrorCount.Content    = string.Format("{0:#,#}  ", ErrorCount);
                            labelErrorCount.Visibility = Visibility.Visible;
                            imageError.Visibility      = Visibility.Visible;
                        }
                        else
                        {
                            labelErrorCount.Visibility = Visibility.Hidden;
                            imageError.Visibility      = Visibility.Hidden;
                        }

                        int InfoCount =
                            (
                                from entry in Entries
                                where entry.Level == "INFO"
                                select entry
                            ).Count();

                        if (InfoCount > 0)
                        {
                            labelInfoCount.Content    = string.Format("{0:#,#}  ", InfoCount);
                            labelInfoCount.Visibility = Visibility.Visible;
                            imageInfo.Visibility      = Visibility.Visible;
                        }
                        else
                        {
                            labelInfoCount.Visibility = Visibility.Hidden;
                            imageInfo.Visibility      = Visibility.Hidden;
                        }

                        int WarnCount =
                            (
                                from entry in Entries
                                where entry.Level == "WARN"
                                select entry
                            ).Count();

                        if (WarnCount > 0)
                        {
                            labelWarnCount.Content    = string.Format("{0:#,#}  ", WarnCount);
                            labelWarnCount.Visibility = Visibility.Visible;
                            imageWarn.Visibility      = Visibility.Visible;
                        }
                        else
                        {
                            labelWarnCount.Visibility = Visibility.Hidden;
                            imageWarn.Visibility      = Visibility.Hidden;
                        }

                        int DebugCount =
                            (
                                from entry in Entries
                                where entry.Level == "DEBUG"
                                select entry
                            ).Count();

                        if (DebugCount > 0)
                        {
                            labelDebugCount.Content    = string.Format("{0:#,#}  ", DebugCount);
                            labelDebugCount.Visibility = Visibility.Visible;
                            imageDebug.Visibility      = Visibility.Visible;
                        }
                        else
                        {
                            labelDebugCount.Visibility = Visibility.Hidden;
                            labelDebugCount.Visibility = Visibility.Hidden;
                        }
                        ////////////////////////////////////////////////////////////////////////////////
                        #endregion
                    }
                }
                ////////////////////////////////////////////////////////////////////////////////
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            this.listView1.ItemsSource = _Entries;
        }
コード例 #2
0
        /// <summary>
        /// Loads the file.
        /// </summary>
        public void loadFile(string logFileName, bool withMerge = false)
        {
            if (!withMerge)
            {
                log.Info("Clearing entries to load single log file");
                entries.Clear();
                log.Info("Notifying Entries property as changed");
                notifyPropertyChanged("Entries");
                log.Info("Resetting the ListView item source to nothing");
                listView1.ItemsSource = null;
                FileName = logFileName;
            }
            else
            {
                log.Info("Adding the log files that need to be merged to mergedFile object");
                if (mergedFiles.Count == 0)
                {
                    mergedFiles.Add(FileName);
                }
                log.Info("If the same file is being added then return immediately");
                if (mergedFiles.Contains(logFileName))
                {
                    return;
                }
                log.Info("If not add the file to the merged list");
                mergedFiles.Add(logFileName);
            }

            log.Info("Clearing the log filter");
            logFilter.Clear();
            log.Info("Turning off the IsFiltered property of the FilterIndicator");
            FilterIndicator.IsFiltered = false;

            var dt     = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var iIndex = 1;

            if (withMerge)
            {
                iIndex = entries.Count + 1;
            }

            clear();

            try
            {
                log.Info("Initializing FileStream objet to open the log file");
                var oFileStream = new FileStream(logFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
                log.Info("Initializing a Stream Reader");
                var oStreamReader = new StreamReader(oFileStream);
                log.Info("Read all the contents in the log file to a StreamReader");
                var sBuffer = string.Format("<root>{0}</root>", oStreamReader.ReadToEnd());
                log.Info("Closing StreamReader and FileStream object");
                oStreamReader.Close();
                oFileStream.Close();

                #region Read File Buffer
                ////////////////////////////////////////////////////////////////////////////////
                log.Info("Reading File");
                var oStringReader  = new StringReader(sBuffer);
                var oXmlTextReader = new XmlTextReader(oStringReader)
                {
                    Namespaces = false
                };
                log.Info("Start reading the log file");
                while (oXmlTextReader.Read())
                {
                    if ((oXmlTextReader.NodeType != XmlNodeType.Element) || (oXmlTextReader.Name != "log4j:event"))
                    {
                        continue;
                    }
                    var logentry = new LogEntry {
                        Item = iIndex
                    };

// ReSharper disable StringLiteralsWordIsNotInDictionary
                    var dSeconds = Convert.ToDouble(oXmlTextReader.GetAttribute("timestamp"));
// ReSharper restore StringLiteralsWordIsNotInDictionary
                    logentry.TimeStamp = dt.AddMilliseconds(dSeconds).ToLocalTime();
                    logentry.Thread    = oXmlTextReader.GetAttribute("thread");
                    logentry.Logger    = oXmlTextReader.GetAttribute("logger");
                    logentry.LogFile   = logFileName;

                    #region get level
                    ////////////////////////////////////////////////////////////////////////////////
                    logentry.Level = oXmlTextReader.GetAttribute("level");
                    switch (logentry.Level)
                    {
                    case "ERROR":
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Error);
                        break;
                    }

                    case "INFO":
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Info);
                        break;
                    }

                    case "DEBUG":
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Debug);
                        break;
                    }

                    case "WARN":
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Warn);
                        break;
                    }

                    case "FATAL":
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Fatal);
                        break;
                    }

                    default:
                    {
                        logentry.Image = LogEntry.Images(LogImageType.Custom);
                        break;
                    }
                    }
                    ////////////////////////////////////////////////////////////////////////////////
                    #endregion

                    #region read xml
                    ////////////////////////////////////////////////////////////////////////////////
                    while (oXmlTextReader.Read())
                    {
                        var breakLoop = false;
                        switch (oXmlTextReader.Name)
                        {
                        case "log4j:event":
                            breakLoop = true;
                            break;

                        default:
                            switch (oXmlTextReader.Name)
                            {
                            case ("log4j:message"):
                            {
                                logentry.Message = oXmlTextReader.ReadString();
                                break;
                            }

                            case ("log4j:data"):
                            {
                                switch (oXmlTextReader.GetAttribute("name"))
                                {
                                case ("log4jmachinename"):
                                {
                                    logentry.MachineName = oXmlTextReader.GetAttribute("value");
                                    break;
                                }

                                case ("log4net:HostName"):
                                {
                                    logentry.HostName = oXmlTextReader.GetAttribute("value");
                                    break;
                                }

                                case ("log4net:UserName"):
                                {
                                    logentry.UserName = oXmlTextReader.GetAttribute("value");
                                    break;
                                }

                                case ("log4net:Identity"):
                                {
                                    logentry.Identity = oXmlTextReader.GetAttribute("value");
                                    break;
                                }

                                case ("NDC"):
                                {
                                    logentry.NDC = oXmlTextReader.GetAttribute("value");
                                    break;
                                }

                                case ("log4japp"):
                                {
                                    logentry.App = oXmlTextReader.GetAttribute("value");
                                    break;
                                }
                                }
                                break;
                            }

// ReSharper disable StringLiteralsWordIsNotInDictionary
                            case ("log4j:throwable"):
// ReSharper restore StringLiteralsWordIsNotInDictionary
                            {
                                logentry.Throwable = oXmlTextReader.ReadString();
                                break;
                            }

                            case ("log4j:locationInfo"):
                            {
                                logentry.Class  = oXmlTextReader.GetAttribute("class");
                                logentry.Method = oXmlTextReader.GetAttribute("method");
                                logentry.File   = oXmlTextReader.GetAttribute("file");
                                logentry.Line   = oXmlTextReader.GetAttribute("line");
                                break;
                            }
                            }
                            break;
                        }
                        if (breakLoop)
                        {
                            break;
                        }
                    }
                    ////////////////////////////////////////////////////////////////////////////////
                    #endregion

                    entries.Add(logentry);
                    iIndex++;
                }
                notifyPropertyChanged("Entries");
                log.Info("Completing of Log xml reading");
                ////////////////////////////////////////////////////////////////////////////////
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            #region Show Counts
            ////////////////////////////////////////////////////////////////////////////////
            log.Info("Calculating the number of ERROR in the log file");
            var errorCount =
                (
                    from entry in Entries
                    where entry.Level == "ERROR"
                    select entry
                ).Count();

            if (errorCount > 0)
            {
                labelErrorCount.Text       = string.Format("{0:#,#}  ", errorCount);
                labelErrorCount.Visibility = Visibility.Visible;
                imageError.Visibility      = Visibility.Visible;
            }
            else
            {
                labelErrorCount.Visibility = Visibility.Hidden;
                imageError.Visibility      = Visibility.Hidden;
            }
            log.Info("ERROR count is " + errorCount);
            log.Info("Calculating the number of INFO in the log file");
            var infoCount =
                (
                    from entry in Entries
                    where entry.Level == "INFO"
                    select entry
                ).Count();

            if (infoCount > 0)
            {
                labelInfoCount.Text       = string.Format("{0:#,#}  ", infoCount);
                labelInfoCount.Visibility = Visibility.Visible;
                imageInfo.Visibility      = Visibility.Visible;
            }
            else
            {
                labelInfoCount.Visibility = Visibility.Hidden;
                imageInfo.Visibility      = Visibility.Hidden;
            }
            log.Info("INFO count is " + infoCount);
            log.Info("Calculating the number of WARN in the log file");
            var warnCount =
                (
                    from entry in Entries
                    where entry.Level == "WARN"
                    select entry
                ).Count();

            if (warnCount > 0)
            {
                labelWarnCount.Text       = string.Format("{0:#,#}  ", warnCount);
                labelWarnCount.Visibility = Visibility.Visible;
                imageWarn.Visibility      = Visibility.Visible;
            }
            else
            {
                labelWarnCount.Visibility = Visibility.Hidden;
                imageWarn.Visibility      = Visibility.Hidden;
            }
            log.Info("WARN count is " + warnCount);
            log.Info("Calculating the number of DEBUG in the log file");
            var debugCount =
                (
                    from entry in Entries
                    where entry.Level == "DEBUG"
                    select entry
                ).Count();

            if (debugCount > 0)
            {
                labelDebugCount.Text       = string.Format("{0:#,#}  ", debugCount);
                labelDebugCount.Visibility = Visibility.Visible;
                imageDebug.Visibility      = Visibility.Visible;
            }
            else
            {
                imageDebug.Visibility      = Visibility.Hidden;
                labelDebugCount.Visibility = Visibility.Hidden;
            }
            log.Info("DEBUG count is " + debugCount);
            tbFiltered.Text            = Entries.Count().ToString();
            FilterIndicator.IsFiltered = false;

            ////////////////////////////////////////////////////////////////////////////////
            #endregion
            log.Info("Initializing ListView to show the log entries");
            listView1.ItemsSource = null;
            log.Info("Loading ListView with Log Entries");
            listView1.ItemsSource = (from e in entries orderby e.TimeStamp select e).ToList();
            log.Info("Clearing Sort Adorner");
            clearSortAdorner();

            if (!withMerge)
            {
                textboxFileName.Text = logFileName;
                Title = string.Format(Properties.Resources.WindowTitle + " - " + logFileName, Assembly.GetExecutingAssembly().GetName().Version);
                log.Info("Setting the title as " + Title);
                return;
            }

            var s = "";
            foreach (var sm in mergedFiles)
            {
                if (s != "")
                {
                    s += "; ";
                }
                s += Path.GetFileName(sm);
            }

            textboxFileName.Text = s;
            Title = string.Format(Properties.Resources.WindowTitle + " - " + s, Assembly.GetExecutingAssembly().GetName().Version);
            log.Info("Setting the title as " + Title);
        }