问题描述
我正在做一个项目,我必须更改某些变量值以进行调试。在发布版本中,某些操作之间有很长的 TimeSpan。对于调试,我会更改 TimeSpans 的值,以免等待太久。示例:
//For Release:
//TimeSpan intervall1 = new TimeSpan.FromMinutes(90);
//For Debug:
TimeSpan intervall1 = new TimeSpan.FromSeconds(10);
我有很多这样的情况,所以很容易忘记撤消发布的更改。
有没有办法跟踪这样的变化?或者在这种情况下有更好的调试方法吗?
解决方法
通常,这些类型的值将由一些可能会有所不同的外部配置提供,或者通过传递给相关方法,或者通过从例如因环境而异的配置文件中读取,因此您在'正在运行您的测试,文件 /config/test.json
已加载,但在生产中文件 /config/prod.json
已加载。
因为生命太短暂,不能以艰难的方式去做。
,一个简单的方法是使用 #if preprocessor directive
检查是否处于调试模式。
这是演示。
#if DEBUG
Console.WriteLine("Mode=Debug");
TimeSpan intervall1 = new TimeSpan.FromSeconds(10);
#else
Console.WriteLine("Mode=Release");
TimeSpan intervall1 = new TimeSpan.FromMinutes(90);
#endif