iflight / Logging.Memory

Logging.Memory allows write log to memory
MIT License
9 stars 8 forks source link

Clear Logs #7

Open ebuljan opened 3 years ago

ebuljan commented 3 years ago

I don't see a public way to clear the log dictionary. I also don't have access to do a pull request. Can this be added?

    /// <summary>
    /// Public method to clear logsDictionary and reinitialize
    /// </summary>
    public static void ClearLogs()
    {
        logsDictionary.Clear();
        foreach (var level in ((LogLevel[])Enum.GetValues(typeof(LogLevel))).Where(x => x != LogLevel.None))
        {
            logsDictionary.Add(level, new LogForLevel(MaxLogCount));
        }
    }
SergeyBarskiyTyler commented 3 years ago

Workaround: public static class MemoryLogHelper { public static void ClearLogs() { var field = typeof(MemoryLogger).GetField("logsDictionary", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance); Debug.Assert(field != null, nameof(field) + " != null"); var logs = field.GetValue(null) as System.Collections.IDictionary; Debug.Assert(logs != null, nameof(logs) + " != null"); foreach (var value in logs.Values) { Debug.Assert(value != null, nameof(value) + " != null"); var logData = value.GetType().GetField("logList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) ?.GetValue(value) as List<Tuple<DateTime, string>>; Debug.Assert(logData != null, nameof(logData) + " != null"); logData.Clear(); } } }