商信互联
本主题描述可用于从代码向XAF日志文件添加信息的API 。该DevExpress.Persistent.Base.Tracing类是用于这一目的。
该跟踪类不存储日志字符串内部。它将所有字符串传递给Trace.WriteLine方法。
您可以使用静态Tracing.Tracer属性访问Tracing实例,然后调用其方法之一。
using DevExpress.Persistent.Base;
// ...
Tracing.Tracer.LogText("Some text");
Imports DevExpress.Persistent.Base
' ...
Tracing.Tracer.LogText("Some text")
下表列出了Tracing类的最重要的方法。
方法 | 记录信息 | 样品结果 |
---|---|---|
LogText(字符串文本) | 作为方法参数传递的文本 | 06.09.16 11:58:10.739文字 |
LogValue(字符串valueName,对象objectValue) | 标题和值作为参数传递,以冒号分隔。 | 06.09.16 11:58:10.739 ValueName:ObjectValue |
LogSeparator(字符串注释) | 作为方法的参数传递的文本及其下的分隔符。 | 06.09.16 11:58:10.739评论 06.09.06 11:58:10.739 ==================== |
LogSubSeparator(字符串注释) | 分隔符,然后将文本作为值参数传递。 | 06.09.16 11:58:10.739 -------------- 06.09.06 11:58:10.739评论 |
LogError(Exception exception) | 指定异常的类型和消息,后跟堆栈跟踪。 | 发生的错误 类型:异常类型 消息:异常消息 堆栈跟踪: ... |
您可以继承Tracing类并重写其虚拟方法以实现自定义日志记录。
using DevExpress.Persistent.Base;
// ...
public class MyTracing : Tracing {
public override void LogError(Exception exception) {
// Implement custom logging for exceptions here.
}
// You can also override other virtual methods of Tracing
}
Imports DevExpress.Persistent.Base
' ...
Public Class MyTracing
Inherits Tracing
Public Overrides Sub LogError(ByVal exception As Exception)
' Implement custom logging for exceptions here.
End Sub
' You can also override other virtual methods of Tracing
End Class
初始化Tracing.Tracer实例时,将发生静态Tracing.CreateCustomTracer事件。处理此事件,将默认的跟踪实例替换为自定义实例。
using DevExpress.Persistent.Base;
// ...
Tracing.CreateCustomTracer = delegate(object s, CreateCustomTracerEventArgs args) {
args.Tracer = new MyTracing();
};
Imports DevExpress.Persistent.Base
' ...
AddHandler Tracing.CreateCustomTracer, Sub(s As Object, args As CreateCustomTracerEventArgs)
args.Tracer = New MyTracing()
End Sub
您可以将此代码放在以下位置之一: