进入/退出方法时如何增加/减少缩进? [英] How to increase/decrease indenting when stepping in/out of methods?

查看:137
本文介绍了进入/退出方法时如何增加/减少缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR-跳至最后一段

背景

我正在执行一些数据驱动的测试,并将日志文件用作测试输出之一.它的工作原理是这样的-

  • 读取文件夹中的第一个文件
    • 处理第一行并转换为测试
    • 运行测试
      • 执行验证1
      • ...
    • ...
  • 阅读下一个文件
  • 等等.

我的日志文件反映了这一点:

INFO - Start RunAllFilesInFolder  
INFO - File1:
INFO -      Some info
INFO -      Executing Test 1  
INFO -          Validation A result
INFO -          ...
INFO -      ...  
INFO - File2:  
...

此刻,我像这样使用/调用Log 4网络-

static class LogHelper
{
    internal static readonly log4net.ILog Log = log4net.LogManager.GetLogger
            (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}

// another file
using static Some.Namespace.LogHelper;
class SomeClass
{
     Log.Info($"\t\tExecuting {t.Number}-{t.Name}");
}

缩进="\ t","\ t \ t"或"\ t \ t \ t",具体取决于测试所处的级别.


是否有一种方法可以重构对LogHelper.Log的调用,以便它考虑到一个静态的缩进",当我进入不同的测试级别时,可以在不显式指定每次调用的缩进的情况下增加/减少该静态缩进"? /p>

即 可以在适当的地方拨打电话

Log.Indent.Increase();

Log.Indent.Decrease();

并将上述调用替换为-

Log.Info($"Executing {t.Number}-{t.Name}");

解决方案

您可以使用

Where indent = "\t", "\t\t" or "\t\t\t" depending on the level the test is in.


Is there a way to refactor the call to LogHelper.Log so that it takes into account a static "indent" that can be increased/decreased as I step into the different test levels without explicitly specifying the indent in each call?

I.e. Be able to call where applicable something like

Log.Indent.Increase();

Or

Log.Indent.Decrease();

And replace the above call to log simply with -

Log.Info($"Executing {t.Number}-{t.Name}");

解决方案

You could use the stack trace length, by counting the number of new lines:

int someSensibleMinimum = 3; //something that works for you
int count = Environment.StackTrace.Count(a => a=='\n');
var indent = new string('\t', Math.Max(0, count - someSensibleMinimum));

Note that in Release it may behave differently:

However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.

Or, you could automagically calculate the length using this approach (pseudocode):

int count = Environment.StackTrace.Count(a => a=='\n');
Look for stack length in dictionary<int,string> (length to indent string)
If found use it.
If not found, find the largest entry in dictionary where key < count
add new entry to dictionary one tab char longer

In code:

public sealed class IndentTracker
{
    private readonly ThreadLocal<Dictionary<int, string>> _dictionaryLocal =
        new ThreadLocal<Dictionary<int, string>>(() => new Dictionary<int, string>());

    public string GetIndent()
    {
        Dictionary<int, string> dictionary = _dictionaryLocal.Value;

        int count = Environment.StackTrace.Count(a => a == '\n');

        if (!dictionary.Any())
        {
            string initialIndent = string.Empty;
            dictionary.Add(count, initialIndent);
            return initialIndent;
        }

        string indent;
        if (dictionary.TryGetValue(count, out indent))
            return indent;

        string last = dictionary.OrderByDescending(k => k.Key).First(k => k.Key < count).Value;
        string newIndent = last + '\t';
        dictionary.Add(count, newIndent);
        return newIndent;
    }
}

This works when log in increasing depth order. For example, it fails if you log at stack depth 10, then 5 (without previously logging at stack depth 5 then 10).

这篇关于进入/退出方法时如何增加/减少缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆