【发布时间】:2016-03-14 02:46:28
【问题描述】:
我有一个 TraceSource 类的包装器。我想知道如何对它进行单元测试。由于我需要配置 app.config 文件以便将跟踪侦听器添加到 TraceSource 对象,添加 SwtichSource 等。那么在单元测试 dll 中手动添加 app.config 文件并将其配置为进行单元测试是否有效?我知道我们可以以编程方式完成所有这些,但是我在 TraceSource 周围创建一个包装器的方式看起来很棘手,或者可能是不可能的。任何建议都将受到欢迎。
public class Logger : ILogger
{
private const int ERROR_EVENT_ID = 2;
private const int DEBUG_EVENT_ID = 5;
private static TraceSource source;
public Logger(string nameOfComponent)
{
source = new TraceSource(nameOfComponent);
}
public void LogDebug(string message, string methodName)
{
if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(methodName))
throw new ArgumentNullException("message, methodName can't be null or empty");
source.TraceEvent(TraceEventType.Verbose, DEBUG_EVENT_ID);
}
public void LogError(string methodName, Exception ex)
{
LogError(null, methodName, ex);
}
public void LogError(string message, string methodName, Exception ex) {
if (String.IsNullOrEmpty(message) || String.IsNullOrEmpty(methodName) || ex == null)
throw new ArgumentNullException("message, methodName and exception can't be null or empty");
source.TraceData(TraceEventType.Error, ERROR_EVENT_ID, message, methodName, ex);
}
}
【问题讨论】:
标签: c# unit-testing logging trace