我通过创建一个新的 Trace Writter 为我的 Web API 应用程序启用了跟踪,它只将详细信息记录在一个文件中:
public class MyTraceWriter : ITraceWriter
{
private readonly ILog _log;
public MyTraceWriter(ILog log)
{
_log = log;
}
public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
{
var traceRecord = new TraceRecord(request, category, level);
traceAction(traceRecord);
_log.Info(GetDetails(traceRecord));
}
private string GetDetails(TraceRecord traceRecord)
{
var sb = new StringBuilder();
sb.AppendFormat("\r\n\t{0} {1}\r\n\tCategory={2}, Level={3}, Kind={4}\r\n\tOperator:{5}, Operation: {6}",
traceRecord.Request.Method,
traceRecord.Request.RequestUri,
traceRecord.Category,
traceRecord.Level,
traceRecord.Kind,
traceRecord.Operator,
traceRecord.Operation);
if (traceRecord.Exception != null)
{
sb.AppendFormat("\r\n\tException : {0}", traceRecord.Exception.GetBaseException().Message);
}
else if (traceRecord.Message != null)
{
sb.AppendFormat("\r\n\tMessage : {0}", traceRecord.Message);
}
sb.AppendLine();
return sb.ToString();
}
}
然后在 Global.ascx 中注册我的课程:
config.Services.Replace(typeof(ITraceWriter),
new MyTraceWriter(LogManager.GetLogger("Tracing")));
我只是想知道是否有任何内置功能/工具/页面可以使用/监控来查看跟踪的详细信息?
如果可能的话,我想避免创建自己的自定义数据库表和页面。