我有一些使用NLog的代码,我喜欢在我的单元测试中捕获这个输出,所以我可以看到发生了什么。我使用MSpec作为我的测试框架,使用NCrunch作为测试运行器。我为每个测试程序集配置一次,例如:
public class LogSetup : IAssemblyContext
{
static Logger log;
public void OnAssemblyStart()
{
var configuration = new LoggingConfiguration();
var unitTestRunnerTarget = new TraceTarget();
configuration.AddTarget("Unit test runner", unitTestRunnerTarget);
unitTestRunnerTarget.Layout =
"${time}|${pad:padding=-5:inner=${uppercase:${level}}}|${pad:padding=-16:inner=${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false:includeNamespace=false}}|${message}";
var logEverything = new LoggingRule("*", LogLevel.Trace, unitTestRunnerTarget);
configuration.LoggingRules.Add(logEverything);
LogManager.Configuration = configuration;
log = LogManager.GetCurrentClassLogger();
log.Info("Logging initialized");
}
public void OnAssemblyComplete() { }
}
在任何地方都没有XML配置文件,这是设置日志记录配置的唯一位置。
当我运行单元测试时,我得到的输出就像这个例子:
22:37:51.0253|DEBUG|SimulatorState |Transitioning from => TA.DigitalDomeworks.HardwareSimulator.StateStartup 22:37:51.1263|DEBUG|SimulatorState |Transitioning from TA.DigitalDomeworks.HardwareSimulator.StateStartup => TA.DigitalDomeworks.HardwareSimulator.StateReceivingCommand nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.5923|INFO |TransactionObserver|Transaction pipeline connected to channel with endpoint Simulator:Fast nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.7393|INFO |TransactionObserver|Committing transaction TID=1 [GINF] [{no value}] 00:00:10 Created 22:37:51.7623|DEBUG|c__DisplayClass0_1`1|StatusTransaction[1]: Subscribe() nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.7932|INFO |SimulatorState |State [ReceiveCommand] received stimulus 'G' nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.7932|INFO |SimulatorState |State [ReceiveCommand] received stimulus 'I' nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.7943|INFO |SimulatorState |State [ReceiveCommand] received stimulus 'N' nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.7943|INFO |SimulatorState |State [ReceiveCommand] received stimulus 'F' 22:37:51.7943|DEBUG|SimulatorState |Transitioning from TA.DigitalDomeworks.HardwareSimulator.StateReceivingCommand => TA.DigitalDomeworks.HardwareSimulator.StateExecutingCommand 22:37:51.7943|DEBUG|StateExecutingCommand|Processing command [GINF] 22:37:51.7943|DEBUG|SimulatorState |Transitioning from TA.DigitalDomeworks.HardwareSimulator.StateExecutingCommand => TA.DigitalDomeworks.HardwareSimulator.StateSendStatus
如您所见,日志输出使用两种不同的布局编写。前几行符合预期,然后出现了这种怪异:
nCrunch.TestHost462.x86.exe Information: 0 : 22:37:51.5923|INFO |TransactionObserver|Transaction pipeline connected to channel with endpoint Simulator:Fast
它几乎就像输出格式化两次,一次使用默认格式然后再次使用我想要的格式。似乎并不是关于"奇怪"输出发生了。在第1行中,输出来自类SimulatorState
,然后在第6行中有更多来自SimulatorState
的输出,但这次它是突变格式。
这怎么可能?