예제 #1
0
        private static LogItem CreateEntry(string[] items, MatchCollection matches)
        {
            if (items == null) 
                throw new ArgumentNullException("items");
            if (matches == null) 
                throw new ArgumentNullException("matches");

            if (items.Length != matches.Count)
                throw new NotValidValueException("different length of items/matches values");

            LogItem entry = new LogItem();
            for (int i = 0; i < matches.Count; i++)
            {
                string value = items[i];
                Match match = matches[i];
                string name = match.Value;
                switch (name)
                {
                    case "%date":
                        entry.TimeStamp = DateTime.ParseExact(
                            value, DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
                        break;

                    case "%message":
                        entry.Message = value;
                        break;

                    case "%level":
                        entry.Level = value;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(name, "unmanaged value");
                }
            }
            return entry;
        }
예제 #2
0
        private IEnumerable<LogItem> InternalGetEntries(string dataSource, FilterParams filter)
        {
            using (IDbConnection connection = this.CreateConnection(dataSource))
            {
                connection.Open();
                using (IDbTransaction transaction = connection.BeginTransaction())
                {                    
                    using (IDbCommand command = connection.CreateCommand())
                    {
                        command.CommandText =
                            @"select caller, date, level, logger, thread, message, exception from log where date >= @date";

                        IDbDataParameter parameter = command.CreateParameter();
                        parameter.ParameterName = "@date";
                        parameter.Value = filter.Date.HasValue ? filter.Date.Value : MinDateTime;
                        command.Parameters.Add(parameter);

                        switch (filter.Level)
                        {
                            case 1:
                                AddLevelClause(command, "ERROR");
                                break;

                            case 2:
                                AddLevelClause(command, "INFO");
                                break;

                            case 3:
                                AddLevelClause(command, "DEBUG");
                                break;

                            case 4:
                                AddLevelClause(command, "WARN");
                                break;

                            case 5:
                                AddLevelClause(command, "FATAL");
                                break;

                            default:
                                break;
                        }

                        AddLoggerClause(command, filter.Logger);
                        AddThreadClause(command, filter.Thread);
                        AddMessageClause(command, filter.Message);

                        AddOrderByClause(command);

                        using (IDataReader reader = command.ExecuteReader())
                        {
                            int index = 0;
                            while (reader.Read())
                            {
                                string caller = reader.GetString(0);
                                string[] split = caller.Split(',');

                                const string machineKey = "{log4jmachinename=";
                                string item0 = Find(split, machineKey);
                                string machineName = GetValue(item0, machineKey);

                                const string hostKey = " log4net:HostName=";
                                string item1 = Find(split, hostKey);
                                string hostName = GetValue(item1, hostKey);

                                const string userKey = " log4net:UserName="******" log4japp=";
                                string item3 = Find(split, appKey);
                                string app = GetValue(item3, appKey);

                                DateTime timeStamp = reader.GetDateTime(1);
                                string level = reader.GetString(2);
                                string logger = reader.GetString(3);
                                string thread = reader.GetString(4);
                                string message = reader.GetString(5);
                                string exception = reader.GetString(6);

                                LogItem entry = new LogItem
                                {
                                    Id = ++index,
                                    TimeStamp = timeStamp,
                                    Level = level,
                                    Thread = thread,
                                    Logger = logger,
                                    Message = message,
                                    Throwable = exception,
                                    MachineName = machineName,
                                    HostName = hostName,
                                    UserName = userName,
                                    App = app,
                                };
                                // TODO: altri filtri
                                yield return entry;
                            }
                        }
                    }
                    transaction.Commit();
                }
            }
        }
예제 #3
0
        private static bool filterByParameters(LogItem entry, FilterParams parameters)
        {
            if (entry == null)
                throw new ArgumentNullException("entry");
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            bool accept = false;
            switch (parameters.Level)
            {
                case 1:
                    if (String.Equals(entry.Level, "ERROR",
                        StringComparison.InvariantCultureIgnoreCase))
                        accept = true;
                    break;

                case 2:
                    if (String.Equals(entry.Level, "INFO",
                        StringComparison.InvariantCultureIgnoreCase))
                        accept = true;
                    break;

                case 3:
                    if (String.Equals(entry.Level, "DEBUG",
                        StringComparison.InvariantCultureIgnoreCase))
                        accept = true;
                    break;

                case 4:
                    if (String.Equals(entry.Level, "WARN",
                        StringComparison.InvariantCultureIgnoreCase))
                        accept = true;
                    break;

                case 5:
                    if (String.Equals(entry.Level, "FATAL",
                        StringComparison.InvariantCultureIgnoreCase))
                        accept = true;
                    break;

                default:
                    accept = true;
                    break;
            }

            if (parameters.Date.HasValue)
                if (entry.TimeStamp < parameters.Date)
                    accept = false;

            if (!String.IsNullOrEmpty(parameters.Thread))
                if (!String.Equals(entry.Thread, parameters.Thread, StringComparison.InvariantCultureIgnoreCase))
                    accept = false;

            if (!String.IsNullOrEmpty(parameters.Message))
                if (!entry.Message.ToUpper().Contains(parameters.Message.ToUpper()))
                    accept = false;

            if (!String.IsNullOrEmpty(parameters.Logger))
                if (!entry.Logger.ToUpper().Contains(parameters.Logger.ToUpper()))
                    accept = false;

            return accept;
        }
예제 #4
0
        public override IEnumerable<LogItem> GetEntries(string dataSource, FilterParams filter)
        {
            var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
            var nt = new NameTable();
            var mgr = new XmlNamespaceManager(nt);
            mgr.AddNamespace("log4j", Constants.LAYOUT_LOG4J);
            var pc = new XmlParserContext(nt, mgr, string.Empty, XmlSpace.Default);
            var date = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            using (var stream = new FileStream(dataSource, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = new StreamReader(stream, System.Text.Encoding.Default, true))
                {
                    using (var xmlTextReader = XmlReader.Create(reader, settings, pc))
                    {
                        var entryId = 1;
                        DateTime? prevTimeStamp = null;
                        while (xmlTextReader.Read())
                        {
                            if ((xmlTextReader.NodeType != XmlNodeType.Element) || (xmlTextReader.Name != "log4j:event"))
                                continue;

                            var entry = new LogItem { Id = entryId, Path = dataSource };

                            entry.Logger = xmlTextReader.GetAttribute("logger");

                            entry.TimeStamp = date.AddMilliseconds(Convert.ToDouble(xmlTextReader.GetAttribute("timestamp"))).ToLocalTime();
                            if (prevTimeStamp.HasValue)
                                entry.Delta = (entry.TimeStamp - prevTimeStamp.Value).TotalSeconds;
                            prevTimeStamp = entry.TimeStamp;

                            entry.Level = xmlTextReader.GetAttribute("level");
                            entry.Thread = xmlTextReader.GetAttribute("thread");

                            while (xmlTextReader.Read())
                            {
                                var breakLoop = false;
                                switch (xmlTextReader.Name)
                                {
                                    case "log4j:event":
                                        breakLoop = true;
                                        break;
                                    default:
                                        switch (xmlTextReader.Name)
                                        {
                                            case ("log4j:message"):
                                                entry.Message = xmlTextReader.ReadString();
                                                break;
                                            case ("log4j:data"):
                                                switch (xmlTextReader.GetAttribute("name"))
                                                {
                                                    case ("log4net:UserName"):
                                                        entry.UserName = xmlTextReader.GetAttribute("value");
                                                        break;
                                                    case ("log4japp"):
                                                        entry.App = xmlTextReader.GetAttribute("value");
                                                        break;
                                                    case ("log4jmachinename"):
                                                        entry.MachineName = xmlTextReader.GetAttribute("value");
                                                        break;
                                                    case ("log4net:HostName"):
                                                        entry.HostName = xmlTextReader.GetAttribute("value");
                                                        break;
                                                }
                                                break;
                                            case ("log4j:throwable"):
                                                entry.Throwable = xmlTextReader.ReadString();
                                                break;
                                            case ("log4j:locationInfo"):
                                                entry.Class = xmlTextReader.GetAttribute("class");
                                                entry.Method = xmlTextReader.GetAttribute("method");
                                                entry.File = xmlTextReader.GetAttribute("file");
                                                entry.Line = xmlTextReader.GetAttribute("line");
                                                break;
                                        }
                                        break;
                                }
                                if (breakLoop) break;
                            }

                            if (filterByParameters(entry, filter))
                            {
                                yield return entry;
                                entryId++;
                            }
                        }
                    }
                }
            }
        }