在日志中查找随时间推移的“真实”变化

时间:2014-01-30 00:09:06

标签: c# linq generics

我经常处理包含各种字段的原始日志文件以及更改它们的DateTime。我想要一种通用的方法来获取这些日志和列的指定子集,只保留与其中一个指定列中的“实际”更改相关联的条目。我希望这通常接受所选列的许多不同类类型的组合。

1)有内置/更好的方法吗?

2)如果没有,我可以获得有关我提议的方法的反馈吗?

e.g。我想用它的方式是:

IEnumberable<SomeObjectWithManyProperties> rawLogs = getRawData();

var rowsToKeep = rawLogs.Select(x => new Tuple<int, int>(x.a, x.b))
                        .FindChanges();

这是我的代码:

public class TimeStampedLog<T> 
{
    public DateTime TimeStamp;
    public T Log;

    public TimeStampedLog(DateTime timeStamp, T log)
    {
        TimeStamp = timeStamp;
        Log = log;
    } 
}

public static class Extensions
{
    /// <summary>
    /// Takes a list of objects with an associated Time for each object. 
    /// Returns the list of objects which reflect changes in the value
    /// of the object compared to the TimeStampedLog immediately prior to
    /// it in time.
    /// </summary>
    public static List<TimeStampedLog<T>> 
         FindChanges<T>(this IEnumerable<TimeStampedLog<T>> input)
    {
        var outputList = new List<TimeStampedLog<T>>();

        T prevEntry = default(T);

        // Sort the entries by their dates (ascending order
        foreach (var entry in input.OrderBy(x => x.TimeStamp))
        {
            //if (!EqualityComparer<T>.Default.Equals(entry.Log, prevEntry))
            if (!entry.Log.Equals(prevEntry))
            {
                outputList.Add(entry);
            }
            prevEntry = entry.Log;
        }

        return outputList;
    }
}

0 个答案:

没有答案